name: Converting name to use EncodingBuffer
Change-Id: I7dbc7bdd7198085235cbb0b7108cf4c6d5242a62
diff --git a/src/name.hpp b/src/name.hpp
index 18d4252..96a2e9d 100644
--- a/src/name.hpp
+++ b/src/name.hpp
@@ -88,8 +88,12 @@
set(uri.c_str());
}
+ /**
+ * @brief Fast encoding or block size estimation
+ */
+ template<bool T>
size_t
- wireEncode (EncodingBuffer& blk);
+ wireEncode(EncodingImpl<T> &block) const;
const Block &
wireEncode() const;
@@ -167,7 +171,11 @@
Name&
append(const Block &value)
{
- m_nameBlock.elements().push_back(Component(value.begin(), value.end()));
+ if (value.type() == Tlv::NameComponent)
+ m_nameBlock.elements().push_back(value);
+ else
+ m_nameBlock.elements().push_back(Block(Tlv::NameComponent, value));
+
return *this;
}
@@ -483,14 +491,15 @@
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();
+ EncodingEstimator estimator;
+ size_t estimatedSize = wireEncode(estimator);
+
+ EncodingBuffer buffer(estimatedSize, 0);
+ wireEncode(buffer);
+
+ m_nameBlock = buffer.block();
+ m_nameBlock.parse();
+
return m_nameBlock;
}
@@ -501,12 +510,13 @@
m_nameBlock.parse();
}
+template<bool T>
inline size_t
-Name::wireEncode (EncodingBuffer& blk)
+Name::wireEncode(EncodingImpl<T>& blk) const
{
size_t total_len = 0;
- for (reverse_iterator i = rbegin ();
+ for (const_reverse_iterator i = rbegin ();
i != rend ();
++i)
{
@@ -518,6 +528,14 @@
return total_len;
}
+template
+size_t
+Name::wireEncode<true>(EncodingBuffer& block) const;
+
+template
+size_t
+Name::wireEncode<false>(EncodingEstimator& block) const;
+
} // namespace ndn