Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [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 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 24 | #ifndef NDN_ENCODING_BUFFER_HPP |
| 25 | #define NDN_ENCODING_BUFFER_HPP |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 27 | #include "../common.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 31 | namespace ndn { |
| 32 | |
| 33 | class Buffer; |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 34 | typedef shared_ptr<const Buffer> ConstBufferPtr; |
| 35 | typedef shared_ptr<Buffer> BufferPtr; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * @brief Class representing a general-use automatically managed/resized buffer |
| 39 | * |
| 40 | * In most respect, Buffer class is equivalent to std::vector<uint8_t> and is in fact |
| 41 | * uses it as a base class. In addition to that, it provides buf() and buf<T>() helper |
| 42 | * method for easier access to the underlying data (buf<T>() casts pointer to the requested class) |
| 43 | */ |
| 44 | class Buffer : public std::vector<uint8_t> |
| 45 | { |
| 46 | public: |
| 47 | /** |
| 48 | * @brief Creates an empty buffer |
| 49 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 50 | Buffer() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 51 | { |
| 52 | } |
| 53 | |
| 54 | /** |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 55 | * @brief Creates a buffer with pre-allocated size |
| 56 | * @param size size of the buffer to be allocated |
| 57 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 58 | explicit |
| 59 | Buffer(size_t size) |
| 60 | : std::vector<uint8_t>(size, 0) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 61 | { |
| 62 | } |
| 63 | |
| 64 | /** |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 65 | * @brief Create a buffer by copying the supplied data from a const buffer |
| 66 | * @param buf const pointer to buffer |
| 67 | * @param length length of the buffer to copy |
| 68 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 69 | Buffer(const void* buf, size_t length) |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 70 | : std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(buf), |
| 71 | reinterpret_cast<const uint8_t*>(buf) + length) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 72 | { |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @brief Create a buffer by copying the supplied data using iterator interface |
| 77 | * |
| 78 | * Note that the supplied iterators must be compatible with std::vector<uint8_t> interface |
| 79 | * |
| 80 | * @param first iterator to a first element to copy |
| 81 | * @param last iterator to an element immediately following the last element to copy |
| 82 | */ |
| 83 | template <class InputIterator> |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 84 | Buffer(InputIterator first, InputIterator last) |
| 85 | : std::vector<uint8_t>(first, last) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 86 | { |
| 87 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 88 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 89 | /** |
| 90 | * @brief Get pointer to the first byte of the buffer |
| 91 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 92 | uint8_t* |
| 93 | get() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 94 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 95 | return &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @brief Get pointer to the first byte of the buffer (alternative version) |
| 100 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 101 | uint8_t* |
| 102 | buf() |
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 &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 and cast |
| 109 | * it (reinterpret_cast) to the requested type T |
| 110 | */ |
| 111 | template<class T> |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 112 | T* |
| 113 | get() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 114 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 115 | return reinterpret_cast<T*>(&front()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @brief Get pointer to the first byte of the buffer (alternative version) |
| 120 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 121 | const uint8_t* |
| 122 | buf() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 123 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 124 | return &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @brief Get const pointer to the first byte of the buffer |
| 129 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 130 | const uint8_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 &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @brief Get const pointer to the first byte of the buffer and cast |
| 138 | * it (reinterpret_cast) to the requested type T |
| 139 | */ |
| 140 | template<class T> |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 141 | const T* |
| 142 | get() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 143 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 144 | return reinterpret_cast<const T*>(&front()); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 145 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 146 | }; |
| 147 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 148 | } // ndn |
| 149 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 150 | #endif // NDN_ENCODING_BUFFER_HPP |