blob: 12c19dcd83809c93e248a6539ca13b5e2512a125 [file] [log] [blame]
Jeff Thompson25e89382013-09-11 15:35:19 -07001/**
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
11namespace ndn {
12
13/**
Jeff Thompson0e6d1422013-09-12 11:36:37 -070014 * 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 Thompson675d71a2013-09-12 10:57:37 -070017 * of the string. However, like a JavaScript string, it is possible to change the pointer, and so this does allow
Jeff Thompson0e6d1422013-09-12 11:36:37 -070018 * 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 Thompson25e89382013-09-11 15:35:19 -070022 */
Jeff Thompson0e6d1422013-09-12 11:36:37 -070023class Blob : public ptr_lib::shared_ptr<const std::vector<unsigned char> > {
Jeff Thompson25e89382013-09-11 15:35:19 -070024public:
25 /**
Jeff Thompson0e6d1422013-09-12 11:36:37 -070026 * Create a new Blob with a null pointer.
27 */
28 Blob()
29 {
30 }
31
32 /**
Jeff Thompson25e89382013-09-11 15:35:19 -070033 * 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 Thompson0e6d1422013-09-12 11:36:37 -070038 : ptr_lib::shared_ptr<const std::vector<unsigned char> >(new std::vector<unsigned char>(value, value + valueLength))
Jeff Thompson25e89382013-09-11 15:35:19 -070039 {
40 }
41
42 /**
43 * Create a new Blob with an immutable copy of the array in the given vector.
Jeff Thompson675d71a2013-09-12 10:57:37 -070044 * 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 Thompson25e89382013-09-11 15:35:19 -070046 * @param value A reference to a vector which is copied.
47 */
48 Blob(const std::vector<unsigned char> &value)
Jeff Thompson0e6d1422013-09-12 11:36:37 -070049 : ptr_lib::shared_ptr<const std::vector<unsigned char> >(new std::vector<unsigned char>(value))
Jeff Thompson25e89382013-09-11 15:35:19 -070050 {
51 }
52
53 /**
54 * Create a new Blob to point to an existing byte array. IMPORTANT: After calling this constructor,
Jeff Thompson0e6d1422013-09-12 11:36:37 -070055 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
Jeff Thompson25e89382013-09-11 15:35:19 -070056 * @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 Thompson0e6d1422013-09-12 11:36:37 -070059 : ptr_lib::shared_ptr<const std::vector<unsigned char> >(value)
Jeff Thompson25e89382013-09-11 15:35:19 -070060 {
61 }
Jeff Thompsond0be4712013-09-12 15:20:05 -070062 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 Thompson25e89382013-09-11 15:35:19 -070066
67 /**
Jeff Thompson25e89382013-09-11 15:35:19 -070068 * Return the length of the immutable byte array.
69 */
70 unsigned int size() const
71 {
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070072 if (*this)
73 return (*this)->size();
74 else
75 return 0;
Jeff Thompson25e89382013-09-11 15:35:19 -070076 }
77
78 /**
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070079 * Return a const pointer to the first byte of the immutable byte array, or 0 if the pointer is null.
Jeff Thompson25e89382013-09-11 15:35:19 -070080 */
Jeff Thompson0e6d1422013-09-12 11:36:37 -070081 const unsigned char* buf() const
Jeff Thompson25e89382013-09-11 15:35:19 -070082 {
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070083 if (*this)
Jeff Thompsonf6d5a622013-09-12 12:21:00 -070084 return &(*this)->front();
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070085 else
86 return 0;
Jeff Thompson25e89382013-09-11 15:35:19 -070087 }
Jeff Thompson25e89382013-09-11 15:35:19 -070088};
89
90}
91
92#endif