Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame^] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_BLOB_HPP |
| 8 | #define NDN_BLOB_HPP |
| 9 | |
| 10 | #include "../common.hpp" |
| 11 | |
| 12 | namespace ndn { |
| 13 | |
| 14 | /** |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 15 | * A Blob holds a pointer to an immutable byte array implemented as const std::vector<unsigned char>. |
| 16 | * This is like a JavaScript string which is a pointer to an immutable string. |
| 17 | * 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] | 18 | * 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] | 19 | * the copy constructor and assignment to change the pointer. Also remember that the pointer can be null. |
| 20 | * (Note that we could have made Blob derive directly from vector<unsigned char> and then explicitly use |
| 21 | * a pointer to it like shared_ptr<Blob>, but this does not enforce immutability because we can't declare |
| 22 | * Blob as derived from const vector<unsigned char>.) |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 23 | */ |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 24 | class Blob : public ptr_lib::shared_ptr<const std::vector<unsigned char> > { |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 25 | public: |
| 26 | /** |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 27 | * Create a new Blob with a null pointer. |
| 28 | */ |
| 29 | Blob() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | /** |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 34 | * Create a new Blob with an immutable copy of the given array. |
| 35 | * @param value A pointer to the byte array which is copied. |
| 36 | * @param valueLength The length of value. |
| 37 | */ |
| 38 | Blob(const unsigned char* value, unsigned int valueLength) |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 39 | : 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] | 40 | { |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * 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] | 45 | * If you want to transfer the array without copying, the the vector has to start as a |
| 46 | * 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] | 47 | * @param value A reference to a vector which is copied. |
| 48 | */ |
| 49 | Blob(const std::vector<unsigned char> &value) |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 50 | : 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] | 51 | { |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * 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] | 56 | * 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] | 57 | * @param value A pointer to a vector with the byte array. This takes another reference and does not copy the bytes. |
| 58 | */ |
| 59 | Blob(const ptr_lib::shared_ptr<std::vector<unsigned char> > &value) |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 60 | : ptr_lib::shared_ptr<const std::vector<unsigned char> >(value) |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 61 | { |
| 62 | } |
Jeff Thompson | d0be471 | 2013-09-12 15:20:05 -0700 | [diff] [blame] | 63 | Blob(const ptr_lib::shared_ptr<const std::vector<unsigned char> > &value) |
| 64 | : ptr_lib::shared_ptr<const std::vector<unsigned char> >(value) |
| 65 | { |
| 66 | } |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 69 | * Return the length of the immutable byte array. |
| 70 | */ |
| 71 | unsigned int size() const |
| 72 | { |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 73 | if (*this) |
| 74 | return (*this)->size(); |
| 75 | else |
| 76 | return 0; |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 80 | * 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] | 81 | */ |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 82 | const unsigned char* buf() const |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 83 | { |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 84 | if (*this) |
Jeff Thompson | f6d5a62 | 2013-09-12 12:21:00 -0700 | [diff] [blame] | 85 | return &(*this)->front(); |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 86 | else |
| 87 | return 0; |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 88 | } |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | } |
| 92 | |
| 93 | #endif |