blob: 5701511b22e9fdcc7ad74eb0b588fd6ba8651a7c [file] [log] [blame]
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08005 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08008 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07009 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080013 */
14
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070015#ifndef NDN_ENCODING_BUFFER_HPP
16#define NDN_ENCODING_BUFFER_HPP
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080017
Alexander Afanasyev19508852014-01-29 01:01:51 -080018#include "../common.hpp"
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080019
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070020#include <vector>
21
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080022namespace ndn {
23
24class Buffer;
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070025typedef shared_ptr<const Buffer> ConstBufferPtr;
26typedef shared_ptr<Buffer> BufferPtr;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080027
28/**
29 * @brief Class representing a general-use automatically managed/resized buffer
30 *
31 * In most respect, Buffer class is equivalent to std::vector<uint8_t> and is in fact
32 * uses it as a base class. In addition to that, it provides buf() and buf<T>() helper
33 * method for easier access to the underlying data (buf<T>() casts pointer to the requested class)
34 */
35class Buffer : public std::vector<uint8_t>
36{
37public:
38 /**
39 * @brief Creates an empty buffer
40 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -070041 Buffer()
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080042 {
43 }
44
45 /**
Wentao Shang77949212014-02-01 23:42:24 -080046 * @brief Creates a buffer with pre-allocated size
47 * @param size size of the buffer to be allocated
48 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -070049 explicit
50 Buffer(size_t size)
51 : std::vector<uint8_t>(size, 0)
Wentao Shang77949212014-02-01 23:42:24 -080052 {
53 }
54
55 /**
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080056 * @brief Create a buffer by copying the supplied data from a const buffer
57 * @param buf const pointer to buffer
58 * @param length length of the buffer to copy
59 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060 Buffer(const void* buf, size_t length)
Alexander Afanasyeva465e972014-03-22 17:21:49 -070061 : std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(buf),
62 reinterpret_cast<const uint8_t*>(buf) + length)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080063 {
64 }
65
66 /**
67 * @brief Create a buffer by copying the supplied data using iterator interface
68 *
69 * Note that the supplied iterators must be compatible with std::vector<uint8_t> interface
70 *
71 * @param first iterator to a first element to copy
72 * @param last iterator to an element immediately following the last element to copy
73 */
74 template <class InputIterator>
Alexander Afanasyeva465e972014-03-22 17:21:49 -070075 Buffer(InputIterator first, InputIterator last)
76 : std::vector<uint8_t>(first, last)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080077 {
78 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070079
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080080 /**
81 * @brief Get pointer to the first byte of the buffer
82 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -070083 uint8_t*
84 get()
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080085 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -070086 return &front();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080087 }
88
89 /**
90 * @brief Get pointer to the first byte of the buffer (alternative version)
91 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -070092 uint8_t*
93 buf()
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 and cast
100 * it (reinterpret_cast) to the requested type T
101 */
102 template<class T>
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700103 T*
104 get()
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800105 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700106 return reinterpret_cast<T*>(&front());
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800107 }
108
109 /**
110 * @brief Get pointer to the first byte of the buffer (alternative version)
111 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700112 const uint8_t*
113 buf() const
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800114 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700115 return &front();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800116 }
117
118 /**
119 * @brief Get const pointer to the first byte of the buffer
120 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700121 const uint8_t*
122 get() 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 and cast
129 * it (reinterpret_cast) to the requested type T
130 */
131 template<class T>
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700132 const T*
133 get() const
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800134 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700135 return reinterpret_cast<const T*>(&front());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136 }
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800137};
138
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800139} // ndn
140
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700141#endif // NDN_ENCODING_BUFFER_HPP