key-locator.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_CXX_KEY_LOCATOR_HPP
23 #define NDN_CXX_KEY_LOCATOR_HPP
24 
25 #include "ndn-cxx/name.hpp"
26 
27 #include <variant>
28 
29 namespace ndn {
30 
31 class KeyLocator : private boost::equality_comparable<KeyLocator>
32 {
33 public:
34  class Error : public tlv::Error
35  {
36  public:
37  using tlv::Error::Error;
38  };
39 
40 public: // constructors
45 
50  KeyLocator(const Name& name);
51 
54  explicit
55  KeyLocator(const Block& wire);
56 
57 public: // encode and decode
60  template<encoding::Tag TAG>
61  size_t
62  wireEncode(EncodingImpl<TAG>& encoder) const;
63 
64  const Block&
65  wireEncode() const;
66 
71  void
72  wireDecode(const Block& wire);
73 
74 public: // attributes
75  [[nodiscard]] bool
76  empty() const
77  {
78  return std::holds_alternative<std::monostate>(m_locator);
79  }
80 
81  uint32_t
82  getType() const;
83 
89  KeyLocator&
90  clear();
91 
95  const Name&
96  getName() const;
97 
102  KeyLocator&
103  setName(const Name& name);
104 
108  const Block&
109  getKeyDigest() const;
110 
116  KeyLocator&
117  setKeyDigest(const Block& keyDigest);
118 
124  KeyLocator&
125  setKeyDigest(const ConstBufferPtr& keyDigest);
126 
127 private:
128  void
129  print(std::ostream& os) const;
130 
131 private: // non-member operators
132  // NOTE: the following "hidden friend" operators are available via
133  // argument-dependent lookup only and must be defined inline.
134  // boost::equality_comparable provides != operator.
135 
136  friend bool
137  operator==(const KeyLocator& lhs, const KeyLocator& rhs)
138  {
139  return lhs.m_locator == rhs.m_locator;
140  }
141 
142  friend std::ostream&
143  operator<<(std::ostream& os, const KeyLocator& kl)
144  {
145  kl.print(os);
146  return os;
147  }
148 
149 private:
150  // - monostate represents an empty KeyLocator, without any nested TLVs
151  // - Name is used when the nested TLV contains a name
152  // - Block is used when the nested TLV is a KeyDigest
153  // - in all other (unsupported) cases the nested TLV type is stored as uint32_t
154  std::variant<std::monostate, Name, Block, uint32_t> m_locator;
155 
156  mutable Block m_wire;
157 };
158 
160 
161 } // namespace ndn
162 
163 #endif // NDN_CXX_KEY_LOCATOR_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
const Block & getKeyDigest() const
Get nested KeyDigest element.
const Name & getName() const
Get nested Name element.
friend bool operator==(const KeyLocator &lhs, const KeyLocator &rhs)
KeyLocator()
Construct an empty KeyLocator.
const Block & wireEncode() const
Definition: key-locator.cpp:68
friend std::ostream & operator<<(std::ostream &os, const KeyLocator &kl)
uint32_t getType() const
void wireDecode(const Block &wire)
Decode from wire encoding.
Definition: key-locator.cpp:84
KeyLocator & setKeyDigest(const Block &keyDigest)
Set nested KeyDigest element (whole TLV).
KeyLocator & clear()
Reset KeyLocator to its default-constructed state.
KeyLocator & setName(const Name &name)
Set nested Name element.
bool empty() const
Definition: key-locator.hpp:76
Represents an absolute name.
Definition: name.hpp:45
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:28
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
@ KeyLocator
Definition: tlv.hpp:101
Definition: data.cpp:25
std::shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:140