blob: af1651dc18455e98d2ee13025cb2997d3734cf08 [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 }
62
63 /**
Jeff Thompson25e89382013-09-11 15:35:19 -070064 * Return the length of the immutable byte array.
65 */
66 unsigned int size() const
67 {
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070068 if (*this)
69 return (*this)->size();
70 else
71 return 0;
Jeff Thompson25e89382013-09-11 15:35:19 -070072 }
73
74 /**
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070075 * 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 -070076 */
Jeff Thompson0e6d1422013-09-12 11:36:37 -070077 const unsigned char* buf() const
Jeff Thompson25e89382013-09-11 15:35:19 -070078 {
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070079 if (*this)
80 return &(*this)->front ();
81 else
82 return 0;
Jeff Thompson25e89382013-09-11 15:35:19 -070083 }
Jeff Thompson25e89382013-09-11 15:35:19 -070084};
85
86}
87
88#endif