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: |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 47 | /** @brief Creates an empty buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 48 | */ |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 49 | Buffer(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 50 | |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 51 | /** @brief Creates a buffer with pre-allocated size |
| 52 | * @param size size of the buffer to be allocated |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 53 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 54 | explicit |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 55 | Buffer(size_t size); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 56 | |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 57 | /** @brief Create a buffer by copying contents from a buffer |
| 58 | * @param buf const pointer to buffer |
| 59 | * @param length length of the buffer to copy |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 60 | */ |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 61 | Buffer(const void* buf, size_t length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 62 | |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 63 | /** @brief Create a buffer by copying contents of the range [first, last) |
| 64 | * @tparam InputIterator an InputIterator compatible with std::vector<uint8_t> constructor |
| 65 | * @param first iterator to the first element to copy |
| 66 | * @param last iterator to the element immediately following the last element to copy |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 67 | */ |
| 68 | template <class InputIterator> |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 69 | Buffer(InputIterator first, InputIterator last) |
| 70 | : std::vector<uint8_t>(first, last) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 71 | { |
| 72 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 73 | |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 74 | /** @return pointer to the first byte of the buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 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 | |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 82 | /** @return pointer to the first byte of the buffer |
| 83 | * |
| 84 | * This is same as \p .get() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 85 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 86 | uint8_t* |
| 87 | buf() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 88 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 89 | return &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 92 | /** @return pointer to the first byte of the buffer and reinterpret_cast |
| 93 | * it to the requested type T |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 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 | |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 102 | /** @return pointer to the first byte of the buffer |
| 103 | * |
| 104 | * This is same as \p .get() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 105 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 106 | const uint8_t* |
| 107 | buf() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 108 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 109 | return &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 112 | /** @return pointer to the first byte of the buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 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 | |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 120 | /** @return const pointer to the first byte of the buffer and reinterpret_cast |
| 121 | * it to the requested type T |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 122 | */ |
| 123 | template<class T> |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 124 | const T* |
| 125 | get() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 126 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 127 | return reinterpret_cast<const T*>(&front()); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 128 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 131 | } // namespace ndn |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 133 | #endif // NDN_ENCODING_BUFFER_HPP |