Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 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 <alexander.afanasyev@ucla.edu> |
| 8 | */ |
| 9 | |
| 10 | #ifndef NDN_BUFFER_HPP |
| 11 | #define NDN_BUFFER_HPP |
| 12 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 13 | #include "../common.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 14 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 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 | |
| 21 | /** |
| 22 | * @brief Class representing a general-use automatically managed/resized buffer |
| 23 | * |
| 24 | * In most respect, Buffer class is equivalent to std::vector<uint8_t> and is in fact |
| 25 | * uses it as a base class. In addition to that, it provides buf() and buf<T>() helper |
| 26 | * method for easier access to the underlying data (buf<T>() casts pointer to the requested class) |
| 27 | */ |
| 28 | class Buffer : public std::vector<uint8_t> |
| 29 | { |
| 30 | public: |
| 31 | /** |
| 32 | * @brief Creates an empty buffer |
| 33 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 34 | Buffer() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
| 38 | /** |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 39 | * @brief Creates a buffer with pre-allocated size |
| 40 | * @param size size of the buffer to be allocated |
| 41 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 42 | explicit |
| 43 | Buffer(size_t size) |
| 44 | : std::vector<uint8_t>(size, 0) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
| 48 | /** |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 49 | * @brief Create a buffer by copying the supplied data from a const buffer |
| 50 | * @param buf const pointer to buffer |
| 51 | * @param length length of the buffer to copy |
| 52 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 53 | Buffer(const void* buf, size_t length) |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 54 | : std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(buf), |
| 55 | reinterpret_cast<const uint8_t*>(buf) + length) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @brief Create a buffer by copying the supplied data using iterator interface |
| 61 | * |
| 62 | * Note that the supplied iterators must be compatible with std::vector<uint8_t> interface |
| 63 | * |
| 64 | * @param first iterator to a first element to copy |
| 65 | * @param last iterator to an element immediately following the last element to copy |
| 66 | */ |
| 67 | template <class InputIterator> |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 68 | Buffer(InputIterator first, InputIterator last) |
| 69 | : std::vector<uint8_t>(first, last) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 70 | { |
| 71 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 72 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 73 | /** |
| 74 | * @brief Get pointer to the first byte of the buffer |
| 75 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 76 | uint8_t* |
| 77 | get() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 78 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 79 | return &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @brief Get pointer to the first byte of the buffer (alternative version) |
| 84 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 85 | uint8_t* |
| 86 | buf() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 87 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 88 | return &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @brief Get pointer to the first byte of the buffer and cast |
| 93 | * it (reinterpret_cast) to the requested type T |
| 94 | */ |
| 95 | template<class T> |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 96 | T* |
| 97 | get() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 98 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 99 | return reinterpret_cast<T*>(&front()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @brief Get pointer to the first byte of the buffer (alternative version) |
| 104 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 105 | const uint8_t* |
| 106 | buf() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 107 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 108 | return &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @brief Get const pointer to the first byte of the buffer |
| 113 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 114 | const uint8_t* |
| 115 | get() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 116 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 117 | return &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @brief Get const pointer to the first byte of the buffer and cast |
| 122 | * it (reinterpret_cast) to the requested type T |
| 123 | */ |
| 124 | template<class T> |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 125 | const T* |
| 126 | get() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 127 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 128 | return reinterpret_cast<const T*>(&front()); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 129 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | /// @cond include_hidden |
| 133 | namespace iostreams |
| 134 | { |
| 135 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 136 | class buffer_append_device |
| 137 | { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 138 | public: |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 139 | typedef char char_type; |
| 140 | typedef boost::iostreams::sink_tag category; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 141 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 142 | buffer_append_device(Buffer& container) |
| 143 | : m_container(container) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 144 | { |
| 145 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 146 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 147 | std::streamsize |
| 148 | write(const char_type* s, std::streamsize n) |
| 149 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 150 | std::copy(s, s+n, std::back_inserter(m_container)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 151 | return n; |
| 152 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 153 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 154 | protected: |
| 155 | Buffer& m_container; |
| 156 | }; |
| 157 | |
| 158 | } // iostreams |
| 159 | /// @endcond |
| 160 | |
| 161 | /** |
| 162 | * Class implementing interface similar to ostringstream, but to construct ndn::Buffer |
| 163 | * |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 164 | * The benefit of using stream interface is that it provides automatic buffering of |
| 165 | * written data and eliminates (or reduces) overhead of resizing the underlying buffer |
| 166 | * when writing small pieces of data. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 167 | * |
| 168 | * Usage example: |
| 169 | * @code |
| 170 | * OBufferStream obuf; |
| 171 | * obuf.put(0); |
| 172 | * obuf.write(another_buffer, another_buffer_size); |
| 173 | * ptr_lib::shared_ptr<Buffer> buf = obuf.get(); |
| 174 | * @endcode |
| 175 | */ |
| 176 | struct OBufferStream : public boost::iostreams::stream<iostreams::buffer_append_device> |
| 177 | { |
| 178 | /** |
| 179 | * Default constructor |
| 180 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 181 | OBufferStream() |
| 182 | : m_buffer(ptr_lib::make_shared<Buffer>()) |
| 183 | , m_device(*m_buffer) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 184 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 185 | open(m_device); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Flush written data to the stream and return shared pointer to the underlying buffer |
| 190 | */ |
| 191 | ptr_lib::shared_ptr<Buffer> |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 192 | buf() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 193 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 194 | flush(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 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 |