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 | /** |
Jeff Thompson | 675d71a | 2013-09-12 10:57:37 -0700 | [diff] [blame^] | 14 | * A Blob holds a pointer to an immutable byte array. This is like a JavaScript string which is a pointer |
| 15 | * to an immutable string. It is OK to pass a pointer to the string because the new owner can't change the bytes |
| 16 | * of the string. However, like a JavaScript string, it is possible to change the pointer, and so this does allow |
| 17 | * the copy constructor and assignment to change the pointer. |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 18 | */ |
| 19 | class Blob { |
| 20 | public: |
| 21 | /** |
| 22 | * Create a new Blob with an immutable copy of the given array. |
| 23 | * @param value A pointer to the byte array which is copied. |
| 24 | * @param valueLength The length of value. |
| 25 | */ |
| 26 | Blob(const unsigned char* value, unsigned int valueLength) |
| 27 | : value_(new std::vector<unsigned char>(value, value + valueLength)) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Create a new Blob with an immutable copy of the array in the given vector. |
Jeff Thompson | 675d71a | 2013-09-12 10:57:37 -0700 | [diff] [blame^] | 33 | * If you want to transfer the array without copying, the the vector has to start as a |
| 34 | * ptr_lib::shared_ptr<std::vector<unsigned char> > and you can use the Blob constructor with this type. |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 35 | * @param value A reference to a vector which is copied. |
| 36 | */ |
| 37 | Blob(const std::vector<unsigned char> &value) |
| 38 | : value_(new std::vector<unsigned char>(value)) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Create a new Blob to point to an existing byte array. IMPORTANT: After calling this constructor, |
| 44 | * you must treat the array as immutable and promise not to change it. |
| 45 | * @param value A pointer to a vector with the byte array. This takes another reference and does not copy the bytes. |
| 46 | */ |
| 47 | Blob(const ptr_lib::shared_ptr<std::vector<unsigned char> > &value) |
| 48 | : value_(value) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Return a const reference to the immutable byte array. |
| 54 | */ |
| 55 | const std::vector<unsigned char>& getValue() const |
| 56 | { |
| 57 | return *value_; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Return the length of the immutable byte array. |
| 62 | */ |
| 63 | unsigned int size() const |
| 64 | { |
| 65 | return value_->size(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get const pointer to the first byte of the immutable byte array. |
| 70 | */ |
| 71 | const unsigned char* buffer() const |
| 72 | { |
| 73 | return &value_->front (); |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | ptr_lib::shared_ptr<std::vector<unsigned char> > value_; |
| 78 | }; |
| 79 | |
| 80 | } |
| 81 | |
| 82 | #endif |