Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
| 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 5 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * 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 Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 8 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 9 | * 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 Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 15 | #ifndef NDN_ENCODING_BUFFER_HPP |
| 16 | #define NDN_ENCODING_BUFFER_HPP |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 17 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 18 | #include "../common.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 19 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 22 | namespace ndn { |
| 23 | |
| 24 | class Buffer; |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 25 | typedef shared_ptr<const Buffer> ConstBufferPtr; |
| 26 | typedef shared_ptr<Buffer> BufferPtr; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 27 | |
| 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 | */ |
| 35 | class Buffer : public std::vector<uint8_t> |
| 36 | { |
| 37 | public: |
| 38 | /** |
| 39 | * @brief Creates an empty buffer |
| 40 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 41 | Buffer() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | /** |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 46 | * @brief Creates a buffer with pre-allocated size |
| 47 | * @param size size of the buffer to be allocated |
| 48 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 49 | explicit |
| 50 | Buffer(size_t size) |
| 51 | : std::vector<uint8_t>(size, 0) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
| 55 | /** |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 56 | * @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 Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 60 | Buffer(const void* buf, size_t length) |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 61 | : std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(buf), |
| 62 | reinterpret_cast<const uint8_t*>(buf) + length) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 63 | { |
| 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 Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 75 | Buffer(InputIterator first, InputIterator last) |
| 76 | : std::vector<uint8_t>(first, last) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 77 | { |
| 78 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 79 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 80 | /** |
| 81 | * @brief Get pointer to the first byte of the buffer |
| 82 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 83 | uint8_t* |
| 84 | get() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 85 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 86 | return &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @brief Get pointer to the first byte of the buffer (alternative version) |
| 91 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 92 | uint8_t* |
| 93 | buf() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 94 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 95 | return &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 96 | } |
| 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 Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 103 | T* |
| 104 | get() |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 105 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 106 | return reinterpret_cast<T*>(&front()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @brief Get pointer to the first byte of the buffer (alternative version) |
| 111 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 112 | const uint8_t* |
| 113 | buf() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 114 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 115 | return &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @brief Get const pointer to the first byte of the buffer |
| 120 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 121 | const uint8_t* |
| 122 | get() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 123 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 124 | return &front(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 125 | } |
| 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 Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 132 | const T* |
| 133 | get() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 134 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 135 | return reinterpret_cast<const T*>(&front()); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 136 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 139 | } // ndn |
| 140 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 141 | #endif // NDN_ENCODING_BUFFER_HPP |