key-locator: KeyDigest

refs #1426

Change-Id: Ib4de0c7348786879775e52c803f6a2eaeaa2100c
diff --git a/src/key-locator.hpp b/src/key-locator.hpp
index a86275d..2528912 100644
--- a/src/key-locator.hpp
+++ b/src/key-locator.hpp
@@ -22,9 +22,7 @@
 #ifndef NDN_KEY_LOCATOR_HPP
 #define NDN_KEY_LOCATOR_HPP
 
-#include "encoding/block.hpp"
 #include "encoding/encoding-buffer.hpp"
-
 #include "name.hpp"
 
 namespace ndn {
@@ -42,174 +40,128 @@
     }
   };
 
-  enum {
-    KeyLocator_None = 65535, // just an arbitrarily large number (used only internally)
+  enum Type {
+    /** \brief indicates KeyLocator is empty (internal use only)
+     */
+    KeyLocator_None = 65535,
+    /** \brief indicates KeyLocator contains a Name
+     */
     KeyLocator_Name = 0,
-
+    /** \brief indicates KeyLocator contains a KeyDigest
+     */
+    KeyLocator_KeyDigest = 1,
+    /** \brief indicates KeyLocator contains an unknown element
+     */
     KeyLocator_Unknown = 255
   };
 
-  KeyLocator()
-    : m_type(KeyLocator_None)
-  {
-  }
+public: // constructors
+  /** \brief construct an empty KeyLocator
+   */
+  KeyLocator();
 
-  KeyLocator(const Name& name)
-  {
-    setName(name);
-  }
-
-  /**
-   * @brief Create from wire encoding
+  /** \brief construct from wire encoding
    */
   explicit
-  KeyLocator(const Block& wire)
-  {
-    wireDecode(wire);
-  }
+  KeyLocator(const Block& wire);
 
-  ///////////////////////////////////////////////////////////////////////////////
+  /** \brief construct from Name
+   *  \note implicit conversion is permitted
+   */
+  KeyLocator(const Name& name);
 
+public: // encode and decode
+  /** \brief prepend wire encoding
+   *  \param block EncodingBuffer or Estimator
+   */
   template<bool T>
   size_t
   wireEncode(EncodingImpl<T>& block) const;
 
+  /** \return wire encoding
+   */
   const Block&
   wireEncode() const;
 
+  /** \brief decode from wire encoding
+   *  \throw Error outer TLV type is not KeyLocator
+   *  \note No error is thrown for unrecognized inner TLV, but type becomes KeyLocator_Unknown.
+   */
   void
   wireDecode(const Block& wire);
 
-  ///////////////////////////////////////////////////////////////////////////////
-
+public: // attributes
   bool
   empty() const
   {
     return m_type == KeyLocator_None;
   }
 
-  uint32_t
-  getType() const { return m_type; }
+  Type
+  getType() const
+  {
+    return m_type;
+  }
 
-  ////////////////////////////////////////////////////////
-  // Helper methods for different types of key locators
-  //
-  // For now only Name type is actually supported
+  /** \brief clear KeyLocator
+   *  \details type becomes KeyLocator_None
+   *  \return self
+   */
+  KeyLocator&
+  clear();
 
+  /** \brief get Name element
+   *  \throw Error if type is not KeyLocator_Name
+   */
   const Name&
   getName() const;
 
-  void
+  /** \brief set Name element
+   *  \details type becomes KeyLocator_Name
+   *  \return self
+   */
+  KeyLocator&
   setName(const Name& name);
 
+  /** \brief get KeyDigest element
+   *  \throw Error if type is not KeyLocator_KeyDigest
+   */
+  const Block&
+  getKeyDigest() const;
+
+  /** \brief set KeyDigest element
+   *  \details type becomes KeyLocator_KeyDigest
+   *  \throw Error if Block type is not KeyDigest
+   *  \return self
+   */
+  KeyLocator&
+  setKeyDigest(const Block& keyDigest);
+
+  /** \brief set KeyDigest value
+   *  \details type becomes KeyLocator_KeyDigest
+   *  \return self
+   */
+  KeyLocator&
+  setKeyDigest(const ConstBufferPtr& keyDigest);
+
 public: // EqualityComparable concept
   bool
   operator==(const KeyLocator& other) const;
 
   bool
-  operator!=(const KeyLocator& other) const;
+  operator!=(const KeyLocator& other) const
+  {
+    return !this->operator==(other);
+  }
 
 private:
-  uint32_t m_type;
+  Type m_type;
   Name m_name;
+  Block m_keyDigest;
 
   mutable Block m_wire;
 };
 
-template<bool T>
-inline size_t
-KeyLocator::wireEncode(EncodingImpl<T>& block) const
-{
-  // KeyLocator ::= KEY-LOCATOR-TYPE TLV-LENGTH KeyLocatorValue
-
-  // KeyLocatorValue ::= Name |
-  //                     KeyLocatorDigest |     (not supported yet)
-  //                     ...
-
-  // KeyLocatorDigest ::= KEY-LOCATOR-DIGEST-TYPE TLV-LENGTH BYTE+
-
-  size_t totalLength = 0;
-
-  switch (m_type) {
-  case KeyLocator_None:
-    break;
-  case KeyLocator_Name:
-    totalLength += m_name.wireEncode(block);
-    break;
-  default:
-    throw Error("Unsupported KeyLocator type");
-  }
-
-  totalLength += block.prependVarNumber(totalLength);
-  totalLength += block.prependVarNumber(tlv::KeyLocator);
-  return totalLength;
-}
-
-inline const Block&
-KeyLocator::wireEncode() const
-{
-  if (m_wire.hasWire())
-    return m_wire;
-
-  EncodingEstimator estimator;
-  size_t estimatedSize = wireEncode(estimator);
-
-  EncodingBuffer buffer(estimatedSize, 0);
-  wireEncode(buffer);
-
-  m_wire = buffer.block();
-  return m_wire;
-}
-
-inline void
-KeyLocator::wireDecode(const Block& value)
-{
-  if (value.type() != tlv::KeyLocator)
-    throw Error("Unexpected TLV type during KeyLocator decoding");
-
-  m_wire = value;
-  m_wire.parse();
-
-  if (!m_wire.elements().empty() && m_wire.elements_begin()->type() == tlv::Name)
-    {
-      m_type = KeyLocator_Name;
-      m_name.wireDecode(*m_wire.elements_begin());
-    }
-  else
-    {
-      m_type = KeyLocator_Unknown;
-    }
-}
-
-inline const Name&
-KeyLocator::getName() const
-{
-  if (m_type != KeyLocator_Name)
-    throw Error("Requested Name, but KeyLocator is not of the Name type");
-
-  return m_name;
-}
-
-inline void
-KeyLocator::setName(const Name& name)
-{
-  m_wire.reset();
-  m_type = KeyLocator_Name;
-  m_name = name;
-}
-
-inline bool
-KeyLocator::operator==(const KeyLocator& other) const
-{
-  return wireEncode() == other.wireEncode();
-}
-
-inline bool
-KeyLocator::operator!=(const KeyLocator& other) const
-{
-  return !this->operator==(other);
-}
-
 } // namespace ndn
 
-#endif
+#endif // NDN_KEY_LOCATOR_HPP