NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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
Copyright (c) 2011-2015 Regents of the University of California.
Buffer(const void *buf, size_t length)
Creates a Buffer by copying contents from a raw buffer.
Definition: buffer.hpp:79
std::ostream & boost_test_print_type(std::ostream &os, const Buffer &buf)
Definition: buffer.cpp:28
Buffer & operator=(const Buffer &)
Copy assignment operator.
STL namespace.
Buffer(InputIt first, InputIt last)
Creates a Buffer by copying the elements of the range [first, last)
Definition: buffer.hpp:90
Buffer()=default
Creates an empty Buffer.
Common includes and macros used throughout the library.
Buffer(std::initializer_list< uint8_t > il)
Creates a Buffer with the contents of an initializer list.
Definition: buffer.hpp:97
Buffer(size_t size)
Creates a Buffer with pre-allocated size.
Definition: buffer.hpp:70
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span-lite.hpp:1535
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:41
shared_ptr< Buffer > BufferPtr
Definition: buffer.hpp:138
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139