Make Blob inherit from shared_ptr<const vector<unsigned char> >
diff --git a/ndn-cpp/util/blob.hpp b/ndn-cpp/util/blob.hpp
index bdec7d6..f205c8c 100644
--- a/ndn-cpp/util/blob.hpp
+++ b/ndn-cpp/util/blob.hpp
@@ -11,20 +11,31 @@
namespace ndn {
/**
- * A Blob holds a pointer to an immutable byte array. This is like a JavaScript string which is a pointer
- * to an immutable string. It is OK to pass a pointer to the string because the new owner can't change the bytes
+ * A Blob holds a pointer to an immutable byte array implemented as const std::vector<unsigned char>.
+ * This is like a JavaScript string which is a pointer to an immutable string.
+ * It is OK to pass a pointer to the string because the new owner can't change the bytes
* of the string. However, like a JavaScript string, it is possible to change the pointer, and so this does allow
- * the copy constructor and assignment to change the pointer.
+ * the copy constructor and assignment to change the pointer. Also remember that the pointer can be null.
+ * (Note that we could have made Blob derive directly from vector<unsigned char> and then explicitly use
+ * a pointer to it like shared_ptr<Blob>, but this does not enforce immutability because we can't declare
+ * Blob as derived from const vector<unsigned char>.)
*/
-class Blob {
+class Blob : public ptr_lib::shared_ptr<const std::vector<unsigned char> > {
public:
/**
+ * Create a new Blob with a null pointer.
+ */
+ Blob()
+ {
+ }
+
+ /**
* Create a new Blob with an immutable copy of the given array.
* @param value A pointer to the byte array which is copied.
* @param valueLength The length of value.
*/
Blob(const unsigned char* value, unsigned int valueLength)
- : value_(new std::vector<unsigned char>(value, value + valueLength))
+ : ptr_lib::shared_ptr<const std::vector<unsigned char> >(new std::vector<unsigned char>(value, value + valueLength))
{
}
@@ -35,46 +46,35 @@
* @param value A reference to a vector which is copied.
*/
Blob(const std::vector<unsigned char> &value)
- : value_(new std::vector<unsigned char>(value))
+ : ptr_lib::shared_ptr<const std::vector<unsigned char> >(new std::vector<unsigned char>(value))
{
}
/**
* Create a new Blob to point to an existing byte array. IMPORTANT: After calling this constructor,
- * you must treat the array as immutable and promise not to change it.
+ * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
* @param value A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
*/
Blob(const ptr_lib::shared_ptr<std::vector<unsigned char> > &value)
- : value_(value)
+ : ptr_lib::shared_ptr<const std::vector<unsigned char> >(value)
{
}
/**
- * Return a const reference to the immutable byte array.
- */
- const std::vector<unsigned char>& getValue() const
- {
- return *value_;
- }
-
- /**
* Return the length of the immutable byte array.
*/
unsigned int size() const
{
- return value_->size();
+ return (*this)->size();
}
/**
* Get const pointer to the first byte of the immutable byte array.
*/
- const unsigned char* buffer() const
+ const unsigned char* buf() const
{
- return &value_->front ();
+ return &(*this)->front ();
}
-
-private:
- ptr_lib::shared_ptr<std::vector<unsigned char> > value_;
};
}