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-2022 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 #include "ndn-cxx/util/variant.hpp"
27 
28 namespace ndn {
29 
31 {
32 public:
33  class Error : public tlv::Error
34  {
35  public:
36  using tlv::Error::Error;
37  };
38 
39 public: // constructors
44 
49  KeyLocator(const Name& name);
50 
53  explicit
54  KeyLocator(const Block& wire);
55 
56 public: // encode and decode
59  template<encoding::Tag TAG>
60  size_t
61  wireEncode(EncodingImpl<TAG>& encoder) const;
62 
63  const Block&
64  wireEncode() const;
65 
70  void
71  wireDecode(const Block& wire);
72 
73 public: // attributes
75  empty() const
76  {
77  return holds_alternative<monostate>(m_locator);
78  }
79 
80  uint32_t
81  getType() const;
82 
88  KeyLocator&
89  clear();
90 
94  const Name&
95  getName() const;
96 
101  KeyLocator&
102  setName(const Name& name);
103 
107  const Block&
108  getKeyDigest() const;
109 
115  KeyLocator&
116  setKeyDigest(const Block& keyDigest);
117 
123  KeyLocator&
124  setKeyDigest(const ConstBufferPtr& keyDigest);
125 
126 private: // non-member operators
127  // NOTE: the following "hidden friend" operators are available via
128  // argument-dependent lookup only and must be defined inline.
129 
130  friend bool
131  operator==(const KeyLocator& lhs, const KeyLocator& rhs)
132  {
133  return lhs.m_locator == rhs.m_locator;
134  }
135 
136  friend bool
137  operator!=(const KeyLocator& lhs, const KeyLocator& rhs)
138  {
139  return lhs.m_locator != rhs.m_locator;
140  }
141 
142 private:
143  // - monostate represents an empty KeyLocator, without any nested TLVs
144  // - Name is used when the nested TLV contains a name
145  // - Block is used when the nested TLV is a KeyDigest
146  // - in all other (unsupported) cases the nested TLV type is stored as uint32_t
147  variant<monostate, Name, Block, uint32_t> m_locator;
148 
149  mutable Block m_wire;
150 
151  friend std::ostream& operator<<(std::ostream&, const KeyLocator&);
152 };
153 
155 
156 std::ostream&
157 operator<<(std::ostream& os, const KeyLocator& keyLocator);
158 
159 } // namespace ndn
160 
161 #endif // NDN_CXX_KEY_LOCATOR_HPP
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
const Block & getKeyDigest() const
Get nested KeyDigest element.
friend bool operator!=(const KeyLocator &lhs, const KeyLocator &rhs)
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:75
uint32_t getType() const
void wireDecode(const Block &wire)
Decode from wire encoding.
Definition: key-locator.cpp:91
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.
friend std::ostream & operator<<(std::ostream &, const KeyLocator &)
bool empty() const
Definition: key-locator.hpp:75
Represents an absolute name.
Definition: name.hpp:44
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
@ KeyLocator
Definition: tlv.hpp:101
Definition: data.cpp:25
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:374