blob: f205c8c3d0e9a83582b330a697043e590dadd767 [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 Thompson0e6d1422013-09-12 11:36:37 -070068 return (*this)->size();
Jeff Thompson25e89382013-09-11 15:35:19 -070069 }
70
71 /**
72 * Get const pointer to the first byte of the immutable byte array.
73 */
Jeff Thompson0e6d1422013-09-12 11:36:37 -070074 const unsigned char* buf() const
Jeff Thompson25e89382013-09-11 15:35:19 -070075 {
Jeff Thompson0e6d1422013-09-12 11:36:37 -070076 return &(*this)->front ();
Jeff Thompson25e89382013-09-11 15:35:19 -070077 }
Jeff Thompson25e89382013-09-11 15:35:19 -070078};
79
80}
81
82#endif