All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
buffer.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2 /*
3  * Copyright (c) 2013, Regents of the University of California
4  *
5  * BSD license, See the LICENSE file for more information
6  *
7  * Author: Alexander Afanasyev <[email protected]>
8  */
9 
10 #ifndef NDN_BUFFER_HPP
11 #define NDN_BUFFER_HPP
12 
13 #include "../common.hpp"
14 
15 namespace ndn {
16 
17 class Buffer;
18 typedef ptr_lib::shared_ptr<const Buffer> ConstBufferPtr;
19 typedef ptr_lib::shared_ptr<Buffer> BufferPtr;
20 
28 class Buffer : public std::vector<uint8_t>
29 {
30 public:
35  {
36  }
37 
42  explicit
43  Buffer(size_t size)
44  : std::vector<uint8_t>(size, 0)
45  {
46  }
47 
53  Buffer(const void* buf, size_t length)
54  : std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(buf),
55  reinterpret_cast<const uint8_t*>(buf) + length)
56  {
57  }
58 
67  template <class InputIterator>
68  Buffer(InputIterator first, InputIterator last)
69  : std::vector<uint8_t>(first, last)
70  {
71  }
72 
76  uint8_t*
77  get()
78  {
79  return &front();
80  }
81 
85  uint8_t*
86  buf()
87  {
88  return &front();
89  }
90 
95  template<class T>
96  T*
97  get()
98  {
99  return reinterpret_cast<T*>(&front());
100  }
101 
105  const uint8_t*
106  buf() const
107  {
108  return &front();
109  }
110 
114  const uint8_t*
115  get() const
116  {
117  return &front();
118  }
119 
124  template<class T>
125  const T*
126  get() const
127  {
128  return reinterpret_cast<const T*>(&front());
129  }
130 };
131 
133 namespace iostreams
134 {
135 
136 class buffer_append_device
137 {
138 public:
139  typedef char char_type;
140  typedef boost::iostreams::sink_tag category;
141 
142  buffer_append_device(Buffer& container)
143  : m_container(container)
144  {
145  }
146 
147  std::streamsize
148  write(const char_type* s, std::streamsize n)
149  {
150  std::copy(s, s+n, std::back_inserter(m_container));
151  return n;
152  }
153 
154 protected:
155  Buffer& m_container;
156 };
157 
158 } // iostreams
160 
176 struct OBufferStream : public boost::iostreams::stream<iostreams::buffer_append_device>
177 {
182  : m_buffer(ptr_lib::make_shared<Buffer>())
183  , m_device(*m_buffer)
184  {
185  open(m_device);
186  }
187 
191  ptr_lib::shared_ptr<Buffer>
192  buf()
193  {
194  flush();
195  return m_buffer;
196  }
197 
198 private:
199  BufferPtr m_buffer;
200  iostreams::buffer_append_device m_device;
201 };
202 
203 
204 } // ndn
205 
206 #endif // NDN_BUFFER_HPP
Class implementing interface similar to ostringstream, but to construct ndn::Buffer.
Definition: buffer.hpp:176
Buffer()
Creates an empty buffer.
Definition: buffer.hpp:34
Buffer(const void *buf, size_t length)
Create a buffer by copying the supplied data from a const buffer.
Definition: buffer.hpp:53
ptr_lib::shared_ptr< Buffer > BufferPtr
Definition: buffer.hpp:19
static const bool Buffer
ptr_lib::shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:17
const uint8_t * buf() const
Get pointer to the first byte of the buffer (alternative version)
Definition: buffer.hpp:106
uint8_t * buf()
Get pointer to the first byte of the buffer (alternative version)
Definition: buffer.hpp:86
ptr_lib::shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Definition: buffer.hpp:192
Buffer(InputIterator first, InputIterator last)
Create a buffer by copying the supplied data using iterator interface.
Definition: buffer.hpp:68
Buffer(size_t size)
Creates a buffer with pre-allocated size.
Definition: buffer.hpp:43
Class representing a general-use automatically managed/resized buffer.
Definition: buffer.hpp:28
OBufferStream()
Default constructor.
Definition: buffer.hpp:181