encoding: Fixing Block (and as a result Name) encoding bugs
As of this commit, all non-const operations on Block will call resetWire
to remove all references to the wire, so it will be recreated next time
"encode" method is called. Also, all getter methods now have only const
versions and non-const access to the internal data structure is
prohibited.
Change-Id: If4b485dd62541d9d4d168a44490068e4deff56c1
diff --git a/src/encoding/block.cpp b/src/encoding/block.cpp
index e6a8b80..9c8ba1e 100644
--- a/src/encoding/block.cpp
+++ b/src/encoding/block.cpp
@@ -183,7 +183,7 @@
}
void
-Block::parse()
+Block::parse() const
{
if (!m_subBlocks.empty() || value_size()==0)
return;
diff --git a/src/encoding/block.hpp b/src/encoding/block.hpp
index 45cf1ad..32523a5 100644
--- a/src/encoding/block.hpp
+++ b/src/encoding/block.hpp
@@ -120,9 +120,24 @@
inline void
reset();
- void
- parse();
+ /**
+ * @brief Reset wire buffer but keep sub elements (if any)
+ */
+ inline void
+ resetWire();
+ /**
+ * @brief Parse wire buffer into subblocks
+ *
+ * This method is not really const, but it does not modify any data. It simply
+ * parses contents of the buffer into subblocks
+ */
+ void
+ parse() const;
+
+ /**
+ * @brief Encode subblocks into wire buffer
+ */
void
encode();
@@ -135,12 +150,6 @@
inline const Block &
get(uint32_t type) const;
- inline Block &
- get(uint32_t type);
-
- inline element_iterator
- find(uint32_t type);
-
inline element_const_iterator
find(uint32_t type) const;
@@ -156,66 +165,48 @@
inline void
push_back(const Block &element);
- /**
- * @brief Get all subelements
- */
- inline const element_container&
- getAll () const;
-
- inline element_container&
- getAll ();
-
- inline const element_container&
- elements () const;
-
- inline element_container&
- elements ();
-
- /**
- * @brief Get all elements of the requested type
- */
- element_container
- getAll(uint32_t type) const;
-
inline Buffer::const_iterator
begin() const;
inline Buffer::const_iterator
end() const;
+ inline const uint8_t*
+ wire() const;
+
inline size_t
size() const;
+ // inline const uint8_t*
+ // buf() const;
+
inline Buffer::const_iterator
value_begin() const;
inline Buffer::const_iterator
value_end() const;
- inline element_iterator
- element_begin();
-
- inline element_iterator
- element_end();
-
- inline element_const_iterator
- element_begin() const;
-
- inline element_const_iterator
- element_end() const;
-
- inline const uint8_t*
- wire() const;
-
- // inline const uint8_t*
- // buf() const;
-
inline const uint8_t*
value() const;
inline size_t
value_size() const;
+ /**
+ * @brief Get all subelements
+ */
+ inline const element_container&
+ elements () const;
+
+ inline element_const_iterator
+ elements_begin() const;
+
+ inline element_const_iterator
+ elements_end() const;
+
+ inline size_t
+ elements_size() const;
+
Block
blockFromValue() const;
@@ -231,7 +222,7 @@
Buffer::const_iterator m_value_begin;
Buffer::const_iterator m_value_end;
- element_container m_subBlocks;
+ mutable element_container m_subBlocks;
};
////////////////////////////////////////////////////////////////////////////////
@@ -267,6 +258,16 @@
m_begin = m_end = m_value_begin = m_value_end = Buffer::const_iterator(); // not really necessary, but for safety
}
+inline void
+Block::resetWire()
+{
+ m_buffer.reset(); // reset of the shared_ptr
+ // keep subblocks
+
+ // keep type
+ m_begin = m_end = m_value_begin = m_value_end = Buffer::const_iterator(); // not really necessary, but for safety
+}
+
inline uint32_t
Block::type() const
{
@@ -288,22 +289,6 @@
throw Error("(Block::get) Requested a non-existed type [" + boost::lexical_cast<std::string>(type) + "] from Block");
}
-
-inline Block &
-Block::get(uint32_t type)
-{
- for (element_iterator i = m_subBlocks.begin ();
- i != m_subBlocks.end();
- i++)
- {
- if (i->type () == type)
- {
- return *i;
- }
- }
-
- throw Error("(Block::get) Requested a non-existed type [" + boost::lexical_cast<std::string>(type) + "] from Block");
-}
inline Block::element_const_iterator
Block::find(uint32_t type) const
@@ -320,24 +305,11 @@
return m_subBlocks.end();
}
-inline Block::element_iterator
-Block::find(uint32_t type)
-{
- for (element_iterator i = m_subBlocks.begin ();
- i != m_subBlocks.end();
- i++)
- {
- if (i->type () == type)
- {
- return i;
- }
- }
- return m_subBlocks.end();
-}
-
inline void
Block::remove(uint32_t type)
{
+ resetWire();
+
element_container newContainer;
newContainer.reserve(m_subBlocks.size());
for (element_iterator i = m_subBlocks.begin();
@@ -348,17 +320,19 @@
newContainer.push_back(*i);
}
m_subBlocks.swap(newContainer);
- }
+}
inline Block::element_iterator
Block::erase(Block::element_iterator position)
{
+ resetWire();
return m_subBlocks.erase(position);
}
inline Block::element_iterator
Block::erase(Block::element_iterator first, Block::element_iterator last)
{
+ resetWire();
return m_subBlocks.erase(first, last);
}
@@ -366,34 +340,10 @@
inline void
Block::push_back(const Block &element)
{
+ resetWire();
m_subBlocks.push_back(element);
}
-
-inline const Block::element_container&
-Block::getAll () const
-{
- return m_subBlocks;
-}
-
-inline Block::element_container&
-Block::getAll ()
-{
- return m_subBlocks;
-}
-
-inline const Block::element_container&
-Block::elements () const
-{
- return m_subBlocks;
-}
-
-inline Block::element_container&
-Block::elements ()
-{
- return m_subBlocks;
-}
-
inline Buffer::const_iterator
Block::begin() const
{
@@ -440,30 +390,6 @@
return m_value_end;
}
-inline Block::element_iterator
-Block::element_begin()
-{
- return m_subBlocks.begin();
-}
-
-inline Block::element_iterator
-Block::element_end()
-{
- return m_subBlocks.end();
-}
-
-inline Block::element_const_iterator
-Block::element_begin() const
-{
- return m_subBlocks.begin();
-}
-
-inline Block::element_const_iterator
-Block::element_end() const
-{
- return m_subBlocks.end();
-}
-
inline const uint8_t*
Block::wire() const
{
@@ -473,15 +399,6 @@
return &*m_begin;
}
-// inline const uint8_t*
-// Block::buf() const
-// {
-// if (!hasWire())
-// throw Error("(Block::wire) Underlying wire buffer is empty");
-
-// return &*m_begin;
-// }
-
inline const uint8_t*
Block::value() const
{
@@ -500,6 +417,31 @@
return m_value_end - m_value_begin;
}
+inline const Block::element_container&
+Block::elements () const
+{
+ return m_subBlocks;
+}
+
+inline Block::element_const_iterator
+Block::elements_begin() const
+{
+ return m_subBlocks.begin();
+}
+
+inline Block::element_const_iterator
+Block::elements_end() const
+{
+ return m_subBlocks.end();
+}
+
+inline size_t
+Block::elements_size() const
+{
+ return m_subBlocks.size();
+}
+
+
} // ndn
#include "block-helpers.hpp"