Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_BLOB_HPP |
| 9 | #define NDN_BLOB_HPP |
| 10 | |
| 11 | #include "../common.hpp" |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 12 | |
| 13 | struct ndn_Blob; |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | /** |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 18 | * 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] | 19 | * This is like a JavaScript string which is a pointer to an immutable string. |
| 20 | * 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] | 21 | * 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] | 22 | * 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] | 23 | * (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] | 24 | * 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] | 25 | * Blob as derived from const vector<uint8_t>.) |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 26 | */ |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 27 | class Blob : public ptr_lib::shared_ptr<const std::vector<uint8_t> > { |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 28 | public: |
| 29 | /** |
Jeff Thompson | 0e6d142 | 2013-09-12 11:36:37 -0700 | [diff] [blame] | 30 | * Create a new Blob with a null pointer. |
| 31 | */ |
| 32 | Blob() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | /** |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 37 | * Create a new Blob with an immutable copy of the given array. |
| 38 | * @param value A pointer to the byte array which is copied. |
| 39 | * @param valueLength The length of value. |
| 40 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 41 | Blob(const uint8_t* value, size_t valueLength) |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 42 | : 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] | 43 | { |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * 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] | 48 | * 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] | 49 | * 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] | 50 | * @param value A reference to a vector which is copied. |
| 51 | */ |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 52 | Blob(const std::vector<uint8_t> &value) |
| 53 | : 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] | 54 | { |
| 55 | } |
| 56 | |
| 57 | /** |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 58 | * Create a new Blob with an immutable copy of the array in the given Blob struct. |
| 59 | * @param blobStruct The C ndn_Blob struct to receive the pointer. |
| 60 | */ |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 61 | Blob(const struct ndn_Blob& blobStruct); |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 64 | * 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] | 65 | * 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] | 66 | * @param value A pointer to a vector with the byte array. This takes another reference and does not copy the bytes. |
| 67 | */ |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 68 | Blob(const ptr_lib::shared_ptr<std::vector<uint8_t> > &value) |
| 69 | : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value) |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 70 | { |
| 71 | } |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 72 | Blob(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value) |
| 73 | : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value) |
Jeff Thompson | d0be471 | 2013-09-12 15:20:05 -0700 | [diff] [blame] | 74 | { |
| 75 | } |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 78 | * Return the length of the immutable byte array. |
| 79 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 80 | size_t |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 81 | size() 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) |
| 84 | return (*this)->size(); |
| 85 | else |
| 86 | return 0; |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 90 | * 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] | 91 | */ |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 92 | const uint8_t* |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 93 | buf() const |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 94 | { |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 95 | if (*this) |
Jeff Thompson | f6d5a62 | 2013-09-12 12:21:00 -0700 | [diff] [blame] | 96 | return &(*this)->front(); |
Jeff Thompson | e5ddbed | 2013-09-12 12:19:31 -0700 | [diff] [blame] | 97 | else |
| 98 | return 0; |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 99 | } |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Set the blobStruct to point to this Blob's byte array, without copying any memory. |
| 103 | * WARNING: The resulting pointer in blobStruct is invalid after a further use of this object which could reallocate memory. |
| 104 | * @param blobStruct The C ndn_Blob struct to receive the pointer. |
| 105 | */ |
| 106 | void |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 107 | get(struct ndn_Blob& blobStruct) const; |
Jeff Thompson | 25e8938 | 2013-09-11 15:35:19 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } |
| 111 | |
| 112 | #endif |