blob: 2c238a652dff58d50d0cc8161b143dc7702bffdc [file] [log] [blame]
Jeff Thompson25e89382013-09-11 15:35:19 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson25e89382013-09-11 15:35:19 -07004 * 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 Thompson93034532013-10-08 11:52:43 -070011#include "../c/util/blob.h"
Jeff Thompson25e89382013-09-11 15:35:19 -070012
13namespace ndn {
14
15/**
Jeff Thompson10ad12a2013-09-24 16:19:11 -070016 * A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>.
Jeff Thompson0e6d1422013-09-12 11:36:37 -070017 * 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 Thompson675d71a2013-09-12 10:57:37 -070019 * 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 -070020 * the copy constructor and assignment to change the pointer. Also remember that the pointer can be null.
Jeff Thompson10ad12a2013-09-24 16:19:11 -070021 * (Note that we could have made Blob derive directly from vector<uint8_t> and then explicitly use
Jeff Thompson0e6d1422013-09-12 11:36:37 -070022 * a pointer to it like shared_ptr<Blob>, but this does not enforce immutability because we can't declare
Jeff Thompson10ad12a2013-09-24 16:19:11 -070023 * Blob as derived from const vector<uint8_t>.)
Jeff Thompson25e89382013-09-11 15:35:19 -070024 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070025class Blob : public ptr_lib::shared_ptr<const std::vector<uint8_t> > {
Jeff Thompson25e89382013-09-11 15:35:19 -070026public:
27 /**
Jeff Thompson0e6d1422013-09-12 11:36:37 -070028 * Create a new Blob with a null pointer.
29 */
30 Blob()
31 {
32 }
33
34 /**
Jeff Thompson25e89382013-09-11 15:35:19 -070035 * 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 Thompson97223af2013-09-24 17:01:27 -070039 Blob(const uint8_t* value, size_t valueLength)
Jeff Thompson10ad12a2013-09-24 16:19:11 -070040 : ptr_lib::shared_ptr<const std::vector<uint8_t> >(new std::vector<uint8_t>(value, value + valueLength))
Jeff Thompson25e89382013-09-11 15:35:19 -070041 {
42 }
43
44 /**
45 * Create a new Blob with an immutable copy of the array in the given vector.
Jeff Thompson675d71a2013-09-12 10:57:37 -070046 * If you want to transfer the array without copying, the the vector has to start as a
Jeff Thompson10ad12a2013-09-24 16:19:11 -070047 * ptr_lib::shared_ptr<std::vector<uint8_t> > and you can use the Blob constructor with this type.
Jeff Thompson25e89382013-09-11 15:35:19 -070048 * @param value A reference to a vector which is copied.
49 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070050 Blob(const std::vector<uint8_t> &value)
51 : ptr_lib::shared_ptr<const std::vector<uint8_t> >(new std::vector<uint8_t>(value))
Jeff Thompson25e89382013-09-11 15:35:19 -070052 {
53 }
54
55 /**
Jeff Thompson93034532013-10-08 11:52:43 -070056 * 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 Thompson25e89382013-09-11 15:35:19 -070065 * Create a new Blob to point to an existing byte array. IMPORTANT: After calling this constructor,
Jeff Thompson0e6d1422013-09-12 11:36:37 -070066 * 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 -070067 * @param value A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
68 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070069 Blob(const ptr_lib::shared_ptr<std::vector<uint8_t> > &value)
70 : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value)
Jeff Thompson25e89382013-09-11 15:35:19 -070071 {
72 }
Jeff Thompson10ad12a2013-09-24 16:19:11 -070073 Blob(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value)
74 : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value)
Jeff Thompsond0be4712013-09-12 15:20:05 -070075 {
76 }
Jeff Thompson25e89382013-09-11 15:35:19 -070077
78 /**
Jeff Thompson25e89382013-09-11 15:35:19 -070079 * Return the length of the immutable byte array.
80 */
Jeff Thompson97223af2013-09-24 17:01:27 -070081 size_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070082 size() const
Jeff Thompson25e89382013-09-11 15:35:19 -070083 {
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070084 if (*this)
85 return (*this)->size();
86 else
87 return 0;
Jeff Thompson25e89382013-09-11 15:35:19 -070088 }
89
90 /**
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070091 * 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 -070092 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070093 const uint8_t*
Jeff Thompson0050abe2013-09-17 12:50:25 -070094 buf() const
Jeff Thompson25e89382013-09-11 15:35:19 -070095 {
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070096 if (*this)
Jeff Thompsonf6d5a622013-09-12 12:21:00 -070097 return &(*this)->front();
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070098 else
99 return 0;
Jeff Thompson25e89382013-09-11 15:35:19 -0700100 }
Jeff Thompson93034532013-10-08 11:52:43 -0700101
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 Thompson25e89382013-09-11 15:35:19 -0700116};
117
118}
119
120#endif