Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame^] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #ifndef NDN_BLOB_HPP |
| 7 | #define NDN_BLOB_HPP |
| 8 | |
| 9 | #include "../common.hpp" |
| 10 | |
| 11 | namespace ndn { |
| 12 | |
| 13 | /** |
| 14 | * A Blob holds a pointer to an immutable byte array. |
| 15 | */ |
| 16 | class Blob { |
| 17 | public: |
| 18 | /** |
| 19 | * Create a new Blob with an immutable copy of the given array. |
| 20 | * @param value A pointer to the byte array which is copied. |
| 21 | * @param valueLength The length of value. |
| 22 | */ |
| 23 | Blob(const unsigned char* value, unsigned int valueLength) |
| 24 | : value_(new std::vector<unsigned char>(value, value + valueLength)) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Create a new Blob with an immutable copy of the array in the given vector. |
| 30 | * @param value A reference to a vector which is copied. |
| 31 | */ |
| 32 | Blob(const std::vector<unsigned char> &value) |
| 33 | : value_(new std::vector<unsigned char>(value)) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Create a new Blob to point to an existing byte array. IMPORTANT: After calling this constructor, |
| 39 | * you must treat the array as immutable and promise not to change it. |
| 40 | * @param value A pointer to a vector with the byte array. This takes another reference and does not copy the bytes. |
| 41 | */ |
| 42 | Blob(const ptr_lib::shared_ptr<std::vector<unsigned char> > &value) |
| 43 | : value_(value) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Return a const reference to the immutable byte array. |
| 49 | */ |
| 50 | const std::vector<unsigned char>& getValue() const |
| 51 | { |
| 52 | return *value_; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Return the length of the immutable byte array. |
| 57 | */ |
| 58 | unsigned int size() const |
| 59 | { |
| 60 | return value_->size(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get const pointer to the first byte of the immutable byte array. |
| 65 | */ |
| 66 | const unsigned char* buffer() const |
| 67 | { |
| 68 | return &value_->front (); |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | ptr_lib::shared_ptr<std::vector<unsigned char> > value_; |
| 73 | }; |
| 74 | |
| 75 | } |
| 76 | |
| 77 | #endif |