name: Fixes and improvements in Name and name::Component classes
Change-Id: I4695ea252d7cd2de7d68991f5c029bd3f1b39828
diff --git a/src/name-component.hpp b/src/name-component.hpp
index c8ca77e..a74c40c 100644
--- a/src/name-component.hpp
+++ b/src/name-component.hpp
@@ -34,7 +34,17 @@
{
}
- // copy constructor OK
+ /**
+ * @brief Directly create component from wire block
+ *
+ * ATTENTION wire MUST BE of type Tlv::Component. Any other value would cause an exception
+ */
+ Component(const Block& wire)
+ : Block(wire)
+ {
+ if (type() != Tlv::NameComponent)
+ throw Error("Constructing name component from non name component TLV wire block");
+ }
/**
* Create a new Name::Component, taking another pointer to the Blob value.
@@ -69,12 +79,19 @@
: Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(begin, end)))
{
}
-
- Component(const char *string)
- : Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(string, ::strlen(string))))
+
+ explicit
+ Component(const char *str)
+ : Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(str, ::strlen(str))))
{
}
+ explicit
+ Component(const std::string& str)
+ : Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(str.begin(), str.end())))
+ {
+ }
+
/**
* @brief Fast encoding or block size estimation
*/
@@ -142,6 +159,18 @@
toEscapedString(result);
return result.str();
}
+
+ inline void
+ toUri(std::ostream& result) const
+ {
+ return toEscapedString(result);
+ }
+
+ inline std::string
+ toUri() const
+ {
+ return toEscapedString();
+ }
/**
* Interpret this name component as a network-ordered number and return an integer.
diff --git a/src/name.cpp b/src/name.cpp
index 5f4a018..5c267d7 100644
--- a/src/name.cpp
+++ b/src/name.cpp
@@ -187,45 +187,4 @@
return os;
}
-const Block &
-Name::wireEncode() const
-{
- if (m_nameBlock.hasWire())
- return m_nameBlock;
-
- for (Block::element_iterator i = m_nameBlock.element_begin();
- i != m_nameBlock.element_end();
- ++i)
- {
- i->encode();
- }
-
- m_nameBlock.encode();
- return m_nameBlock;
-}
-
-void
-Name::wireDecode(const Block &wire)
-{
- m_nameBlock = wire;
- m_nameBlock.parse();
-}
-
-size_t
-Name::wireEncode (EncodingBuffer& blk)
-{
- size_t total_len = 0;
-
- for (reverse_iterator i = rbegin ();
- i != rend ();
- ++i)
- {
- total_len += i->wireEncode (blk);
- }
-
- total_len += blk.prependVarNumber (total_len);
- total_len += blk.prependVarNumber (Tlv::Name);
- return total_len;
-}
-
} // namespace ndn
diff --git a/src/name.hpp b/src/name.hpp
index 78b3b9d..18d4252 100644
--- a/src/name.hpp
+++ b/src/name.hpp
@@ -54,16 +54,21 @@
: m_nameBlock(Tlv::Name)
{
}
-
- // Name(const Block &name)
- // {
- // for (Block::element_const_iterator i = name.getAll().begin();
- // i != name.getAll().end();
- // ++i)
- // {
- // append(Component(i->value_begin(), i->value_end()));
- // }
- // }
+
+ /**
+ * @brief Create Name object from wire block
+ *
+ * This is a more efficient equivalent for
+ * @code
+ * Name name;
+ * name.wireDecode(wire);
+ * @endcode
+ */
+ Name(const Block &wire)
+ {
+ m_nameBlock = wire;
+ m_nameBlock.parse();
+ }
/**
* Parse the uri according to the NDN URI Scheme and create the name with the components.
@@ -472,6 +477,48 @@
return os.str();
}
+inline const Block &
+Name::wireEncode() const
+{
+ if (m_nameBlock.hasWire())
+ return m_nameBlock;
+
+ for (Block::element_iterator i = m_nameBlock.element_begin();
+ i != m_nameBlock.element_end();
+ ++i)
+ {
+ i->encode();
+ }
+
+ m_nameBlock.encode();
+ return m_nameBlock;
+}
+
+inline void
+Name::wireDecode(const Block &wire)
+{
+ m_nameBlock = wire;
+ m_nameBlock.parse();
+}
+
+inline size_t
+Name::wireEncode (EncodingBuffer& blk)
+{
+ size_t total_len = 0;
+
+ for (reverse_iterator i = rbegin ();
+ i != rend ();
+ ++i)
+ {
+ total_len += i->wireEncode (blk);
+ }
+
+ total_len += blk.prependVarNumber (total_len);
+ total_len += blk.prependVarNumber (Tlv::Name);
+ return total_len;
+}
+
+
} // namespace ndn
#endif