encoding: Refactoring EncodingBuffer

Breaks: nfd:commit:c0273e3505ac2ccf843401be77a513d8eb663127
Breaks: ChronoSync:commit:e042f83a1df184a8e7a90ef00034d11026891cd1

Change-Id: I8275c6276c5ecfa280f87f584189907521febf5f
Refs: #2494, #2490
diff --git a/src/data.cpp b/src/data.cpp
index 8615c52..fc3dfa7 100644
--- a/src/data.cpp
+++ b/src/data.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -47,9 +47,9 @@
   wireDecode(wire);
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-Data::wireEncode(EncodingImpl<T>& block, bool unsignedPortion/* = false*/) const
+Data::wireEncode(EncodingImpl<TAG>& encoder, bool unsignedPortion/* = false*/) const
 {
   size_t totalLength = 0;
 
@@ -69,35 +69,37 @@
   if (!unsignedPortion)
     {
       // SignatureValue
-      totalLength += prependBlock(block, m_signature.getValue());
+      totalLength += encoder.prependBlock(m_signature.getValue());
     }
 
   // SignatureInfo
-  totalLength += prependBlock(block, m_signature.getInfo());
+  totalLength += encoder.prependBlock(m_signature.getInfo());
 
   // Content
-  totalLength += prependBlock(block, getContent());
+  totalLength += encoder.prependBlock(getContent());
 
   // MetaInfo
-  totalLength += getMetaInfo().wireEncode(block);
+  totalLength += getMetaInfo().wireEncode(encoder);
 
   // Name
-  totalLength += getName().wireEncode(block);
+  totalLength += getName().wireEncode(encoder);
 
   if (!unsignedPortion)
     {
-      totalLength += block.prependVarNumber(totalLength);
-      totalLength += block.prependVarNumber(tlv::Data);
+      totalLength += encoder.prependVarNumber(totalLength);
+      totalLength += encoder.prependVarNumber(tlv::Data);
     }
   return totalLength;
 }
 
 
 template size_t
-Data::wireEncode<true>(EncodingImpl<true>& block, bool unsignedPortion) const;
+Data::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block,
+                                       bool unsignedPortion) const;
 
 template size_t
-Data::wireEncode<false>(EncodingImpl<false>& block, bool unsignedPortion) const;
+Data::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block,
+                                         bool unsignedPortion) const;
 
 
 const Block&
diff --git a/src/data.hpp b/src/data.hpp
index 3cccc16..2f56dfc 100644
--- a/src/data.hpp
+++ b/src/data.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -97,9 +97,9 @@
    *                                blocks will be encoded into the block. Note that there
    *                                will be no outer TLV header of the Data packet.
    */
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block, bool wantUnsignedPortionOnly = false) const;
+  wireEncode(EncodingImpl<TAG>& block, bool wantUnsignedPortionOnly = false) const;
 
   /**
    * @brief Encode to a wire format
diff --git a/src/encoding/block-helpers.hpp b/src/encoding/block-helpers.hpp
index 0a58aa0..e3e86f4 100644
--- a/src/encoding/block-helpers.hpp
+++ b/src/encoding/block-helpers.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -17,8 +17,6 @@
  * <http://www.gnu.org/licenses/>.
  *
  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- *
- * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
  */
 
 #ifndef NDN_ENCODING_BLOCK_HELPERS_HPP
@@ -27,8 +25,66 @@
 #include "block.hpp"
 #include "encoding-buffer.hpp"
 
+#include <iterator>
+
 namespace ndn {
 
+/**
+ * @deprecated Use Encoder::prependBlock and Estimator::prependBlock instead
+ */
+template<bool P>
+inline size_t
+prependBlock(EncodingImpl<P>& encoder, const Block& block)
+{
+  return encoder.prependBlock(block);
+}
+
+/**
+ * @deprecated Use Encoder::prependByteArrayBlock and Estimator::prependByteArrayBlock instead
+ */
+template<bool P>
+inline size_t
+prependByteArrayBlock(EncodingImpl<P>& encoder,
+                      uint32_t type, const uint8_t* array, size_t arraySize)
+{
+  return encoder.prependByteArrayBlock(type, array, arraySize);
+}
+
+template<bool P>
+inline size_t
+prependNonNegativeIntegerBlock(EncodingImpl<P>& encoder, uint32_t type, uint64_t number)
+{
+  size_t valueLength = encoder.prependNonNegativeInteger(number);
+  size_t totalLength = valueLength;
+  totalLength += encoder.prependVarNumber(valueLength);
+  totalLength += encoder.prependVarNumber(type);
+
+  return totalLength;
+}
+
+template<bool P>
+inline size_t
+prependBooleanBlock(EncodingImpl<P>& encoder, uint32_t type)
+{
+  size_t totalLength = encoder.prependVarNumber(0);
+  totalLength += encoder.prependVarNumber(type);
+
+  return totalLength;
+}
+
+template<bool P, class U>
+inline size_t
+prependNestedBlock(EncodingImpl<P>& encoder, uint32_t type, const U& nestedBlock)
+{
+  size_t valueLength = nestedBlock.wireEncode(encoder);
+  size_t totalLength = valueLength;
+  totalLength += encoder.prependVarNumber(valueLength);
+  totalLength += encoder.prependVarNumber(type);
+
+  return totalLength;
+}
+
+
 inline Block
 nonNegativeIntegerBlock(uint32_t type, uint64_t value)
 {
@@ -64,10 +120,10 @@
 dataBlock(uint32_t type, const uint8_t* data, size_t dataSize)
 {
   EncodingEstimator estimator;
-  size_t totalLength = prependByteArrayBlock(estimator, type, data, dataSize);
+  size_t totalLength = estimator.prependByteArrayBlock(type, data, dataSize);
 
   EncodingBuffer encoder(totalLength, 0);
-  prependByteArrayBlock(encoder, type, data, dataSize);
+  encoder.prependByteArrayBlock(type, data, dataSize);
 
   return encoder.block();
 }
@@ -78,21 +134,77 @@
   return dataBlock(type, reinterpret_cast<const uint8_t*>(data), dataSize);
 }
 
-// template<class InputIterator>
-// inline Block
-// dataBlock(uint32_t type, InputIterator first, InputIterator last)
-// {
-//   size_t dataSize = 0;
-//   for (InputIterator i = first; i != last; i++)
-//     ++dataSize;
+/**
+ * @brief Helper class template to create a data block when RandomAccessIterator is used
+ */
+template<class Iterator>
+class DataBlockFast
+{
+public:
+  BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Iterator>));
 
-//   OBufferStream os;
-//   tlv::writeVarNumber(os, type);
-//   tlv::writeVarNumber(os, dataSize);
-//   std::copy(first, last, std::ostream_iterator<uint8_t>(os));
+  static Block
+  makeBlock(uint32_t type, Iterator first, Iterator last)
+  {
+    EncodingEstimator estimator;
+    size_t valueLength = last - first;
+    size_t totalLength = valueLength;
+    totalLength += estimator.prependVarNumber(valueLength);
+    totalLength += estimator.prependVarNumber(type);
 
-//   return Block(os.buf());
-// }
+    EncodingBuffer encoder(totalLength, 0);
+    encoder.prependRange(first, last);
+    encoder.prependVarNumber(valueLength);
+    encoder.prependVarNumber(type);
+
+    return encoder.block();
+  }
+};
+
+/**
+ * @brief Helper class template to create a data block when generic InputIterator is used
+ */
+template<class Iterator>
+class DataBlockSlow
+{
+public:
+  BOOST_CONCEPT_ASSERT((boost::InputIterator<Iterator>));
+
+  static Block
+  makeBlock(uint32_t type, Iterator first, Iterator last)
+  {
+    // reserve 4 bytes in front (common for 1(type)-3(length) encoding
+    // Actual size will be adjusted as necessary by the encoder
+    EncodingBuffer encoder(4, 4);
+    size_t valueLength = encoder.appendRange(first, last);
+    encoder.prependVarNumber(valueLength);
+    encoder.prependVarNumber(type);
+
+    return encoder.block();
+  }
+};
+
+/**
+ * @brief Free function to create a block given @p type and range [@p first, @p last) of bytes
+ * @tparam Iterator iterator type satisfying at least InputIterator concept.  Implementation
+ *                  is more optimal when the iterator type satisfies RandomAccessIterator concept
+ *                  It is required that sizeof(std::iterator_traits<Iterator>::value_type) == 1.
+ */
+template<class Iterator>
+inline Block
+dataBlock(uint32_t type, Iterator first, Iterator last)
+{
+  static_assert(sizeof(typename std::iterator_traits<Iterator>::value_type) == 1,
+                "Iterator should point only to char or unsigned char");
+
+  typedef typename boost::mpl::if_<
+    std::is_base_of<std::random_access_iterator_tag,
+                    typename std::iterator_traits<Iterator>::iterator_category>,
+    DataBlockFast<Iterator>,
+    DataBlockSlow<Iterator>>::type DataBlock;
+
+  return DataBlock::makeBlock(type, first, last);
+}
 
 } // namespace ndn
 
diff --git a/src/encoding/block.cpp b/src/encoding/block.cpp
index e8d2cf9..ce13427 100644
--- a/src/encoding/block.cpp
+++ b/src/encoding/block.cpp
@@ -51,7 +51,7 @@
 }
 
 Block::Block(const EncodingBuffer& buffer)
-  : m_buffer(buffer.m_buffer)
+  : m_buffer(const_cast<EncodingBuffer&>(buffer).getBuffer())
   , m_begin(buffer.begin())
   , m_end(buffer.end())
   , m_size(m_end - m_begin)
diff --git a/src/encoding/block.hpp b/src/encoding/block.hpp
index 3daf497..4bbfe52 100644
--- a/src/encoding/block.hpp
+++ b/src/encoding/block.hpp
@@ -28,6 +28,7 @@
 
 #include "buffer.hpp"
 #include "tlv.hpp"
+#include "encoding-buffer-fwd.hpp"
 
 namespace boost {
 namespace asio {
@@ -37,9 +38,6 @@
 
 namespace ndn {
 
-template<bool> class EncodingImpl;
-typedef EncodingImpl<true> EncodingBuffer;
-
 /** @brief Class representing a wire element of NDN-TLV packet format
  */
 class Block
@@ -247,6 +245,12 @@
   Block
   blockFromValue() const;
 
+  /**
+   * @brief Get underlying buffer
+   */
+  shared_ptr<const Buffer>
+  getBuffer() const;
+
 public: // EqualityComparable concept
   bool
   operator==(const Block& other) const;
@@ -258,7 +262,7 @@
   operator boost::asio::const_buffer() const;
 
 protected:
-  ConstBufferPtr m_buffer;
+  shared_ptr<const Buffer> m_buffer;
 
   uint32_t m_type;
 
@@ -270,13 +274,18 @@
   Buffer::const_iterator m_value_end;
 
   mutable element_container m_subBlocks;
-  friend class EncodingImpl<true>;
 };
 
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
 
+inline shared_ptr<const Buffer>
+Block::getBuffer() const
+{
+  return m_buffer;
+}
+
 inline bool
 Block::empty() const
 {
diff --git a/src/encoding/encoder.cpp b/src/encoding/encoder.cpp
new file mode 100644
index 0000000..4b32c10
--- /dev/null
+++ b/src/encoding/encoder.cpp
@@ -0,0 +1,283 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "encoder.hpp"
+
+namespace ndn {
+namespace encoding {
+
+Encoder::Encoder(size_t totalReserve/* = 8800*/, size_t reserveFromBack/* = 400*/)
+  : m_buffer(new Buffer(totalReserve))
+{
+  m_begin = m_end = m_buffer->end() - (reserveFromBack < totalReserve ? reserveFromBack : 0);
+}
+
+
+Encoder::Encoder(const Block& block)
+  : m_buffer(const_pointer_cast<Buffer>(block.getBuffer()))
+  , m_begin(m_buffer->begin() + (block.begin() - m_buffer->begin()))
+  , m_end(m_buffer->begin()   + (block.end()   - m_buffer->begin()))
+{
+}
+
+////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
+
+void
+Encoder::reserveBack(size_t size)
+{
+  if ((m_end + size) > m_buffer->end())
+    reserve(m_buffer->size() * 2 + size, false);
+}
+
+void
+Encoder::reserveFront(size_t size)
+{
+  if ((m_buffer->begin() + size) > m_begin)
+    reserve(m_buffer->size() * 2 + size, true);
+}
+
+
+Block
+Encoder::block(bool verifyLength/* = true*/) const
+{
+  return Block(m_buffer,
+               m_begin, m_end,
+               verifyLength);
+}
+
+void
+Encoder::reserve(size_t size, bool addInFront)
+{
+  if (size < m_buffer->size()) {
+    size = m_buffer->size();
+  }
+
+  if (addInFront) {
+    size_t diffEnd = m_buffer->end() - m_end;
+    size_t diffBegin = m_buffer->end() - m_begin;
+
+    Buffer* buf = new Buffer(size);
+    std::copy_backward(m_buffer->begin(), m_buffer->end(), buf->end());
+
+    m_buffer.reset(buf);
+
+    m_end = m_buffer->end() - diffEnd;
+    m_begin = m_buffer->end() - diffBegin;
+  }
+  else {
+    size_t diffEnd = m_end - m_buffer->begin();
+    size_t diffBegin = m_begin - m_buffer->begin();
+
+    Buffer* buf = new Buffer(size);
+    std::copy(m_buffer->begin(), m_buffer->end(), buf->begin());
+
+    m_buffer.reset(buf);
+
+    m_end = m_buffer->begin() + diffEnd;
+    m_begin = m_buffer->begin() + diffBegin;
+  }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
+
+size_t
+Encoder::prependByte(uint8_t value)
+{
+  reserveFront(1);
+
+  m_begin--;
+  *m_begin = value;
+  return 1;
+}
+
+size_t
+Encoder::appendByte(uint8_t value)
+{
+  reserveBack(1);
+
+  *m_end = value;
+  m_end++;
+  return 1;
+}
+
+
+size_t
+Encoder::prependByteArray(const uint8_t* array, size_t length)
+{
+  reserveFront(length);
+
+  m_begin -= length;
+  std::copy(array, array + length, m_begin);
+  return length;
+}
+
+size_t
+Encoder::appendByteArray(const uint8_t* array, size_t length)
+{
+  reserveBack(length);
+
+  std::copy(array, array + length, m_end);
+  m_end += length;
+  return length;
+}
+
+
+size_t
+Encoder::prependVarNumber(uint64_t varNumber)
+{
+  if (varNumber < 253) {
+    prependByte(static_cast<uint8_t>(varNumber));
+    return 1;
+  }
+  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
+    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
+    prependByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
+    prependByte(253);
+    return 3;
+  }
+  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
+    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
+    prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
+    prependByte(254);
+    return 5;
+  }
+  else {
+    uint64_t value = htobe64(varNumber);
+    prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
+    prependByte(255);
+    return 9;
+  }
+}
+
+size_t
+Encoder::appendVarNumber(uint64_t varNumber)
+{
+  if (varNumber < 253) {
+    appendByte(static_cast<uint8_t>(varNumber));
+    return 1;
+  }
+  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
+    appendByte(253);
+    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
+    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
+    return 3;
+  }
+  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
+    appendByte(254);
+    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
+    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
+    return 5;
+  }
+  else {
+    appendByte(255);
+    uint64_t value = htobe64(varNumber);
+    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
+    return 9;
+  }
+}
+
+
+size_t
+Encoder::prependNonNegativeInteger(uint64_t varNumber)
+{
+  if (varNumber <= std::numeric_limits<uint8_t>::max()) {
+    return prependByte(static_cast<uint8_t>(varNumber));
+  }
+  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
+    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
+    return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
+  }
+  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
+    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
+    return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
+  }
+  else {
+    uint64_t value = htobe64(varNumber);
+    return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
+  }
+}
+
+size_t
+Encoder::appendNonNegativeInteger(uint64_t varNumber)
+{
+  if (varNumber <= std::numeric_limits<uint8_t>::max()) {
+    return appendByte(static_cast<uint8_t>(varNumber));
+  }
+  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
+    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
+    return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
+  }
+  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
+    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
+    return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
+  }
+  else {
+    uint64_t value = htobe64(varNumber);
+    return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
+  }
+}
+
+size_t
+Encoder::prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize)
+{
+  size_t totalLength = prependByteArray(array, arraySize);
+  totalLength += prependVarNumber(arraySize);
+  totalLength += prependVarNumber(type);
+
+  return totalLength;
+}
+
+size_t
+Encoder::appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize)
+{
+  size_t totalLength = appendVarNumber(type);
+  totalLength += appendVarNumber(arraySize);
+  totalLength += appendByteArray(array, arraySize);
+
+  return totalLength;
+}
+
+size_t
+Encoder::prependBlock(const Block& block)
+{
+  if (block.hasWire()) {
+    return prependByteArray(block.wire(), block.size());
+  }
+  else {
+    return prependByteArrayBlock(block.type(), block.value(), block.value_size());
+  }
+}
+
+size_t
+Encoder::appendBlock(const Block& block)
+{
+  if (block.hasWire()) {
+    return appendByteArray(block.wire(), block.size());
+  }
+  else {
+    return appendByteArrayBlock(block.type(), block.value(), block.value_size());
+  }
+}
+
+} // namespace encoding
+} // namespace ndn
diff --git a/src/encoding/encoder.hpp b/src/encoding/encoder.hpp
new file mode 100644
index 0000000..a258d1e
--- /dev/null
+++ b/src/encoding/encoder.hpp
@@ -0,0 +1,343 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_ENCODING_ENCODER_HPP
+#define NDN_ENCODING_ENCODER_HPP
+
+#include "../common.hpp"
+#include "block.hpp"
+
+namespace ndn {
+namespace encoding {
+
+/**
+ * @brief Helper class to perform TLV encoding
+ * Interface of this class (mostly) matches interface of Estimator class
+ * @sa Estimator
+ */
+class Encoder
+{
+public: // common interface between Encoder and Estimator
+  /**
+   * @brief Create instance of the encoder with the specified reserved sizes
+   * @param totalReserve  initial buffer size to reserve
+   * @param totalFromBack number of bytes to reserve for append* operations
+   */
+  explicit
+  Encoder(size_t totalReserve = 8800, size_t reserveFromBack = 400);
+
+  Encoder(const Encoder&) = delete;
+
+  Encoder&
+  operator=(const Encoder&) = delete;
+
+  /**
+   * @brief Prepend a byte
+   */
+  size_t
+  prependByte(uint8_t value);
+
+  /**
+   * @brief Append a byte
+   */
+  size_t
+  appendByte(uint8_t value);
+
+  /**
+   * @brief Prepend a byte array @p array of length @p length
+   */
+  size_t
+  prependByteArray(const uint8_t* array, size_t length);
+
+  /**
+   * @brief Append a byte array @p array of length @p length
+   */
+  size_t
+  appendByteArray(const uint8_t* array, size_t length);
+
+  /**
+   * @brief Prepend range of bytes from the range [@p first, @p last)
+   */
+  template<class Iterator>
+  size_t
+  prependRange(Iterator first, Iterator last);
+
+  /**
+   * @brief Append range of bytes from the range [@p first, @p last)
+   */
+  template<class Iterator>
+  size_t
+  appendRange(Iterator first, Iterator last);
+
+  /**
+   * @brief Prepend VarNumber @p varNumber of NDN TLV encoding
+   * @sa http://named-data.net/doc/ndn-tlv/
+   */
+  size_t
+  prependVarNumber(uint64_t varNumber);
+
+  /**
+   * @brief Prepend VarNumber @p varNumber of NDN TLV encoding
+   * @sa http://named-data.net/doc/ndn-tlv/
+   */
+  size_t
+  appendVarNumber(uint64_t varNumber);
+
+  /**
+   * @brief Prepend non-negative integer @p integer of NDN TLV encoding
+   * @sa http://named-data.net/doc/ndn-tlv/
+   */
+  size_t
+  prependNonNegativeInteger(uint64_t integer);
+
+  /**
+   * @brief Append non-negative integer @p integer of NDN TLV encoding
+   * @sa http://named-data.net/doc/ndn-tlv/
+   */
+  size_t
+  appendNonNegativeInteger(uint64_t integer);
+
+  /**
+   * @brief Prepend TLV block of type @p type and value from buffer @p array of size @p arraySize
+   */
+  size_t
+  prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
+
+  /**
+   * @brief Append TLV block of type @p type and value from buffer @p array of size @p arraySize
+   */
+  size_t
+  appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
+
+  /**
+   * @brief Prepend TLV block @p block
+   */
+  size_t
+  prependBlock(const Block& block);
+
+  /**
+   * @brief Append TLV block @p block
+   */
+  size_t
+  appendBlock(const Block& block);
+
+public: // unique interface to the Encoder
+  typedef Buffer::value_type value_type;
+  typedef Buffer::iterator iterator;
+  typedef Buffer::const_iterator const_iterator;
+
+  /**
+   * @brief Create EncodingBlock from existing block
+   *
+   * This is a dangerous constructor and should be used with caution.
+   * It will modify contents of the buffer that is used by block and may
+   * impact data in other blocks.
+   *
+   * The primary purpose for this method is to be used to extend Block
+   * after sign operation.
+   */
+  explicit
+  Encoder(const Block& block);
+
+  /**
+   * @brief Reserve @p size bytes for the underlying buffer
+   * @param addInFront if true, then @p size bytes will be available in front (i.e., subsequent call
+   *        to prepend* will not need to allocate memory).  If false, then reservation will be done
+   *        at the end of the buffer (i.d., for subsequent append* calls)
+   * @note Reserve size is exact, unlike reserveFront and reserveBack methods
+   * @sa reserveFront, reserveBack
+   */
+  void
+  reserve(size_t size, bool addInFront);
+
+  /**
+   * @brief Reserve at least @p size bytes at the back of the underlying buffer
+   * @note the actual reserve size can be (and in most cases is) larger than specified reservation
+   *       length
+   */
+  void
+  reserveBack(size_t size);
+
+  /**
+   * @brief Reserve at least @p isze bytes at the beginning of the underlying buffer
+   * @note the actual reserve size can be (and in most cases is) larger than specified reservation
+   *       length
+   */
+  void
+  reserveFront(size_t size);
+
+  /**
+   * @brief Get size of the underlying buffer
+   */
+  size_t
+  capacity() const;
+
+  /**
+   * @brief Get underlying buffer
+   */
+  shared_ptr<Buffer>
+  getBuffer();
+
+public: // accessors
+
+  /**
+   * @brief Get an iterator pointing to the first byte of the encoded buffer
+   */
+  iterator
+  begin();
+
+  /**
+   * @brief Get an iterator pointing to the past-the-end byte of the encoded buffer
+   */
+  iterator
+  end();
+
+  /**
+   * @brief Get an iterator pointing to the first byte of the encoded buffer
+   */
+  const_iterator
+  begin() const;
+
+  const_iterator
+  end() const;
+
+  /**
+   * @brief Get a pointer to the first byte of the encoded buffer
+   */
+  uint8_t*
+  buf();
+
+  /**
+   * @brief Get a pointer to the first byte of the encoded buffer
+   */
+  const uint8_t*
+  buf() const;
+
+  /**
+   * @brief Get size of the encoded buffer
+   */
+  size_t
+  size() const;
+
+  /**
+   * @brief Create Block from the underlying buffer
+   *
+   * @param verifyLength If this parameter set to true, Block's constructor
+   *                     will be requested to verify consistency of the encoded
+   *                     length in the Block, otherwise ignored
+   */
+  Block
+  block(bool verifyLength = true) const;
+
+private:
+  shared_ptr<Buffer> m_buffer;
+
+  // invariant: m_begin always points to the position of last-written byte (if prepending data)
+  iterator m_begin;
+  // invariant: m_end always points to the position of next unwritten byte (if appending data)
+  iterator m_end;
+};
+
+
+inline size_t
+Encoder::size() const
+{
+  return m_end - m_begin;
+}
+
+inline shared_ptr<Buffer>
+Encoder::getBuffer()
+{
+  return m_buffer;
+}
+
+inline size_t
+Encoder::capacity() const
+{
+  return m_buffer->size();
+}
+
+inline Buffer::iterator
+Encoder::begin()
+{
+  return m_begin;
+}
+
+inline Buffer::iterator
+Encoder::end()
+{
+  return m_end;
+}
+
+inline Buffer::const_iterator
+Encoder::begin() const
+{
+  return m_begin;
+}
+
+inline Buffer::const_iterator
+Encoder::end() const
+{
+  return m_end;
+}
+
+inline uint8_t*
+Encoder::buf()
+{
+  return &(*m_begin);
+}
+
+inline const uint8_t*
+Encoder::buf() const
+{
+  return &(*m_begin);
+}
+
+template<class Iterator>
+inline size_t
+Encoder::prependRange(Iterator first, Iterator last)
+{
+  size_t length = std::distance(first, last);
+  reserveFront(length);
+
+  m_begin -= length;
+  std::copy(first, last, m_begin);
+  return length;
+}
+
+
+template<class Iterator>
+inline size_t
+Encoder::appendRange(Iterator first, Iterator last)
+{
+  size_t length = std::distance(first, last);
+  reserveBack(length);
+
+  std::copy(first, last, m_end);
+  m_end += length;
+  return length;
+}
+
+
+} // namespace encoding
+} // namespace ndn
+
+#endif // NDN_ENCODING_ENCODER_HPP
diff --git a/src/encoding/encoding-buffer-fwd.hpp b/src/encoding/encoding-buffer-fwd.hpp
new file mode 100644
index 0000000..359864f
--- /dev/null
+++ b/src/encoding/encoding-buffer-fwd.hpp
@@ -0,0 +1,58 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_ENCODING_ENCODING_BUFFER_FWD_HPP
+#define NDN_ENCODING_ENCODING_BUFFER_FWD_HPP
+
+namespace ndn {
+namespace encoding {
+
+typedef bool Tag;
+
+/**
+ * @brief Tag for EncodingImpl to indicate that Encoder is requested
+ * Implementation of the tag may change to class. Use of true directly
+ * as a template parameter is discouraged.
+ */
+static const Tag EncoderTag = true;
+
+/**
+ * @brief Tag for EncodingImpl to indicate that Estimator is requested
+ * Implementation of the tag may change to class. Use of false directly
+ * as a template parameter is discouraged.
+ */
+static const Tag EstimatorTag = false;
+
+template<Tag TAG>
+class EncodingImpl;
+
+typedef EncodingImpl<EncoderTag> EncodingBuffer;
+typedef EncodingImpl<EstimatorTag> EncodingEstimator;
+
+} // namespace encoding
+
+using encoding::EncodingImpl;
+using encoding::EncodingBuffer;
+using encoding::EncodingEstimator;
+
+} // namespace ndn
+
+#endif // NDN_ENCODING_ENCODING_BUFFER_FWD_HPP
diff --git a/src/encoding/encoding-buffer.hpp b/src/encoding/encoding-buffer.hpp
index 4c5185e..67cc4bb 100644
--- a/src/encoding/encoding-buffer.hpp
+++ b/src/encoding/encoding-buffer.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -17,636 +17,54 @@
  * <http://www.gnu.org/licenses/>.
  *
  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- *
- * @author Wentao Shang <http://irl.cs.ucla.edu/~wentao/>
- * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
  */
 
 #ifndef NDN_ENCODING_ENCODING_BUFFER_HPP
 #define NDN_ENCODING_ENCODING_BUFFER_HPP
 
 #include "../common.hpp"
-
-#include "buffer.hpp"
-#include "block.hpp"
+#include "encoding-buffer-fwd.hpp"
+#include "encoder.hpp"
+#include "estimator.hpp"
 
 namespace ndn {
-
 namespace encoding {
-static const bool Buffer = true;
-static const bool Estimator = false;
-} // encoding
-
-template<bool isRealEncoderNotEstimator>
-class EncodingImpl;
-
-typedef EncodingImpl<encoding::Buffer> EncodingBuffer;
-typedef EncodingImpl<encoding::Estimator> EncodingEstimator;
 
 /**
- * @brief Class representing wire element of the NDN packet
+ * @brief EncodingImpl specialization for real TLV encoding
  */
 template<>
-class EncodingImpl<encoding::Buffer>
+class EncodingImpl<EncoderTag> : public encoding::Encoder
 {
 public:
-  /**
-   * @brief Constructor to create a EncodingImpl with specified reserved sizes
-   *
-   * The caller should make sure that that reserveFromBack does not exceed totalReserve,
-   * otherwise behavior is undefined.
-   */
-  EncodingImpl(size_t totalReserve = 8800,
-               size_t reserveFromBack = 400)
-    : m_buffer(new Buffer(totalReserve))
+  explicit
+  EncodingImpl(size_t totalReserve = 8800, size_t reserveFromBack = 400)
+    : Encoder(totalReserve, reserveFromBack)
   {
-    m_begin = m_end = m_buffer->end() - (reserveFromBack < totalReserve ? reserveFromBack : 0);
   }
 
-  /**
-   * @brief Create EncodingBlock from existing block
-   *
-   * This is a dangerous constructor and should be used with caution.
-   * It will modify contents of the buffer that is used by block and may
-   * impact data in other blocks.
-   *
-   * The primary purpose for this method is to be used to extend Block
-   * after sign operation.
-   */
   explicit
   EncodingImpl(const Block& block)
-    : m_buffer(const_pointer_cast<Buffer>(block.m_buffer))
-    , m_begin(m_buffer->begin() + (block.begin() - m_buffer->begin()))
-    , m_end(m_buffer->begin()   + (block.end()   - m_buffer->begin()))
+    : Encoder(block)
   {
   }
-
-  inline size_t
-  size() const;
-
-  inline size_t
-  capacity() const;
-
-  inline uint8_t*
-  buf();
-
-  inline const uint8_t*
-  buf() const;
-
-  /**
-   * @brief Create Block from the underlying EncodingBuffer
-   *
-   * @param verifyLength If this parameter set to true, Block's constructor
-   *                     will be requested to verify consistency of the encoded
-   *                     length in the Block, otherwise ignored
-   */
-  inline Block
-  block(bool verifyLength = true) const;
-
-  inline void
-  resize(size_t size, bool addInFront);
-
-  inline Buffer::iterator
-  begin();
-
-  inline Buffer::iterator
-  end();
-
-  inline Buffer::const_iterator
-  begin() const;
-
-  inline Buffer::const_iterator
-  end() const;
-
-  inline size_t
-  prependByte(uint8_t value);
-
-  inline size_t
-  prependByteArray(const uint8_t* array, size_t length);
-
-  inline size_t
-  prependNonNegativeInteger(uint64_t varNumber);
-
-  inline size_t
-  prependVarNumber(uint64_t varNumber);
-
-  inline size_t
-  prependBlock(const Block& block);
-
-  inline size_t
-  appendByte(uint8_t value);
-
-  inline size_t
-  appendByteArray(const uint8_t* array, size_t length);
-
-  inline size_t
-  appendNonNegativeInteger(uint64_t varNumber);
-
-  inline size_t
-  appendVarNumber(uint64_t varNumber);
-
-  inline size_t
-  appendBlock(const Block& block);
-
-  // inline void
-  // removeByteFromFront();
-
-  // inline void
-  // removeByteFromEnd();
-
-  // inline void
-  // removeVarNumberFromFront(uint64_t varNumber);
-
-  // inline void
-  // removeVarNumberFromBack(uint64_t varNumber);
-
-private:
-  BufferPtr m_buffer;
-
-  // invariant: m_begin always points to the position of last-written byte (if prepending data)
-  Buffer::iterator m_begin;
-  // invariant: m_end always points to the position of next unwritten byte (if appending data)
-  Buffer::iterator m_end;
-
-  friend class Block;
 };
 
-
 /**
- * @brief Class representing wire element of the NDN packet
+ * @brief EncodingImpl specialization TLV size estimation
  */
 template<>
-class EncodingImpl<encoding::Estimator>
+class EncodingImpl<EstimatorTag> : public encoding::Estimator
 {
 public:
-  EncodingImpl(size_t totalReserve = 8800,
-               size_t reserveFromBack = 400)
+  explicit
+  EncodingImpl(size_t totalReserve = 0, size_t totalFromBack = 0)
+    : Estimator(totalReserve, totalFromBack)
   {
   }
-
-  inline size_t
-  prependByte(uint8_t value);
-
-  inline size_t
-  prependByteArray(const uint8_t* array, size_t length);
-
-  inline size_t
-  prependNonNegativeInteger(uint64_t varNumber);
-
-  inline size_t
-  prependVarNumber(uint64_t varNumber);
-
-  inline size_t
-  prependBlock(const Block& block);
-
-  inline size_t
-  appendByte(uint8_t value);
-
-  inline size_t
-  appendByteArray(const uint8_t* array, size_t length);
-
-  inline size_t
-  appendNonNegativeInteger(uint64_t varNumber);
-
-  inline size_t
-  appendVarNumber(uint64_t varNumber);
-
-  inline size_t
-  appendBlock(const Block& block);
 };
 
-////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////
-
-/// helper methods
-
-template<bool P>
-inline size_t
-prependNonNegativeIntegerBlock(EncodingImpl<P>& encoder, uint32_t type, uint64_t number)
-{
-  size_t valueLength = encoder.prependNonNegativeInteger(number);
-  size_t totalLength = valueLength;
-  totalLength += encoder.prependVarNumber(valueLength);
-  totalLength += encoder.prependVarNumber(type);
-
-  return totalLength;
-}
-
-template<bool P>
-inline size_t
-prependByteArrayBlock(EncodingImpl<P>& encoder, uint32_t type,
-                      const uint8_t* array, size_t arraySize)
-{
-  size_t valueLength = encoder.prependByteArray(array, arraySize);
-  size_t totalLength = valueLength;
-  totalLength += encoder.prependVarNumber(valueLength);
-  totalLength += encoder.prependVarNumber(type);
-
-  return totalLength;
-}
-
-template<bool P>
-inline size_t
-prependBooleanBlock(EncodingImpl<P>& encoder, uint32_t type)
-{
-  size_t totalLength = encoder.prependVarNumber(0);
-  totalLength += encoder.prependVarNumber(type);
-
-  return totalLength;
-}
-
-
-template<bool P, class U>
-inline size_t
-prependNestedBlock(EncodingImpl<P>& encoder, uint32_t type, const U& nestedBlock)
-{
-  size_t valueLength = nestedBlock.wireEncode(encoder);
-  size_t totalLength = valueLength;
-  totalLength += encoder.prependVarNumber(valueLength);
-  totalLength += encoder.prependVarNumber(type);
-
-  return totalLength;
-}
-
-template<bool P>
-inline size_t
-prependBlock(EncodingImpl<P>& encoder, const Block& block)
-{
-  return encoder.prependByteArray(block.wire(), block.size());
-}
-
-template<bool P>
-inline size_t
-appendByteArrayBlock(EncodingImpl<P>& encoder, uint32_t type,
-                      const uint8_t* array, size_t arraySize)
-{
-  size_t totalLength = encoder.appendVarNumber(type);
-  totalLength += encoder.appendVarNumber(arraySize);
-  totalLength += encoder.appendByteArray(array, arraySize);
-
-  return totalLength;
-}
-
-template<bool P>
-inline size_t
-appendBlock(EncodingImpl<P>& encoder, const Block& block)
-{
-  return encoder.appendByteArray(block.wire(), block.size());
-}
-
-////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////
-
-inline size_t
-EncodingImpl<encoding::Buffer>::size() const
-{
-  return m_end - m_begin;
-}
-
-inline size_t
-EncodingImpl<encoding::Buffer>::capacity() const
-{
-  return m_buffer->size();
-}
-
-inline uint8_t*
-EncodingImpl<encoding::Buffer>::buf()
-{
-  return &(*m_begin);
-}
-
-inline const uint8_t*
-EncodingImpl<encoding::Buffer>::buf() const
-{
-  return &(*m_begin);
-}
-
-inline Block
-EncodingImpl<encoding::Buffer>::block(bool verifyLength/* = true*/) const
-{
-  return Block(m_buffer,
-               m_begin, m_end,
-               verifyLength);
-}
-
-inline void
-EncodingImpl<encoding::Buffer>::resize(size_t size, bool addInFront)
-{
-  if (addInFront)
-    {
-      size_t diff_end = m_buffer->end() - m_end;
-      size_t diff_begin = m_buffer->end() - m_begin;
-
-      Buffer* buf = new Buffer(size);
-      std::copy_backward(m_buffer->begin(), m_buffer->end(), buf->end());
-
-      m_buffer.reset(buf);
-
-      m_end = m_buffer->end() - diff_end;
-      m_begin = m_buffer->end() - diff_begin;
-    }
-  else
-    {
-      size_t diff_end = m_end - m_buffer->begin();
-      size_t diff_begin = m_begin - m_buffer->begin();
-
-      Buffer* buf = new Buffer(size);
-      std::copy(m_buffer->begin(), m_buffer->end(), buf->begin());
-
-      m_buffer.reset(buf);
-
-      m_end = m_buffer->begin() + diff_end;
-      m_begin = m_buffer->begin() + diff_begin;
-    }
-}
-
-inline Buffer::iterator
-EncodingImpl<encoding::Buffer>::begin()
-{
-  return m_begin;
-}
-
-inline Buffer::iterator
-EncodingImpl<encoding::Buffer>::end()
-{
-  return m_end;
-}
-
-inline Buffer::const_iterator
-EncodingImpl<encoding::Buffer>::begin() const
-{
-  return m_begin;
-}
-
-inline Buffer::const_iterator
-EncodingImpl<encoding::Buffer>::end() const
-{
-  return m_end;
-}
-
-
-//////////////////////////////////////////////////////////
-// Prepend to the back of the buffer. Resize if needed. //
-//////////////////////////////////////////////////////////
-
-inline size_t
-EncodingImpl<encoding::Buffer>::prependByte(uint8_t value)
-{
-  if (m_begin == m_buffer->begin())
-    resize(m_buffer->size() * 2, true);
-
-  m_begin--;
-  *m_begin = value;
-  return 1;
-}
-
-inline size_t
-EncodingImpl<encoding::Estimator>::prependByte(uint8_t value)
-{
-  return 1;
-}
-
-inline size_t
-EncodingImpl<encoding::Buffer>::prependByteArray(const uint8_t* array, size_t length)
-{
-  if ((m_buffer->begin() + length) > m_begin)
-    resize(m_buffer->size() * 2 + length, true);
-
-  m_begin -= length;
-  std::copy(array, array + length, m_begin);
-  return length;
-}
-
-inline size_t
-EncodingImpl<encoding::Estimator>::prependByteArray(const uint8_t* array, size_t length)
-{
-  return length;
-}
-
-inline size_t
-EncodingImpl<encoding::Buffer>::prependNonNegativeInteger(uint64_t varNumber)
-{
-  if (varNumber <= std::numeric_limits<uint8_t>::max()) {
-    return prependByte(static_cast<uint8_t>(varNumber));
-  }
-  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
-    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
-    return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
-  }
-  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
-    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
-    return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
-  }
-  else {
-    uint64_t value = htobe64(varNumber);
-    return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
-  }
-}
-
-inline size_t
-EncodingImpl<encoding::Estimator>::prependNonNegativeInteger(uint64_t varNumber)
-{
-  if (varNumber <= std::numeric_limits<uint8_t>::max()) {
-    return 1;
-  }
-  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
-    return 2;
-  }
-  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
-    return 4;
-  }
-  else {
-    return 8;
-  }
-}
-
-inline size_t
-EncodingImpl<encoding::Buffer>::prependVarNumber(uint64_t varNumber)
-{
-  if (varNumber < 253) {
-    prependByte(static_cast<uint8_t>(varNumber));
-    return 1;
-  }
-  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
-    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
-    prependByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
-    prependByte(253);
-    return 3;
-  }
-  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
-    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
-    prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
-    prependByte(254);
-    return 5;
-  }
-  else {
-    uint64_t value = htobe64(varNumber);
-    prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
-    prependByte(255);
-    return 9;
-  }
-}
-
-inline size_t
-EncodingImpl<encoding::Estimator>::prependVarNumber(uint64_t varNumber)
-{
-  if (varNumber < 253) {
-    return 1;
-  }
-  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
-    return 3;
-  }
-  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
-    return 5;
-  }
-  else {
-    return 9;
-  }
-}
-
-inline size_t
-EncodingImpl<encoding::Buffer>::prependBlock(const Block& block)
-{
-  if (block.hasWire()) {
-    return prependByteArray(block.wire(), block.size());
-  }
-  else {
-    return prependByteArrayBlock(*this, block.type(), block.value(), block.value_size());
-  }
-}
-
-inline size_t
-EncodingImpl<encoding::Estimator>::prependBlock(const Block& block)
-{
-  if (block.hasWire()) {
-    return block.size();
-  }
-  else {
-    return prependByteArrayBlock(*this, block.type(), block.value(), block.value_size());
-  }
-}
-
-/////////////////////////////////////////////////////////
-// Append to the back of the buffer. Resize if needed. //
-/////////////////////////////////////////////////////////
-
-inline size_t
-EncodingImpl<encoding::Buffer>::appendByte(uint8_t value)
-{
-  if (m_end == m_buffer->end())
-    resize(m_buffer->size() * 2, false);
-
-  *m_end = value;
-  m_end++;
-  return 1;
-}
-
-inline size_t
-EncodingImpl<encoding::Estimator>::appendByte(uint8_t value)
-{
-  return 1;
-}
-
-inline size_t
-EncodingImpl<encoding::Buffer>::appendByteArray(const uint8_t* array, size_t length)
-{
-  if ((m_end + length) > m_buffer->end())
-    resize(m_buffer->size() * 2 + length, false);
-
-  std::copy(array, array + length, m_end);
-  m_end += length;
-  return length;
-}
-
-inline size_t
-EncodingImpl<encoding::Buffer>::appendBlock(const Block& block)
-{
-  if (block.hasWire()) {
-    return appendByteArray(block.wire(), block.size());
-  }
-  else {
-    return appendByteArrayBlock(*this, block.type(), block.value(), block.value_size());
-  }
-}
-
-inline size_t
-EncodingImpl<encoding::Estimator>::appendBlock(const Block& block)
-{
-  if (block.hasWire()) {
-    return block.size();
-  }
-  else {
-    return appendByteArrayBlock(*this, block.type(), block.value(), block.value_size());
-  }
-}
-
-inline size_t
-EncodingImpl<encoding::Estimator>::appendByteArray(const uint8_t* array, size_t length)
-{
-  return prependByteArray(array, length);
-}
-
-inline size_t
-EncodingImpl<encoding::Buffer>::appendNonNegativeInteger(uint64_t varNumber)
-{
-  if (varNumber <= std::numeric_limits<uint8_t>::max()) {
-    return appendByte(static_cast<uint8_t>(varNumber));
-  }
-  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
-    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
-    return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
-  }
-  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
-    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
-    return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
-  }
-  else {
-    uint64_t value = htobe64(varNumber);
-    return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
-  }
-}
-
-inline size_t
-EncodingImpl<encoding::Estimator>::appendNonNegativeInteger(uint64_t varNumber)
-{
-  return prependNonNegativeInteger(varNumber);
-}
-
-inline size_t
-EncodingImpl<encoding::Buffer>::appendVarNumber(uint64_t varNumber)
-{
-  if (varNumber < 253) {
-    appendByte(static_cast<uint8_t>(varNumber));
-    return 1;
-  }
-  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
-    appendByte(253);
-    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
-    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
-    return 3;
-  }
-  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
-    appendByte(254);
-    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
-    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
-    return 5;
-  }
-  else {
-    appendByte(255);
-    uint64_t value = htobe64(varNumber);
-    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
-    return 9;
-  }
-}
-
-inline size_t
-EncodingImpl<encoding::Estimator>::appendVarNumber(uint64_t varNumber)
-{
-  return prependVarNumber(varNumber);
-}
-
-} // ndn
+} // namespace encoding
+} // namespace ndn
 
 #endif // NDN_ENCODING_ENCODING_BUFFER_HPP
diff --git a/src/encoding/estimator.cpp b/src/encoding/estimator.cpp
new file mode 100644
index 0000000..9ec2336
--- /dev/null
+++ b/src/encoding/estimator.cpp
@@ -0,0 +1,140 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "estimator.hpp"
+
+namespace ndn {
+namespace encoding {
+
+Estimator::Estimator(size_t totalReserve, size_t reserveFromBack)
+{
+}
+
+size_t
+Estimator::prependByte(uint8_t value)
+{
+  return 1;
+}
+
+size_t
+Estimator::appendByte(uint8_t value)
+{
+  return 1;
+}
+
+
+size_t
+Estimator::prependByteArray(const uint8_t* array, size_t length)
+{
+  return length;
+}
+
+size_t
+Estimator::appendByteArray(const uint8_t* array, size_t length)
+{
+  return prependByteArray(array, length);
+}
+
+size_t
+Estimator::prependVarNumber(uint64_t varNumber)
+{
+  if (varNumber < 253) {
+    return 1;
+  }
+  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
+    return 3;
+  }
+  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
+    return 5;
+  }
+  else {
+    return 9;
+  }
+}
+
+size_t
+Estimator::appendVarNumber(uint64_t varNumber)
+{
+  return prependVarNumber(varNumber);
+}
+
+
+size_t
+Estimator::prependNonNegativeInteger(uint64_t varNumber)
+{
+  if (varNumber <= std::numeric_limits<uint8_t>::max()) {
+    return 1;
+  }
+  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
+    return 2;
+  }
+  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
+    return 4;
+  }
+  else {
+    return 8;
+  }
+}
+
+size_t
+Estimator::appendNonNegativeInteger(uint64_t varNumber)
+{
+  return prependNonNegativeInteger(varNumber);
+}
+
+
+size_t
+Estimator::prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize)
+{
+  size_t totalLength = arraySize;
+  totalLength += prependVarNumber(arraySize);
+  totalLength += prependVarNumber(type);
+
+  return totalLength;
+}
+
+size_t
+Estimator::appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize)
+{
+  return prependByteArrayBlock(type, array, arraySize);
+}
+
+
+size_t
+Estimator::prependBlock(const Block& block)
+{
+  if (block.hasWire()) {
+    return block.size();
+  }
+  else {
+    return prependByteArrayBlock(block.type(), block.value(), block.value_size());
+  }
+}
+
+size_t
+Estimator::appendBlock(const Block& block)
+{
+  return prependBlock(block);
+}
+
+
+} // namespace encoding
+} // namespace ndn
diff --git a/src/encoding/estimator.hpp b/src/encoding/estimator.hpp
new file mode 100644
index 0000000..19d6423
--- /dev/null
+++ b/src/encoding/estimator.hpp
@@ -0,0 +1,162 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_ENCODING_ESTIMATOR_HPP
+#define NDN_ENCODING_ESTIMATOR_HPP
+
+#include "../common.hpp"
+#include "block.hpp"
+
+namespace ndn {
+namespace encoding {
+
+/**
+ * @brief Helper class to estimate size of TLV encoding
+ * Interface of this class (mostly) matches interface of Encoder class
+ * @sa Encoder
+ */
+class Estimator
+{
+public: // common interface between Encoder and Estimator
+  /**
+   * @brief Create instance of the estimator
+   * @param totalReserve  not used (for compatibility with the Encoder)
+   * @param totalFromBack not used (for compatibility with the Encoder)
+   */
+  explicit
+  Estimator(size_t totalReserve = 0, size_t reserveFromBack = 0);
+
+  Estimator(const Estimator&) = delete;
+
+  Estimator&
+  operator=(const Estimator&) = delete;
+
+  /**
+   * @brief Prepend a byte
+   */
+  size_t
+  prependByte(uint8_t value);
+
+  /**
+   * @brief Append a byte
+   */
+  size_t
+  appendByte(uint8_t value);
+
+  /**
+   * @brief Prepend a byte array @p array of length @p length
+   */
+  size_t
+  prependByteArray(const uint8_t* array, size_t length);
+
+  /**
+   * @brief Append a byte array @p array of length @p length
+   */
+  size_t
+  appendByteArray(const uint8_t* array, size_t length);
+
+  /**
+   * @brief Prepend range of bytes from the range [@p first, @p last)
+   */
+  template<class Iterator>
+  size_t
+  prependRange(Iterator first, Iterator last);
+
+  /**
+   * @brief Append range of bytes from the range [@p first, @p last)
+   */
+  template<class Iterator>
+  size_t
+  appendRange(Iterator first, Iterator last);
+
+  /**
+   * @brief Prepend VarNumber @p varNumber of NDN TLV encoding
+   * @sa http://named-data.net/doc/ndn-tlv/
+   */
+  size_t
+  prependVarNumber(uint64_t varNumber);
+
+  /**
+   * @brief Prepend VarNumber @p varNumber of NDN TLV encoding
+   * @sa http://named-data.net/doc/ndn-tlv/
+   */
+  size_t
+  appendVarNumber(uint64_t varNumber);
+
+  /**
+   * @brief Prepend non-negative integer @p integer of NDN TLV encoding
+   * @sa http://named-data.net/doc/ndn-tlv/
+   */
+  size_t
+  prependNonNegativeInteger(uint64_t integer);
+
+  /**
+   * @brief Append non-negative integer @p integer of NDN TLV encoding
+   * @sa http://named-data.net/doc/ndn-tlv/
+   */
+  size_t
+  appendNonNegativeInteger(uint64_t integer);
+
+  /**
+   * @brief Prepend TLV block of type @p type and value from buffer @p array of size @p arraySize
+   */
+  size_t
+  prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
+
+  /**
+   * @brief Append TLV block of type @p type and value from buffer @p array of size @p arraySize
+   */
+  size_t
+  appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
+
+  /**
+   * @brief Prepend TLV block @p block
+   */
+  size_t
+  prependBlock(const Block& block);
+
+  /**
+   * @brief Append TLV block @p block
+   */
+  size_t
+  appendBlock(const Block& block);
+};
+
+
+template<class Iterator>
+inline size_t
+Estimator::prependRange(Iterator first, Iterator last)
+{
+  return std::distance(first, last);
+}
+
+
+template<class Iterator>
+inline size_t
+Estimator::appendRange(Iterator first, Iterator last)
+{
+  return prependRange(first, last);
+}
+
+} // namespace encoding
+} // namespace ndn
+
+#endif // NDN_ENCODING_ESTIMATOR_HPP
diff --git a/src/exclude.cpp b/src/exclude.cpp
index c32d214..b48d245 100644
--- a/src/exclude.cpp
+++ b/src/exclude.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -41,9 +41,9 @@
   wireDecode(wire);
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-Exclude::wireEncode(EncodingImpl<T>& block) const
+Exclude::wireEncode(EncodingImpl<TAG>& block) const
 {
   if (m_exclude.empty()) {
     throw Error("Exclude filter cannot be empty");
@@ -72,10 +72,10 @@
 }
 
 template size_t
-Exclude::wireEncode<true>(EncodingImpl<true>& block) const;
+Exclude::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
 
 template size_t
-Exclude::wireEncode<false>(EncodingImpl<false>& block) const;
+Exclude::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
 
 const Block&
 Exclude::wireEncode() const
diff --git a/src/exclude.hpp b/src/exclude.hpp
index 1475697..fe80acc 100644
--- a/src/exclude.hpp
+++ b/src/exclude.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -62,9 +62,9 @@
   /**
    * @brief Fast encoding or block size estimation
    */
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block) const;
+  wireEncode(EncodingImpl<TAG>& block) const;
 
   /**
    * @brief Encode to a wire format
diff --git a/src/interest.cpp b/src/interest.cpp
index d53b949..0ae0852 100644
--- a/src/interest.cpp
+++ b/src/interest.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -17,8 +17,6 @@
  * <http://www.gnu.org/licenses/>.
  *
  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- *
- * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu>
  */
 
 #include "interest.hpp"
@@ -229,9 +227,9 @@
   return true;
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-Interest::wireEncode(EncodingImpl<T>& block) const
+Interest::wireEncode(EncodingImpl<TAG>& block) const
 {
   size_t totalLength = 0;
 
@@ -278,10 +276,10 @@
 }
 
 template size_t
-Interest::wireEncode<true>(EncodingImpl<true>& block) const;
+Interest::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
 
 template size_t
-Interest::wireEncode<false>(EncodingImpl<false>& block) const;
+Interest::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
 
 const Block&
 Interest::wireEncode() const
diff --git a/src/interest.hpp b/src/interest.hpp
index 9516454..48f34f7 100644
--- a/src/interest.hpp
+++ b/src/interest.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -17,8 +17,6 @@
  * <http://www.gnu.org/licenses/>.
  *
  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- *
- * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu>
  */
 
 #ifndef NDN_INTEREST_HPP
@@ -98,9 +96,9 @@
   /**
    * @brief Fast encoding or block size estimation
    */
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block) const;
+  wireEncode(EncodingImpl<TAG>& block) const;
 
   /**
    * @brief Encode to a wire format
diff --git a/src/key-locator.cpp b/src/key-locator.cpp
index ddc0029..fd1a7ad 100644
--- a/src/key-locator.cpp
+++ b/src/key-locator.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -46,9 +46,9 @@
   setName(name);
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-KeyLocator::wireEncode(EncodingImpl<T>& block) const
+KeyLocator::wireEncode(EncodingImpl<TAG>& block) const
 {
   // KeyLocator ::= KEY-LOCATOR-TYPE TLV-LENGTH (Name | KeyDigest)
   // KeyDigest ::= KEY-DIGEST-TYPE TLV-LENGTH BYTE+
@@ -74,10 +74,10 @@
 }
 
 template size_t
-KeyLocator::wireEncode<true>(EncodingImpl<true>& estimator) const;
+KeyLocator::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& estimator) const;
 
 template size_t
-KeyLocator::wireEncode<false>(EncodingImpl<false>& encoder) const;
+KeyLocator::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
 
 const Block&
 KeyLocator::wireEncode() const
diff --git a/src/key-locator.hpp b/src/key-locator.hpp
index 3c63bbd..27b8d67 100644
--- a/src/key-locator.hpp
+++ b/src/key-locator.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -74,9 +74,9 @@
   /** \brief prepend wire encoding
    *  \param block EncodingBuffer or Estimator
    */
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block) const;
+  wireEncode(EncodingImpl<TAG>& block) const;
 
   /** \return wire encoding
    */
diff --git a/src/management/nfd-channel-status.cpp b/src/management/nfd-channel-status.cpp
index 771b8d3..2c93283 100644
--- a/src/management/nfd-channel-status.cpp
+++ b/src/management/nfd-channel-status.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -42,13 +42,13 @@
   this->wireDecode(payload);
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-ChannelStatus::wireEncode(EncodingImpl<T>& encoder) const
+ChannelStatus::wireEncode(EncodingImpl<TAG>& encoder) const
 {
   size_t totalLength = 0;
 
-  totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
+  totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
                  reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
 
   totalLength += encoder.prependVarNumber(totalLength);
@@ -57,10 +57,10 @@
 }
 
 template size_t
-ChannelStatus::wireEncode<true>(EncodingImpl<true>& block) const;
+ChannelStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
 
 template size_t
-ChannelStatus::wireEncode<false>(EncodingImpl<false>& block) const;
+ChannelStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
 
 const Block&
 ChannelStatus::wireEncode() const
diff --git a/src/management/nfd-channel-status.hpp b/src/management/nfd-channel-status.hpp
index 4bb2707..422de2b 100644
--- a/src/management/nfd-channel-status.hpp
+++ b/src/management/nfd-channel-status.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -50,9 +50,9 @@
   explicit
   ChannelStatus(const Block& payload);
 
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& encoder) const;
+  wireEncode(EncodingImpl<TAG>& encoder) const;
 
   const Block&
   wireEncode() const;
diff --git a/src/management/nfd-control-parameters.cpp b/src/management/nfd-control-parameters.cpp
index 773d65a..eb9fa0f 100644
--- a/src/management/nfd-control-parameters.cpp
+++ b/src/management/nfd-control-parameters.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -44,9 +44,9 @@
   wireDecode(block);
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-ControlParameters::wireEncode(EncodingImpl<T>& encoder) const
+ControlParameters::wireEncode(EncodingImpl<TAG>& encoder) const
 {
   size_t totalLength = 0;
 
@@ -90,10 +90,10 @@
 }
 
 template size_t
-ControlParameters::wireEncode<true>(EncodingImpl<true>& encoder) const;
+ControlParameters::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
 
 template size_t
-ControlParameters::wireEncode<false>(EncodingImpl<false>& estimator) const;
+ControlParameters::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
 
 const Block&
 ControlParameters::wireEncode() const
diff --git a/src/management/nfd-control-parameters.hpp b/src/management/nfd-control-parameters.hpp
index faa7c3a..29434fc 100644
--- a/src/management/nfd-control-parameters.hpp
+++ b/src/management/nfd-control-parameters.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -89,9 +89,9 @@
   explicit
   ControlParameters(const Block& block);
 
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& encoder) const;
+  wireEncode(EncodingImpl<TAG>& encoder) const;
 
   const Block&
   wireEncode() const;
diff --git a/src/management/nfd-face-event-notification.cpp b/src/management/nfd-face-event-notification.cpp
index 18b12d1..9e757a3 100644
--- a/src/management/nfd-face-event-notification.cpp
+++ b/src/management/nfd-face-event-notification.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -43,9 +43,9 @@
   this->wireDecode(block);
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-FaceEventNotification::wireEncode(EncodingImpl<T>& encoder) const
+FaceEventNotification::wireEncode(EncodingImpl<TAG>& encoder) const
 {
   size_t totalLength = 0;
 
@@ -55,9 +55,9 @@
                  tlv::nfd::FacePersistency, m_facePersistency);
   totalLength += prependNonNegativeIntegerBlock(encoder,
                  tlv::nfd::FaceScope, m_faceScope);
-  totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
+  totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
                  reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
-  totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
+  totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
                  reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
   totalLength += prependNonNegativeIntegerBlock(encoder,
                  tlv::nfd::FaceId, m_faceId);
diff --git a/src/management/nfd-face-event-notification.hpp b/src/management/nfd-face-event-notification.hpp
index f101f25..56325ef 100644
--- a/src/management/nfd-face-event-notification.hpp
+++ b/src/management/nfd-face-event-notification.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -51,9 +51,9 @@
 
   /** \brief prepend FaceEventNotification to the encoder
    */
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& encoder) const;
+  wireEncode(EncodingImpl<TAG>& encoder) const;
 
   /** \brief encode FaceEventNotification
    */
diff --git a/src/management/nfd-face-query-filter.cpp b/src/management/nfd-face-query-filter.cpp
index d0ca150..0fa02f2 100644
--- a/src/management/nfd-face-query-filter.cpp
+++ b/src/management/nfd-face-query-filter.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -49,9 +49,9 @@
   this->wireDecode(block);
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-FaceQueryFilter::wireEncode(EncodingImpl<T>& encoder) const
+FaceQueryFilter::wireEncode(EncodingImpl<TAG>& encoder) const
 {
   size_t totalLength = 0;
 
@@ -71,17 +71,17 @@
   }
 
   if (m_hasLocalUri) {
-    totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
+    totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
                    reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
   }
 
   if (m_hasRemoteUri) {
-    totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
+    totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
                    reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
   }
 
   if (m_hasUriScheme) {
-    totalLength += prependByteArrayBlock(encoder, tlv::nfd::UriScheme,
+    totalLength += encoder.prependByteArrayBlock(tlv::nfd::UriScheme,
                    reinterpret_cast<const uint8_t*>(m_uriScheme.c_str()), m_uriScheme.size());
   }
 
@@ -96,10 +96,10 @@
 }
 
 template size_t
-FaceQueryFilter::wireEncode<true>(EncodingImpl<true>& block) const;
+FaceQueryFilter::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
 
 template size_t
-FaceQueryFilter::wireEncode<false>(EncodingImpl<false>& block) const;
+FaceQueryFilter::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
 
 const Block&
 FaceQueryFilter::wireEncode() const
diff --git a/src/management/nfd-face-query-filter.hpp b/src/management/nfd-face-query-filter.hpp
index 5833d1f..30ea386 100644
--- a/src/management/nfd-face-query-filter.hpp
+++ b/src/management/nfd-face-query-filter.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -53,9 +53,9 @@
 
   /** \brief prepend FaceQueryFilter to the encoder
    */
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& encoder) const;
+  wireEncode(EncodingImpl<TAG>& encoder) const;
 
   /** \brief encode FaceQueryFilter
    */
diff --git a/src/management/nfd-face-status.cpp b/src/management/nfd-face-status.cpp
index 8a08825..becaa75 100644
--- a/src/management/nfd-face-status.cpp
+++ b/src/management/nfd-face-status.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -49,9 +49,9 @@
   this->wireDecode(block);
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-FaceStatus::wireEncode(EncodingImpl<T>& encoder) const
+FaceStatus::wireEncode(EncodingImpl<TAG>& encoder) const
 {
   size_t totalLength = 0;
 
@@ -77,9 +77,9 @@
     totalLength += prependNonNegativeIntegerBlock(encoder,
                    tlv::nfd::ExpirationPeriod, m_expirationPeriod.count());
   }
-  totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
+  totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
                  reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
-  totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
+  totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
                  reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
   totalLength += prependNonNegativeIntegerBlock(encoder,
                  tlv::nfd::FaceId, m_faceId);
@@ -90,10 +90,10 @@
 }
 
 template size_t
-FaceStatus::wireEncode<true>(EncodingImpl<true>& block) const;
+FaceStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
 
 template size_t
-FaceStatus::wireEncode<false>(EncodingImpl<false>& block) const;
+FaceStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
 
 const Block&
 FaceStatus::wireEncode() const
@@ -321,4 +321,3 @@
 
 } // namespace nfd
 } // namespace ndn
-
diff --git a/src/management/nfd-face-status.hpp b/src/management/nfd-face-status.hpp
index 5b70ce2..f29b568 100644
--- a/src/management/nfd-face-status.hpp
+++ b/src/management/nfd-face-status.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -44,9 +44,9 @@
 
   /** \brief prepend FaceStatus to the encoder
    */
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& encoder) const;
+  wireEncode(EncodingImpl<TAG>& encoder) const;
 
   /** \brief encode FaceStatus
    */
diff --git a/src/management/nfd-fib-entry.cpp b/src/management/nfd-fib-entry.cpp
index 73b5f42..b8c5274 100644
--- a/src/management/nfd-fib-entry.cpp
+++ b/src/management/nfd-fib-entry.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -71,9 +71,9 @@
   return *this;
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-NextHopRecord::wireEncode(EncodingImpl<T>& block) const
+NextHopRecord::wireEncode(EncodingImpl<TAG>& block) const
 {
   size_t totalLength = 0;
   totalLength += prependNonNegativeIntegerBlock(block,
@@ -90,10 +90,10 @@
 }
 
 template size_t
-NextHopRecord::wireEncode<true>(EncodingImpl<true>& block) const;
+NextHopRecord::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
 
 template size_t
-NextHopRecord::wireEncode<false>(EncodingImpl<false>& block) const;
+NextHopRecord::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
 
 const Block&
 NextHopRecord::wireEncode() const
@@ -182,9 +182,9 @@
   return *this;
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-FibEntry::wireEncode(EncodingImpl<T>& block) const
+FibEntry::wireEncode(EncodingImpl<TAG>& block) const
 {
   size_t totalLength = 0;
 
@@ -200,10 +200,10 @@
 }
 
 template size_t
-FibEntry::wireEncode<true>(EncodingImpl<true>& block) const;
+FibEntry::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
 
 template size_t
-FibEntry::wireEncode<false>(EncodingImpl<false>& block) const;
+FibEntry::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
 
 const Block&
 FibEntry::wireEncode() const
diff --git a/src/management/nfd-fib-entry.hpp b/src/management/nfd-fib-entry.hpp
index 0e7bb59..f1f3003 100644
--- a/src/management/nfd-fib-entry.hpp
+++ b/src/management/nfd-fib-entry.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -67,9 +67,9 @@
   NextHopRecord&
   setCost(uint64_t cost);
 
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block) const;
+  wireEncode(EncodingImpl<TAG>& block) const;
 
   const Block&
   wireEncode() const;
@@ -132,9 +132,9 @@
     return *this;
   }
 
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block) const;
+  wireEncode(EncodingImpl<TAG>& block) const;
 
   const Block&
   wireEncode() const;
diff --git a/src/management/nfd-forwarder-status.cpp b/src/management/nfd-forwarder-status.cpp
index c4e736a..4e989ee 100644
--- a/src/management/nfd-forwarder-status.cpp
+++ b/src/management/nfd-forwarder-status.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -53,9 +53,9 @@
   this->wireDecode(payload);
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-ForwarderStatus::wireEncode(EncodingImpl<T>& encoder) const
+ForwarderStatus::wireEncode(EncodingImpl<TAG>& encoder) const
 {
   size_t totalLength = 0;
 
@@ -81,7 +81,7 @@
                                                 time::toUnixTimestamp(m_currentTimestamp).count());
   totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::StartTimestamp,
                                                 time::toUnixTimestamp(m_startTimestamp).count());
-  totalLength += prependByteArrayBlock(encoder,tlv::nfd::NfdVersion,
+  totalLength += encoder.prependByteArrayBlock(tlv::nfd::NfdVersion,
                                        reinterpret_cast<const uint8_t*>(m_nfdVersion.c_str()),
                                        m_nfdVersion.size());
 
@@ -91,10 +91,10 @@
 }
 
 template size_t
-ForwarderStatus::wireEncode<true>(EncodingImpl<true>& block) const;
+ForwarderStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
 
 template size_t
-ForwarderStatus::wireEncode<false>(EncodingImpl<false>& block) const;
+ForwarderStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
 
 const Block&
 ForwarderStatus::wireEncode() const
diff --git a/src/management/nfd-forwarder-status.hpp b/src/management/nfd-forwarder-status.hpp
index c45d403..11f59e1 100644
--- a/src/management/nfd-forwarder-status.hpp
+++ b/src/management/nfd-forwarder-status.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -55,9 +55,9 @@
    *
    *  The outermost Content element isn't part of ForwardStatus structure.
    */
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& encoder) const;
+  wireEncode(EncodingImpl<TAG>& encoder) const;
 
   /** \brief encode ForwarderStatus as a Content block
    *
diff --git a/src/management/nfd-local-control-header.hpp b/src/management/nfd-local-control-header.hpp
index 6a6803e..5f8d667 100644
--- a/src/management/nfd-local-control-header.hpp
+++ b/src/management/nfd-local-control-header.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -155,9 +155,9 @@
   }
 
 private:
-  template<bool T>
+  template<encoding::Tag TAG>
   inline size_t
-  wireEncode(EncodingImpl<T>& block, size_t payloadSize,
+  wireEncode(EncodingImpl<TAG>& block, size_t payloadSize,
              bool encodeIncomingFaceId, bool encodeNextHopFaceId) const;
 
 private:
@@ -169,9 +169,9 @@
 /**
  * @brief Fast encoding or block size estimation
  */
-template<bool T>
+template<encoding::Tag TAG>
 inline size_t
-LocalControlHeader::wireEncode(EncodingImpl<T>& block, size_t payloadSize,
+LocalControlHeader::wireEncode(EncodingImpl<TAG>& block, size_t payloadSize,
                                bool encodeIncomingFaceId, bool encodeNextHopFaceId) const
 {
   size_t totalLength = payloadSize;
diff --git a/src/management/nfd-rib-entry.cpp b/src/management/nfd-rib-entry.cpp
index 9fc7fd9..ab78d5c 100644
--- a/src/management/nfd-rib-entry.cpp
+++ b/src/management/nfd-rib-entry.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -56,9 +56,9 @@
   wireDecode(block);
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-Route::wireEncode(EncodingImpl<T>& block) const
+Route::wireEncode(EncodingImpl<TAG>& block) const
 {
   size_t totalLength = 0;
 
@@ -92,10 +92,10 @@
 }
 
 template size_t
-Route::wireEncode<true>(EncodingImpl<true>& block) const;
+Route::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
 
 template size_t
-Route::wireEncode<false>(EncodingImpl<false>& block) const;
+Route::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
 
 const Block&
 Route::wireEncode() const
@@ -216,9 +216,9 @@
 }
 
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-RibEntry::wireEncode(EncodingImpl<T>& block) const
+RibEntry::wireEncode(EncodingImpl<TAG>& block) const
 {
   size_t totalLength = 0;
 
@@ -237,10 +237,10 @@
 }
 
 template size_t
-RibEntry::wireEncode<true>(EncodingImpl<true>& block) const;
+RibEntry::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
 
 template size_t
-RibEntry::wireEncode<false>(EncodingImpl<false>& block) const;
+RibEntry::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
 
 const Block&
 RibEntry::wireEncode() const
diff --git a/src/management/nfd-rib-entry.hpp b/src/management/nfd-rib-entry.hpp
index 8fa1704..ad5fe92 100644
--- a/src/management/nfd-rib-entry.hpp
+++ b/src/management/nfd-rib-entry.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -152,9 +152,9 @@
     return m_hasInfiniteExpirationPeriod;
   }
 
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block) const;
+  wireEncode(EncodingImpl<TAG>& block) const;
 
   const Block&
   wireEncode() const;
@@ -243,9 +243,9 @@
     return *this;
   }
 
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block) const;
+  wireEncode(EncodingImpl<TAG>& block) const;
 
   const Block&
   wireEncode() const;
diff --git a/src/management/nfd-strategy-choice.cpp b/src/management/nfd-strategy-choice.cpp
index 9296954..6283acd 100644
--- a/src/management/nfd-strategy-choice.cpp
+++ b/src/management/nfd-strategy-choice.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -42,9 +42,9 @@
   this->wireDecode(payload);
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-StrategyChoice::wireEncode(EncodingImpl<T>& encoder) const
+StrategyChoice::wireEncode(EncodingImpl<TAG>& encoder) const
 {
   size_t totalLength = 0;
 
@@ -57,10 +57,10 @@
 }
 
 template size_t
-StrategyChoice::wireEncode<true>(EncodingImpl<true>& block) const;
+StrategyChoice::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
 
 template size_t
-StrategyChoice::wireEncode<false>(EncodingImpl<false>& block) const;
+StrategyChoice::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
 
 const Block&
 StrategyChoice::wireEncode() const
diff --git a/src/management/nfd-strategy-choice.hpp b/src/management/nfd-strategy-choice.hpp
index 6fc2f18..4d8e781 100644
--- a/src/management/nfd-strategy-choice.hpp
+++ b/src/management/nfd-strategy-choice.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -51,9 +51,9 @@
   explicit
   StrategyChoice(const Block& payload);
 
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& encoder) const;
+  wireEncode(EncodingImpl<TAG>& encoder) const;
 
   const Block&
   wireEncode() const;
diff --git a/src/meta-info.cpp b/src/meta-info.cpp
index a4f9938..3a70b0d 100644
--- a/src/meta-info.cpp
+++ b/src/meta-info.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -123,9 +123,9 @@
   return 0;
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-MetaInfo::wireEncode(EncodingImpl<T>& blk) const
+MetaInfo::wireEncode(EncodingImpl<TAG>& encoder) const
 {
   // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
   //                ContentType?
@@ -137,38 +137,38 @@
 
   for (std::list<Block>::const_reverse_iterator appMetaInfoItem = m_appMetaInfo.rbegin();
        appMetaInfoItem != m_appMetaInfo.rend(); ++appMetaInfoItem) {
-    totalLength += prependBlock(blk, *appMetaInfoItem);
+    totalLength += encoder.prependBlock(*appMetaInfoItem);
   }
 
   // FinalBlockId
   if (!m_finalBlockId.empty())
     {
-      totalLength += prependNestedBlock(blk, tlv::FinalBlockId, m_finalBlockId);
+      totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, m_finalBlockId);
     }
 
   // FreshnessPeriod
   if (m_freshnessPeriod >= time::milliseconds::zero())
     {
-      totalLength += prependNonNegativeIntegerBlock(blk, tlv::FreshnessPeriod,
+      totalLength += prependNonNegativeIntegerBlock(encoder, tlv::FreshnessPeriod,
                                                     m_freshnessPeriod.count());
     }
 
   // ContentType
   if (m_type != tlv::ContentType_Blob)
     {
-      totalLength += prependNonNegativeIntegerBlock(blk, tlv::ContentType, m_type);
+      totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ContentType, m_type);
     }
 
-  totalLength += blk.prependVarNumber(totalLength);
-  totalLength += blk.prependVarNumber(tlv::MetaInfo);
+  totalLength += encoder.prependVarNumber(totalLength);
+  totalLength += encoder.prependVarNumber(tlv::MetaInfo);
   return totalLength;
 }
 
 template size_t
-MetaInfo::wireEncode<true>(EncodingImpl<true>& block) const;
+MetaInfo::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
 
 template size_t
-MetaInfo::wireEncode<false>(EncodingImpl<false>& block) const;
+MetaInfo::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
 
 const Block&
 MetaInfo::wireEncode() const
diff --git a/src/meta-info.hpp b/src/meta-info.hpp
index 520a0c8..5ba4729 100644
--- a/src/meta-info.hpp
+++ b/src/meta-info.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -74,9 +74,9 @@
   explicit
   MetaInfo(const Block& block);
 
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block) const;
+  wireEncode(EncodingImpl<TAG>& block) const;
 
   const Block&
   wireEncode() const;
diff --git a/src/name-component.cpp b/src/name-component.cpp
index 8d2db29..e02ee7e 100644
--- a/src/name-component.cpp
+++ b/src/name-component.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -438,9 +438,9 @@
 }
 
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-Component::wireEncode(EncodingImpl<T>& block) const
+Component::wireEncode(EncodingImpl<TAG>& block) const
 {
   size_t totalLength = 0;
   if (value_size() > 0)
@@ -451,10 +451,10 @@
 }
 
 template size_t
-Component::wireEncode<true>(EncodingImpl<true>& block) const;
+Component::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
 
 template size_t
-Component::wireEncode<false>(EncodingImpl<false>& block) const;
+Component::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
 
 const Block&
 Component::wireEncode() const
diff --git a/src/name-component.hpp b/src/name-component.hpp
index e49275a..4e6119b 100644
--- a/src/name-component.hpp
+++ b/src/name-component.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -17,10 +17,6 @@
  * <http://www.gnu.org/licenses/>.
  *
  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- *
- * @author Jeff Thompson <jefft0@remap.ucla.edu>
- * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
- * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
  */
 
 #ifndef NDN_NAME_COMPONENT_HPP
@@ -28,6 +24,7 @@
 
 #include "common.hpp"
 #include "encoding/block.hpp"
+#include "encoding/block-helpers.hpp"
 #include "util/time.hpp"
 
 namespace ndn {
@@ -111,15 +108,18 @@
   Component(const uint8_t* buffer, size_t bufferSize);
 
   /**
-   * @brief Create a new name::Component from the buffer (data from buffer will be copied)
-   * @param begin Iterator pointing to the beginning of the buffer
-   * @param end   Iterator pointing to the ending of the buffer
+   * @brief Create a new name::Component frome the range [@p first, @p last) of bytes
+   * @param first     Iterator pointing to the beginning of the buffer
+   * @param last      Iterator pointing to the ending of the buffer
+   * @tparam Iterator iterator type satisfying at least InputIterator concept.  Implementation
+   *                  is more optimal when the iterator type satisfies RandomAccessIterator concept.
+   *                  It is required that sizeof(std::iterator_traits<Iterator>::value_type) == 1.
    *
    * This constructor will create a new tlv::NameComponent Block with `buffer` as a payload.
    * Note that this method **will** allocate new memory for and copy the payload.
    */
-  template<class InputIterator>
-  Component(InputIterator begin, InputIterator end);
+  template<class Iterator>
+  Component(Iterator first, Iterator last);
 
   /**
    * @brief Create a new name::Component from the C string (data from string will be copied)
@@ -148,9 +148,9 @@
   /**
    * @brief Fast encoding or block size estimation
    */
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block) const;
+  wireEncode(EncodingImpl<TAG>& block) const;
 
   /**
    * @brief Encode to a wire format
@@ -615,10 +615,10 @@
   return os;
 }
 
-template<class InputIterator>
+template<class Iterator>
 inline
-Component::Component(InputIterator begin, InputIterator end)
-  : Block(dataBlock(tlv::NameComponent, begin, end))
+Component::Component(Iterator first, Iterator last)
+  : Block(dataBlock(tlv::NameComponent, first, last))
 {
 }
 
diff --git a/src/name.cpp b/src/name.cpp
index afcfb18..9b49fc8 100644
--- a/src/name.cpp
+++ b/src/name.cpp
@@ -43,9 +43,9 @@
 
 const size_t Name::npos = std::numeric_limits<size_t>::max();
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-Name::wireEncode(EncodingImpl<T>& encoder) const
+Name::wireEncode(EncodingImpl<TAG>& encoder) const
 {
   size_t totalLength = 0;
 
@@ -60,10 +60,10 @@
 }
 
 template size_t
-Name::wireEncode<true>(EncodingImpl<true>& estimator) const;
+Name::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& estimator) const;
 
 template size_t
-Name::wireEncode<false>(EncodingImpl<false>& encoder) const;
+Name::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
 
 const Block&
 Name::wireEncode() const
diff --git a/src/name.hpp b/src/name.hpp
index a9716e9..bd6e02b 100644
--- a/src/name.hpp
+++ b/src/name.hpp
@@ -114,9 +114,9 @@
   /**
    * @brief Fast encoding or block size estimation
    */
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block) const;
+  wireEncode(EncodingImpl<TAG>& block) const;
 
   const Block&
   wireEncode() const;
@@ -159,14 +159,19 @@
   }
 
   /**
-   * Append a new component, copying from value of length valueLength.
+   * @brief Append a new component, copying from value frome the range [@p first, @p last) of bytes
+   * @param first     Iterator pointing to the beginning of the buffer
+   * @param last      Iterator pointing to the ending of the buffer
+   * @tparam Iterator iterator type satisfying at least InputIterator concept.  Implementation
+   *                  is more optimal when the iterator type satisfies RandomAccessIterator concept.
+   *                  It is required that sizeof(std::iterator_traits<Iterator>::value_type) == 1.
    * @return This name so that you can chain calls to append.
    */
-  template<class InputIterator>
+  template<class Iterator>
   Name&
-  append(InputIterator begin, InputIterator end)
+  append(Iterator first, Iterator last)
   {
-    m_nameBlock.push_back(Component(begin, end));
+    m_nameBlock.push_back(Component(first, last));
     return *this;
   }
 
diff --git a/src/selectors.cpp b/src/selectors.cpp
index 49676b5..ae078e3 100644
--- a/src/selectors.cpp
+++ b/src/selectors.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -56,9 +56,9 @@
          !m_mustBeFresh;
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-Selectors::wireEncode(EncodingImpl<T>& block) const
+Selectors::wireEncode(EncodingImpl<TAG>& block) const
 {
   size_t totalLength = 0;
 
@@ -110,10 +110,10 @@
 }
 
 template size_t
-Selectors::wireEncode<true>(EncodingImpl<true>& estimator) const;
+Selectors::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& estimator) const;
 
 template size_t
-Selectors::wireEncode<false>(EncodingImpl<false>& encoder) const;
+Selectors::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
 
 const Block&
 Selectors::wireEncode() const
diff --git a/src/selectors.hpp b/src/selectors.hpp
index fabb196..62b4b06 100644
--- a/src/selectors.hpp
+++ b/src/selectors.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -58,9 +58,9 @@
   /**
    * @brief Fast encoding or block size estimation
    */
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block) const;
+  wireEncode(EncodingImpl<TAG>& block) const;
 
   /**
    * @brief Encode to a wire format
diff --git a/src/signature-info.cpp b/src/signature-info.cpp
index 13e5be3..668e11f 100644
--- a/src/signature-info.cpp
+++ b/src/signature-info.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -109,9 +109,9 @@
               boost::lexical_cast<std::string>(type) + "] from SignatureInfo");
 }
 
-template<bool T>
+template<encoding::Tag TAG>
 size_t
-SignatureInfo::wireEncode(EncodingImpl<T>& block) const
+SignatureInfo::wireEncode(EncodingImpl<TAG>& block) const
 {
   size_t totalLength = 0;
 
@@ -131,10 +131,10 @@
 }
 
 template size_t
-SignatureInfo::wireEncode<true>(EncodingImpl<true>& block) const;
+SignatureInfo::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
 
 template size_t
-SignatureInfo::wireEncode<false>(EncodingImpl<false>& block) const;
+SignatureInfo::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
 
 
 const Block&
diff --git a/src/signature-info.hpp b/src/signature-info.hpp
index 5dcdcff..9e0f063 100644
--- a/src/signature-info.hpp
+++ b/src/signature-info.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -103,9 +103,9 @@
   getTypeSpecificTlv(uint32_t type) const;
 
   /// @brief Encode to a wire format or estimate wire format
-  template<bool T>
+  template<encoding::Tag TAG>
   size_t
-  wireEncode(EncodingImpl<T>& block) const;
+  wireEncode(EncodingImpl<TAG>& block) const;
 
   /// @brief Encode to a wire format
   const Block&