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