Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 33 | /** |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 34 | * @brief General-purpose automatically managed/resized buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 35 | * |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 36 | * In most respect, the Buffer class is equivalent to a `std::vector<uint8_t>`, and it in fact |
| 37 | * uses the latter as a base class. In addition to that, it provides the get<T>() helper method |
| 38 | * that automatically casts the returned pointer to the requested type. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 39 | */ |
| 40 | class Buffer : public std::vector<uint8_t> |
| 41 | { |
| 42 | public: |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 43 | /** @brief Creates an empty Buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 44 | */ |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 45 | Buffer() = default; |
| 46 | |
| 47 | /** @brief Copy constructor |
| 48 | */ |
| 49 | Buffer(const Buffer&); |
| 50 | |
| 51 | /** @brief Copy assignment operator |
| 52 | */ |
| 53 | Buffer& |
| 54 | operator=(const Buffer&); |
| 55 | |
| 56 | /** @brief Move constructor |
| 57 | */ |
| 58 | Buffer(Buffer&&) noexcept; |
| 59 | |
| 60 | /** @brief Move assignment operator |
| 61 | */ |
| 62 | Buffer& |
| 63 | operator=(Buffer&&) noexcept; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 64 | |
Davide Pesavento | 35de339 | 2017-10-15 22:00:07 -0400 | [diff] [blame] | 65 | /** @brief Creates a Buffer with pre-allocated size |
| 66 | * @param size size of the Buffer to be allocated |
| 67 | */ |
| 68 | explicit |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 69 | Buffer(size_t size) |
| 70 | : std::vector<uint8_t>(size, 0) |
| 71 | { |
| 72 | } |
Davide Pesavento | 35de339 | 2017-10-15 22:00:07 -0400 | [diff] [blame] | 73 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 74 | /** @brief Creates a Buffer by copying contents from a raw buffer |
| 75 | * @param buf const pointer to buffer to copy |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 76 | * @param length length of the buffer to copy |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 77 | */ |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 78 | Buffer(const void* buf, size_t length) |
| 79 | : std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(buf), |
| 80 | reinterpret_cast<const uint8_t*>(buf) + length) |
| 81 | { |
| 82 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 83 | |
Davide Pesavento | 35de339 | 2017-10-15 22:00:07 -0400 | [diff] [blame] | 84 | /** @brief Creates a Buffer by copying the elements of the range [first, last) |
| 85 | * @param first an input iterator to the first element to copy |
| 86 | * @param last an input iterator to the element immediately following the last element to copy |
| 87 | */ |
| 88 | template<class InputIt> |
| 89 | Buffer(InputIt first, InputIt last) |
| 90 | : std::vector<uint8_t>(first, last) |
| 91 | { |
| 92 | } |
| 93 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 94 | /** @return pointer to the first byte of the buffer, cast to the requested type T |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 95 | */ |
| 96 | template<class T> |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 97 | T* |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 98 | get() noexcept |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 99 | { |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 100 | return reinterpret_cast<T*>(data()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 103 | /** @return const pointer to the first byte of the buffer, cast to the requested type T |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 104 | */ |
| 105 | template<class T> |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 106 | const T* |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 107 | get() const noexcept |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 108 | { |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 109 | return reinterpret_cast<const T*>(data()); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 110 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 113 | inline |
| 114 | Buffer::Buffer(const Buffer&) = default; |
| 115 | |
| 116 | inline Buffer& |
| 117 | Buffer::operator=(const Buffer&) = default; |
| 118 | |
| 119 | inline |
| 120 | Buffer::Buffer(Buffer&&) noexcept = default; |
| 121 | |
| 122 | inline Buffer& |
| 123 | Buffer::operator=(Buffer&&) noexcept = default; |
| 124 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 125 | using BufferPtr = shared_ptr<Buffer>; |
| 126 | using ConstBufferPtr = shared_ptr<const Buffer>; |
| 127 | |
Junxiao Shi | c97d5d4 | 2014-11-27 09:33:37 -0700 | [diff] [blame] | 128 | } // namespace ndn |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 129 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 130 | #endif // NDN_ENCODING_BUFFER_HPP |