blob: 07591ee80cdd1f7222b87edc658a4032f3823561 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08004 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080022 */
23
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070024#ifndef NDN_ENCODING_BUFFER_HPP
25#define NDN_ENCODING_BUFFER_HPP
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080026
Alexander Afanasyev19508852014-01-29 01:01:51 -080027#include "../common.hpp"
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080028
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070029#include <vector>
30
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080031namespace ndn {
32
33class Buffer;
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070034typedef shared_ptr<const Buffer> ConstBufferPtr;
35typedef shared_ptr<Buffer> BufferPtr;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080036
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 */
44class Buffer : public std::vector<uint8_t>
45{
46public:
47 /**
48 * @brief Creates an empty buffer
49 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -070050 Buffer()
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080051 {
52 }
53
54 /**
Wentao Shang77949212014-02-01 23:42:24 -080055 * @brief Creates a buffer with pre-allocated size
56 * @param size size of the buffer to be allocated
57 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -070058 explicit
59 Buffer(size_t size)
60 : std::vector<uint8_t>(size, 0)
Wentao Shang77949212014-02-01 23:42:24 -080061 {
62 }
63
64 /**
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080065 * @brief Create a buffer by copying the supplied data from a const buffer
66 * @param buf const pointer to buffer
67 * @param length length of the buffer to copy
68 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069 Buffer(const void* buf, size_t length)
Alexander Afanasyeva465e972014-03-22 17:21:49 -070070 : std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(buf),
71 reinterpret_cast<const uint8_t*>(buf) + length)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080072 {
73 }
74
75 /**
76 * @brief Create a buffer by copying the supplied data using iterator interface
77 *
78 * Note that the supplied iterators must be compatible with std::vector<uint8_t> interface
79 *
80 * @param first iterator to a first element to copy
81 * @param last iterator to an element immediately following the last element to copy
82 */
83 template <class InputIterator>
Alexander Afanasyeva465e972014-03-22 17:21:49 -070084 Buffer(InputIterator first, InputIterator last)
85 : std::vector<uint8_t>(first, last)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080086 {
87 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070088
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080089 /**
90 * @brief Get pointer to the first byte of the buffer
91 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -070092 uint8_t*
93 get()
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080094 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -070095 return &front();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080096 }
97
98 /**
99 * @brief Get pointer to the first byte of the buffer (alternative version)
100 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700101 uint8_t*
102 buf()
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800103 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700104 return &front();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800105 }
106
107 /**
108 * @brief Get pointer to the first byte of the buffer and cast
109 * it (reinterpret_cast) to the requested type T
110 */
111 template<class T>
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700112 T*
113 get()
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800114 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700115 return reinterpret_cast<T*>(&front());
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800116 }
117
118 /**
119 * @brief Get pointer to the first byte of the buffer (alternative version)
120 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700121 const uint8_t*
122 buf() const
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800123 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700124 return &front();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800125 }
126
127 /**
128 * @brief Get const pointer to the first byte of the buffer
129 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700130 const uint8_t*
131 get() const
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800132 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700133 return &front();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800134 }
135
136 /**
137 * @brief Get const pointer to the first byte of the buffer and cast
138 * it (reinterpret_cast) to the requested type T
139 */
140 template<class T>
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700141 const T*
142 get() const
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800143 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700144 return reinterpret_cast<const T*>(&front());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700145 }
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800146};
147
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800148} // ndn
149
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700150#endif // NDN_ENCODING_BUFFER_HPP