encoding: Block::insert
This commit also updates Block::erase to accept element_const_iterator
refs #2998
Change-Id: Ie09c99d14a065444b01abff72fd97a92387b9b91
diff --git a/src/encoding/block.cpp b/src/encoding/block.cpp
index 4ec1d6b..693bc17 100644
--- a/src/encoding/block.cpp
+++ b/src/encoding/block.cpp
@@ -512,17 +512,33 @@
}
Block::element_iterator
-Block::erase(Block::element_iterator position)
+Block::erase(Block::element_const_iterator position)
{
resetWire();
+
+#ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR
return m_subBlocks.erase(position);
+#else
+ element_iterator it = m_subBlocks.begin();
+ std::advance(it, std::distance(m_subBlocks.cbegin(), position));
+ return m_subBlocks.erase(it);
+#endif
}
Block::element_iterator
-Block::erase(Block::element_iterator first, Block::element_iterator last)
+Block::erase(Block::element_const_iterator first, Block::element_const_iterator last)
{
resetWire();
+
+#ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR
return m_subBlocks.erase(first, last);
+#else
+ element_iterator itStart = m_subBlocks.begin();
+ element_iterator itEnd = m_subBlocks.begin();
+ std::advance(itStart, std::distance(m_subBlocks.cbegin(), first));
+ std::advance(itEnd, std::distance(m_subBlocks.cbegin(), last));
+ return m_subBlocks.erase(itStart, itEnd);
+#endif
}
void
@@ -532,6 +548,20 @@
m_subBlocks.push_back(element);
}
+Block::element_iterator
+Block::insert(Block::element_const_iterator pos, const Block& element)
+{
+ resetWire();
+
+#ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR
+ return m_subBlocks.insert(pos, element);
+#else
+ element_iterator it = m_subBlocks.begin();
+ std::advance(it, std::distance(m_subBlocks.cbegin(), pos));
+ return m_subBlocks.insert(it, element);
+#endif
+}
+
Block::element_const_iterator
Block::elements_begin() const
{
diff --git a/src/encoding/block.hpp b/src/encoding/block.hpp
index 7c45543..9407ccf 100644
--- a/src/encoding/block.hpp
+++ b/src/encoding/block.hpp
@@ -253,14 +253,23 @@
remove(uint32_t type);
element_iterator
- erase(element_iterator position);
+ erase(element_const_iterator position);
element_iterator
- erase(element_iterator first, element_iterator last);
+ erase(element_const_iterator first, element_const_iterator last);
void
push_back(const Block& element);
+ /**
+ * @brief insert Insert a new element in a specific position
+ * @param pos Position to insert the new element
+ * @param element Element to be inserted
+ * @return An iterator that points to the first of the newly inserted elements.
+ */
+ element_iterator
+ insert(element_const_iterator pos, const Block& element);
+
/** @brief Get all subelements
*/
const element_container&