blob: 994fbf899e8937803759de57d389966c86337614 [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:
Junxiao Shic97d5d42014-11-27 09:33:37 -070047 /** @brief Creates an empty buffer
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080048 */
Junxiao Shic97d5d42014-11-27 09:33:37 -070049 Buffer();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080050
Junxiao Shic97d5d42014-11-27 09:33:37 -070051 /** @brief Creates a buffer with pre-allocated size
52 * @param size size of the buffer to be allocated
Wentao Shang77949212014-02-01 23:42:24 -080053 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -070054 explicit
Junxiao Shic97d5d42014-11-27 09:33:37 -070055 Buffer(size_t size);
Wentao Shang77949212014-02-01 23:42:24 -080056
Junxiao Shic97d5d42014-11-27 09:33:37 -070057 /** @brief Create a buffer by copying contents from a buffer
58 * @param buf const pointer to buffer
59 * @param length length of the buffer to copy
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080060 */
Junxiao Shic97d5d42014-11-27 09:33:37 -070061 Buffer(const void* buf, size_t length);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080062
Junxiao Shic97d5d42014-11-27 09:33:37 -070063 /** @brief Create a buffer by copying contents of the range [first, last)
64 * @tparam InputIterator an InputIterator compatible with std::vector<uint8_t> constructor
65 * @param first iterator to the first element to copy
66 * @param last iterator to the element immediately following the last element to copy
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080067 */
68 template <class InputIterator>
Alexander Afanasyeva465e972014-03-22 17:21:49 -070069 Buffer(InputIterator first, InputIterator last)
70 : std::vector<uint8_t>(first, last)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080071 {
72 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070073
Junxiao Shic97d5d42014-11-27 09:33:37 -070074 /** @return pointer to the first byte of the buffer
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080075 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -070076 uint8_t*
77 get()
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080078 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -070079 return &front();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080080 }
81
Junxiao Shic97d5d42014-11-27 09:33:37 -070082 /** @return pointer to the first byte of the buffer
83 *
84 * This is same as \p .get()
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080085 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -070086 uint8_t*
87 buf()
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080088 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -070089 return &front();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080090 }
91
Junxiao Shic97d5d42014-11-27 09:33:37 -070092 /** @return pointer to the first byte of the buffer and reinterpret_cast
93 * it to the requested type T
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080094 */
95 template<class T>
Alexander Afanasyeva465e972014-03-22 17:21:49 -070096 T*
97 get()
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080098 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -070099 return reinterpret_cast<T*>(&front());
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800100 }
101
Junxiao Shic97d5d42014-11-27 09:33:37 -0700102 /** @return pointer to the first byte of the buffer
103 *
104 * This is same as \p .get()
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800105 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700106 const uint8_t*
107 buf() const
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800108 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700109 return &front();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800110 }
111
Junxiao Shic97d5d42014-11-27 09:33:37 -0700112 /** @return pointer to the first byte of the buffer
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800113 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700114 const uint8_t*
115 get() const
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800116 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700117 return &front();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800118 }
119
Junxiao Shic97d5d42014-11-27 09:33:37 -0700120 /** @return const pointer to the first byte of the buffer and reinterpret_cast
121 * it to the requested type T
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800122 */
123 template<class T>
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700124 const T*
125 get() const
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800126 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700127 return reinterpret_cast<const T*>(&front());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700128 }
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800129};
130
Junxiao Shic97d5d42014-11-27 09:33:37 -0700131} // namespace ndn
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800132
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700133#endif // NDN_ENCODING_BUFFER_HPP