blob: d71808d9fb1f3bd3d7005f2c2bfd864f8ff22f49 [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"
11
12namespace ndn {
13
14/**
Jeff Thompson10ad12a2013-09-24 16:19:11 -070015 * A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>.
Jeff Thompson0e6d1422013-09-12 11:36:37 -070016 * This is like a JavaScript string which is a pointer to an immutable string.
17 * 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 -070018 * 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 -070019 * the copy constructor and assignment to change the pointer. Also remember that the pointer can be null.
Jeff Thompson10ad12a2013-09-24 16:19:11 -070020 * (Note that we could have made Blob derive directly from vector<uint8_t> and then explicitly use
Jeff Thompson0e6d1422013-09-12 11:36:37 -070021 * 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 -070022 * Blob as derived from const vector<uint8_t>.)
Jeff Thompson25e89382013-09-11 15:35:19 -070023 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070024class Blob : public ptr_lib::shared_ptr<const std::vector<uint8_t> > {
Jeff Thompson25e89382013-09-11 15:35:19 -070025public:
26 /**
Jeff Thompson0e6d1422013-09-12 11:36:37 -070027 * Create a new Blob with a null pointer.
28 */
29 Blob()
30 {
31 }
32
33 /**
Jeff Thompson25e89382013-09-11 15:35:19 -070034 * Create a new Blob with an immutable copy of the given array.
35 * @param value A pointer to the byte array which is copied.
36 * @param valueLength The length of value.
37 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070038 Blob(const uint8_t* value, unsigned int valueLength)
39 : ptr_lib::shared_ptr<const std::vector<uint8_t> >(new std::vector<uint8_t>(value, value + valueLength))
Jeff Thompson25e89382013-09-11 15:35:19 -070040 {
41 }
42
43 /**
44 * Create a new Blob with an immutable copy of the array in the given vector.
Jeff Thompson675d71a2013-09-12 10:57:37 -070045 * If you want to transfer the array without copying, the the vector has to start as a
Jeff Thompson10ad12a2013-09-24 16:19:11 -070046 * 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 -070047 * @param value A reference to a vector which is copied.
48 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070049 Blob(const std::vector<uint8_t> &value)
50 : ptr_lib::shared_ptr<const std::vector<uint8_t> >(new std::vector<uint8_t>(value))
Jeff Thompson25e89382013-09-11 15:35:19 -070051 {
52 }
53
54 /**
55 * Create a new Blob to point to an existing byte array. IMPORTANT: After calling this constructor,
Jeff Thompson0e6d1422013-09-12 11:36:37 -070056 * 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 -070057 * @param value A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
58 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070059 Blob(const ptr_lib::shared_ptr<std::vector<uint8_t> > &value)
60 : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value)
Jeff Thompson25e89382013-09-11 15:35:19 -070061 {
62 }
Jeff Thompson10ad12a2013-09-24 16:19:11 -070063 Blob(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value)
64 : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value)
Jeff Thompsond0be4712013-09-12 15:20:05 -070065 {
66 }
Jeff Thompson25e89382013-09-11 15:35:19 -070067
68 /**
Jeff Thompson25e89382013-09-11 15:35:19 -070069 * Return the length of the immutable byte array.
70 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070071 unsigned int
72 size() const
Jeff Thompson25e89382013-09-11 15:35:19 -070073 {
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070074 if (*this)
75 return (*this)->size();
76 else
77 return 0;
Jeff Thompson25e89382013-09-11 15:35:19 -070078 }
79
80 /**
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070081 * 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 -070082 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070083 const uint8_t*
Jeff Thompson0050abe2013-09-17 12:50:25 -070084 buf() const
Jeff Thompson25e89382013-09-11 15:35:19 -070085 {
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070086 if (*this)
Jeff Thompsonf6d5a622013-09-12 12:21:00 -070087 return &(*this)->front();
Jeff Thompsone5ddbed2013-09-12 12:19:31 -070088 else
89 return 0;
Jeff Thompson25e89382013-09-11 15:35:19 -070090 }
Jeff Thompson25e89382013-09-11 15:35:19 -070091};
92
93}
94
95#endif