Change Component setFromEscapedString and setSegment to static makeFromEscapedString and makeSegment, and make the class read-only.
diff --git a/ndn-cpp/name.cpp b/ndn-cpp/name.cpp
index d94eecd..2b5c6a9 100644
--- a/ndn-cpp/name.cpp
+++ b/ndn-cpp/name.cpp
@@ -103,7 +103,7 @@
return result.str();
}
-bool Name::Component::setFromEscapedString(const char *escapedString, unsigned int beginOffset, unsigned int endOffset)
+Blob Name::Component::makeFromEscapedString(const char *escapedString, unsigned int beginOffset, unsigned int endOffset)
{
string trimmedString(escapedString + beginOffset, escapedString + endOffset);
trim(trimmedString);
@@ -113,18 +113,16 @@
// Special case for component of only periods.
if (component.size() <= 2)
// Zero, one or two periods is illegal. Ignore this component.
- return false;
+ return Blob();
else
// Remove 3 periods.
- value_ = Blob((const unsigned char *)&component[3], component.size() - 3);
+ return Blob((const unsigned char *)&component[3], component.size() - 3);
}
else
- value_ = Blob((const unsigned char *)&component[0], component.size());
-
- return true;
+ return Blob((const unsigned char *)&component[0], component.size());
}
-void Name::Component::setSegment(unsigned long segment)
+Blob Name::Component::makeSegment(unsigned long segment)
{
ptr_lib::shared_ptr<vector<unsigned char> > value;
@@ -139,7 +137,7 @@
// Make it big endian.
reverse(value->begin() + 1, value->end());
- value_ = value;
+ return Blob(value);
}
void Name::set(const char *uri_cstr)
@@ -189,10 +187,10 @@
if (iComponentEnd == string::npos)
iComponentEnd = uri.size();
- components_.push_back(Component());
- if (!components_[components_.size() - 1].setFromEscapedString(&uri[0], iComponentStart, iComponentEnd))
- // Ignore the illegal component. This also gets rid of a trailing '/'.
- components_.pop_back();
+ Blob component = Component::makeFromEscapedString(&uri[0], iComponentStart, iComponentEnd);
+ // Ignore illegal components. This also gets rid of a trailing '/'.
+ if (component)
+ components_.push_back(Component(component));
iComponentStart = iComponentEnd + 1;
}
diff --git a/ndn-cpp/name.hpp b/ndn-cpp/name.hpp
index 587301c..cd80fac 100644
--- a/ndn-cpp/name.hpp
+++ b/ndn-cpp/name.hpp
@@ -19,12 +19,12 @@
class Name {
public:
/**
- * A Name::Component is holds an immutable name component value.
+ * A Name::Component is holds a read-only name component value.
*/
class Component {
public:
/**
- * Create a new Name::Component with an empty value.
+ * Create a new Name::Component with a null value.
*/
Component()
{
@@ -74,24 +74,23 @@
const Blob& getValue() const { return value_; }
- void setValue(const Blob& value) { value_ = value; }
-
/**
- * Set this component value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme.
- * If the escaped string is "", "." or ".." then return false, which means this component value was not changed, and
+ * Make a component value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme.
+ * If the escaped string is "", "." or ".." then return a Blob with a null pointer, which means this component value was not changed, and
* the component should be skipped in a URI name.
* @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset.
* @param beginOffset The offset in escapedString of the beginning of the portion to decode.
* @param endOffset The offset in escapedString of the end of the portion to decode.
- * @return True for success, false if escapedString is not a valid escaped component.
+ * @return The component value as a Blob, or a Blob with a null pointer if escapedString is not a valid escaped component.
*/
- bool setFromEscapedString(const char *escapedString, unsigned int beginOffset, unsigned int endOffset);
+ static Blob makeFromEscapedString(const char *escapedString, unsigned int beginOffset, unsigned int endOffset);
/**
- * Set this component to the encoded segment number.
+ * Make a component as the encoded segment number.
* @param segment The segment number.
+ * @return The component value as a Blob.
*/
- void setSegment(unsigned long segment);
+ static Blob makeSegment(unsigned long segment);
private:
Blob value_;
@@ -198,8 +197,7 @@
*/
void appendSegment(unsigned long segment)
{
- components_.push_back(Component());
- components_.back().setSegment(segment);
+ components_.push_back(Component(Component::makeSegment(segment)));
}
/**