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-2024 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 
27 #include <cstdint>
28 #include <initializer_list>
29 #include <iosfwd>
30 #include <memory>
31 #include <vector>
32 
33 namespace ndn {
34 
42 class Buffer : public std::vector<uint8_t>
43 {
44 public:
47  Buffer() = default;
48 
51  Buffer(const Buffer&);
52 
55  Buffer&
56  operator=(const Buffer&);
57 
60  Buffer(Buffer&&) noexcept;
61 
64  Buffer&
65  operator=(Buffer&&) noexcept;
66 
70  explicit
71  Buffer(size_t size)
72  : std::vector<uint8_t>(size, 0)
73  {
74  }
75 
80  Buffer(const void* buf, size_t length)
81  : std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(buf),
82  reinterpret_cast<const uint8_t*>(buf) + length)
83  {
84  }
85 
90  template<class InputIt>
91  Buffer(InputIt first, InputIt last)
92  : std::vector<uint8_t>(first, last)
93  {
94  }
95 
98  Buffer(std::initializer_list<uint8_t> il)
99  : std::vector<uint8_t>(il)
100  {
101  }
102 
105  template<class T>
106  T*
107  get() noexcept
108  {
109  return reinterpret_cast<T*>(data());
110  }
111 
114  template<class T>
115  const T*
116  get() const noexcept
117  {
118  return reinterpret_cast<const T*>(data());
119  }
120 };
121 
122 inline
123 Buffer::Buffer(const Buffer&) = default;
124 
125 inline Buffer&
126 Buffer::operator=(const Buffer&) = default;
127 
128 inline
129 Buffer::Buffer(Buffer&&) noexcept = default;
130 
131 inline Buffer&
132 Buffer::operator=(Buffer&&) noexcept = default;
133 
135 std::ostream&
136 boost_test_print_type(std::ostream&, const Buffer&);
139 using BufferPtr = std::shared_ptr<Buffer>;
140 using ConstBufferPtr = std::shared_ptr<const Buffer>;
141 
142 } // namespace ndn
143 
144 #endif // NDN_CXX_ENCODING_BUFFER_HPP
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:43
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:91
T * get() noexcept
Definition: buffer.hpp:107
Buffer(std::initializer_list< uint8_t > il)
Creates a Buffer with the contents of an initializer list.
Definition: buffer.hpp:98
const T * get() const noexcept
Definition: buffer.hpp:116
Buffer(const void *buf, size_t length)
Creates a Buffer by copying contents from a raw buffer.
Definition: buffer.hpp:80
Buffer(Buffer &&) noexcept
Move constructor.
Definition: data.cpp:25
std::ostream & boost_test_print_type(std::ostream &os, const Buffer &buf)
Definition: buffer.cpp:28
std::shared_ptr< Buffer > BufferPtr
Definition: buffer.hpp:139
std::shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:140