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 | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 14 | * A Blob holds a pointer to an immutable byte array implemented as const std::vector<unsigned char>. |
| 15 | * This is like a JavaScript string which is a pointer to an immutable string. |
| 16 | * It is OK to pass a pointer to the string because the new owner can't change the bytes |
Jeff Thompson | 675d71a | 2013-09-12 10:57:37 -0700 | [diff] [blame] | 17 | * of the string. However, like a JavaScript string, it is possible to change the pointer, and so this does allow |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 18 | * the copy constructor and assignment to change the pointer. Also remember that the pointer can be null. |
| 19 | * (Note that we could have made Blob derive directly from vector<unsigned char> and then explicitly use |
| 20 | * a pointer to it like shared_ptr<Blob>, but this does not enforce immutability because we can't declare |
| 21 | * Blob as derived from const vector<unsigned char>.) |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 22 | */ |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 23 | class Blob : public ptr_lib::shared_ptr<const std::vector<unsigned char> > { |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 24 | public: |
| 25 | /** |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 26 | * Create a new Blob with a null pointer. |
| 27 | */ |
| 28 | Blob() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | /** |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 33 | * Create a new Blob with an immutable copy of the given array. |
| 34 | * @param value A pointer to the byte array which is copied. |
| 35 | * @param valueLength The length of value. |
| 36 | */ |
| 37 | Blob(const unsigned char* value, unsigned int valueLength) |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 38 | : ptr_lib::shared_ptr<const std::vector<unsigned char> >(new std::vector<unsigned char>(value, value + valueLength)) |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * 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] | 44 | * If you want to transfer the array without copying, the the vector has to start as a |
| 45 | * 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] | 46 | * @param value A reference to a vector which is copied. |
| 47 | */ |
| 48 | Blob(const std::vector<unsigned char> &value) |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 49 | : ptr_lib::shared_ptr<const std::vector<unsigned char> >(new std::vector<unsigned char>(value)) |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 50 | { |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Create a new Blob to point to an existing byte array. IMPORTANT: After calling this constructor, |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 55 | * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it. |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 56 | * @param value A pointer to a vector with the byte array. This takes another reference and does not copy the bytes. |
| 57 | */ |
| 58 | Blob(const ptr_lib::shared_ptr<std::vector<unsigned char> > &value) |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 59 | : ptr_lib::shared_ptr<const std::vector<unsigned char> >(value) |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 60 | { |
| 61 | } |
Jeff Thompson | d0be471 | 2013-09-12 15:20:05 -0700 | [diff] [blame] | 62 | Blob(const ptr_lib::shared_ptr<const std::vector<unsigned char> > &value) |
| 63 | : ptr_lib::shared_ptr<const std::vector<unsigned char> >(value) |
| 64 | { |
| 65 | } |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 68 | * Return the length of the immutable byte array. |
| 69 | */ |
| 70 | unsigned int size() const |
| 71 | { |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 72 | if (*this) |
| 73 | return (*this)->size(); |
| 74 | else |
| 75 | return 0; |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 79 | * Return a const pointer to the first byte of the immutable byte array, or 0 if the pointer is null. |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 80 | */ |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 81 | const unsigned char* buf() const |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 82 | { |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 83 | if (*this) |
Jeff Thompson | f6d5a62 | 2013-09-12 12:21:00 -0700 | [diff] [blame] | 84 | return &(*this)->front(); |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 85 | else |
| 86 | return 0; |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 87 | } |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | } |
| 91 | |
| 92 | #endif |