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 | } |
| 62 | |
| 63 | /** |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 64 | * Return the length of the immutable byte array. |
| 65 | */ |
| 66 | unsigned int size() const |
| 67 | { |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame^] | 68 | return (*this)->size(); |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get const pointer to the first byte of the immutable byte array. |
| 73 | */ |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame^] | 74 | const unsigned char* buf() const |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 75 | { |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame^] | 76 | return &(*this)->front (); |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 77 | } |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | } |
| 81 | |
| 82 | #endif |