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" |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame^] | 11 | #include "../c/util/blob.h" |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 12 | |
| 13 | namespace ndn { |
| 14 | |
| 15 | /** |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 16 | * A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>. |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 17 | * This is like a JavaScript string which is a pointer to an immutable string. |
| 18 | * 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] | 19 | * 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] | 20 | * the copy constructor and assignment to change the pointer. Also remember that the pointer can be null. |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 21 | * (Note that we could have made Blob derive directly from vector<uint8_t> and then explicitly use |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 22 | * a pointer to it like shared_ptr<Blob>, but this does not enforce immutability because we can't declare |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 23 | * Blob as derived from const vector<uint8_t>.) |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 24 | */ |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 25 | class Blob : public ptr_lib::shared_ptr<const std::vector<uint8_t> > { |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 26 | public: |
| 27 | /** |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 28 | * Create a new Blob with a null pointer. |
| 29 | */ |
| 30 | Blob() |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | /** |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 35 | * Create a new Blob with an immutable copy of the given array. |
| 36 | * @param value A pointer to the byte array which is copied. |
| 37 | * @param valueLength The length of value. |
| 38 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 39 | Blob(const uint8_t* value, size_t valueLength) |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 40 | : ptr_lib::shared_ptr<const std::vector<uint8_t> >(new std::vector<uint8_t>(value, value + valueLength)) |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * 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] | 46 | * If you want to transfer the array without copying, the the vector has to start as a |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 47 | * ptr_lib::shared_ptr<std::vector<uint8_t> > and you can use the Blob constructor with this type. |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 48 | * @param value A reference to a vector which is copied. |
| 49 | */ |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 50 | Blob(const std::vector<uint8_t> &value) |
| 51 | : ptr_lib::shared_ptr<const std::vector<uint8_t> >(new std::vector<uint8_t>(value)) |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
| 55 | /** |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame^] | 56 | * Create a new Blob with an immutable copy of the array in the given Blob struct. |
| 57 | * @param blobStruct The C ndn_Blob struct to receive the pointer. |
| 58 | */ |
| 59 | Blob(const struct ndn_Blob& blobStruct) |
| 60 | : ptr_lib::shared_ptr<const std::vector<uint8_t> >(new std::vector<uint8_t>(blobStruct.value, blobStruct.value + blobStruct.length)) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | /** |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 65 | * 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] | 66 | * 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] | 67 | * @param value A pointer to a vector with the byte array. This takes another reference and does not copy the bytes. |
| 68 | */ |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 69 | Blob(const ptr_lib::shared_ptr<std::vector<uint8_t> > &value) |
| 70 | : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value) |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 71 | { |
| 72 | } |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 73 | Blob(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value) |
| 74 | : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value) |
Jeff Thompson | d0be471 | 2013-09-12 15:20:05 -0700 | [diff] [blame] | 75 | { |
| 76 | } |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 79 | * Return the length of the immutable byte array. |
| 80 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 81 | size_t |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 82 | size() 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) |
| 85 | return (*this)->size(); |
| 86 | else |
| 87 | return 0; |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 91 | * 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] | 92 | */ |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 93 | const uint8_t* |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 94 | buf() const |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 95 | { |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 96 | if (*this) |
Jeff Thompson | f6d5a62 | 2013-09-12 12:21:00 -0700 | [diff] [blame] | 97 | return &(*this)->front(); |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 98 | else |
| 99 | return 0; |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 100 | } |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame^] | 101 | |
| 102 | /** |
| 103 | * Set the blobStruct to point to this Blob's byte array, without copying any memory. |
| 104 | * WARNING: The resulting pointer in blobStruct is invalid after a further use of this object which could reallocate memory. |
| 105 | * @param blobStruct The C ndn_Blob struct to receive the pointer. |
| 106 | */ |
| 107 | void |
| 108 | get(struct ndn_Blob& blobStruct) const |
| 109 | { |
| 110 | blobStruct.length = size(); |
| 111 | if (size() > 0) |
| 112 | blobStruct.value = (uint8_t*)buf(); |
| 113 | else |
| 114 | blobStruct.value = 0; |
| 115 | } |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } |
| 119 | |
| 120 | #endif |