name: ensure Name is a RandomAccessRange

refs #2425

Change-Id: I9ef5fdadb81281e9a82dc8ff1654cc7cbfe20508
diff --git a/src/name.cpp b/src/name.cpp
index 58b1cc3..6dc735f 100644
--- a/src/name.cpp
+++ b/src/name.cpp
@@ -32,6 +32,8 @@
 #include <sstream>
 #include <boost/algorithm/string/trim.hpp>
 #include <boost/functional/hash.hpp>
+#include <boost/range/adaptor/reversed.hpp>
+#include <boost/range/concepts.hpp>
 
 namespace ndn {
 
@@ -39,6 +41,11 @@
 BOOST_CONCEPT_ASSERT((WireEncodable<Name>));
 BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Name>));
 BOOST_CONCEPT_ASSERT((WireDecodable<Name>));
+BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::iterator>));
+BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_iterator>));
+BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::reverse_iterator>));
+BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_reverse_iterator>));
+BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<Name>));
 static_assert(std::is_base_of<tlv::Error, Name::Error>::value,
               "Name::Error must inherit from tlv::Error");
 
@@ -52,8 +59,8 @@
 }
 
 Name::Name(const Block& wire)
+  : m_wire(wire)
 {
-  m_wire = wire;
   m_wire.parse();
 }
 
@@ -124,9 +131,8 @@
 Name::wireEncode(EncodingImpl<TAG>& encoder) const
 {
   size_t totalLength = 0;
-
-  for (const_reverse_iterator i = rbegin(); i != rend(); ++i) {
-    totalLength += i->wireEncode(encoder);
+  for (const Component& comp : *this | boost::adaptors::reversed) {
+    totalLength += comp.wireEncode(encoder);
   }
 
   totalLength += encoder.prependVarNumber(totalLength);
diff --git a/src/name.hpp b/src/name.hpp
index 1760def..764bdac 100644
--- a/src/name.hpp
+++ b/src/name.hpp
@@ -27,7 +27,7 @@
 #define NDN_NAME_HPP
 
 #include "name-component.hpp"
-#include <boost/iterator/reverse_iterator.hpp>
+#include <iterator>
 
 namespace ndn {
 
@@ -59,13 +59,13 @@
   using value_type             = Component;
   using allocator_type         = void;
   using reference              = Component&;
-  using const_reference        = const Component;
+  using const_reference        = const Component&;
   using pointer                = Component*;
   using const_pointer          = const Component*;
-  using iterator               = Component*;
+  using iterator               = const Component*; // disallow modifying via iterator
   using const_iterator         = const Component*;
-  using reverse_iterator       = boost::reverse_iterator<iterator>;
-  using const_reverse_iterator = boost::reverse_iterator<const_iterator>;
+  using reverse_iterator       = std::reverse_iterator<iterator>;
+  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
   using difference_type        = component_container::difference_type;
   using size_type              = component_container::size_type;
 
@@ -534,7 +534,7 @@
           const Name& other, size_t pos2 = 0, size_t count2 = npos) const;
 
 public:
-  /** \brief indicates "until the end" in getSubName and compare
+  /** @brief indicates "until the end" in getSubName and compare
    */
   static const size_t npos;