buffer.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2021 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
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.
20  *
21  * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22  */
23 
24 #ifndef NDN_CXX_ENCODING_BUFFER_HPP
25 #define NDN_CXX_ENCODING_BUFFER_HPP
26 
28 
29 #include <initializer_list>
30 #include <vector>
31 
32 namespace ndn {
33 
41 class Buffer : public std::vector<uint8_t>
42 {
43 public:
46  Buffer() = default;
47 
50  Buffer(const Buffer&);
51 
54  Buffer&
55  operator=(const Buffer&);
56 
59  Buffer(Buffer&&) noexcept;
60 
63  Buffer&
64  operator=(Buffer&&) noexcept;
65 
69  explicit
70  Buffer(size_t size)
71  : std::vector<uint8_t>(size, 0)
72  {
73  }
74 
79  Buffer(const void* buf, size_t length)
80  : std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(buf),
81  reinterpret_cast<const uint8_t*>(buf) + length)
82  {
83  }
84 
89  template<class InputIt>
90  Buffer(InputIt first, InputIt last)
91  : std::vector<uint8_t>(first, last)
92  {
93  }
94 
97  Buffer(std::initializer_list<uint8_t> il)
98  : std::vector<uint8_t>(il)
99  {
100  }
101 
104  template<class T>
105  T*
106  get() noexcept
107  {
108  return reinterpret_cast<T*>(data());
109  }
110 
113  template<class T>
114  const T*
115  get() const noexcept
116  {
117  return reinterpret_cast<const T*>(data());
118  }
119 };
120 
121 inline
122 Buffer::Buffer(const Buffer&) = default;
123 
124 inline Buffer&
125 Buffer::operator=(const Buffer&) = default;
126 
127 inline
128 Buffer::Buffer(Buffer&&) noexcept = default;
129 
130 inline Buffer&
131 Buffer::operator=(Buffer&&) noexcept = default;
132 
134 std::ostream&
135 boost_test_print_type(std::ostream&, const Buffer&);
138 using BufferPtr = shared_ptr<Buffer>;
139 using ConstBufferPtr = shared_ptr<const Buffer>;
140 
141 } // namespace ndn
142 
143 #endif // NDN_CXX_ENCODING_BUFFER_HPP
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:42
Buffer & operator=(const Buffer &)
Copy assignment operator.
Buffer(const Buffer &)
Copy constructor.
Buffer()=default
Creates an empty Buffer.
Buffer(InputIt first, InputIt last)
Creates a Buffer by copying the elements of the range [first, last)
Definition: buffer.hpp:90
T * get() noexcept
Definition: buffer.hpp:106
Buffer(std::initializer_list< uint8_t > il)
Creates a Buffer with the contents of an initializer list.
Definition: buffer.hpp:97
const T * get() const noexcept
Definition: buffer.hpp:115
Buffer(const void *buf, size_t length)
Creates a Buffer by copying contents from a raw buffer.
Definition: buffer.hpp:79
Buffer(Buffer &&) noexcept
Move constructor.
Common includes and macros used throughout the library.
Definition: data.cpp:25
std::ostream & boost_test_print_type(std::ostream &os, const Buffer &buf)
Definition: buffer.cpp:28
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
shared_ptr< Buffer > BufferPtr
Definition: buffer.hpp:138