key-locator.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_KEY_LOCATOR_HPP
23 #define NDN_KEY_LOCATOR_HPP
24 
25 #include <vector>
26 #include "c/key-types.h"
27 #include "name.hpp"
28 #include "util/change-counter.hpp"
29 #include "lite/key-locator-lite.hpp"
30 
31 namespace ndn {
32 
33 class Signature;
34 
35 class KeyLocator {
36 public:
37  KeyLocator()
38  : type_((ndn_KeyLocatorType)-1), changeCount_(0)
39  {
40  }
41 
45  void
47  {
48  type_ = (ndn_KeyLocatorType)-1;
49  keyData_.reset();
50  setKeyName(Name());
51  ++changeCount_;
52  }
53 
62  void
63  get(KeyLocatorLite& keyLocatorLite) const;
64 
69  void
70  set(const KeyLocatorLite& keyLocatorLite);
71 
72  ndn_KeyLocatorType
73  getType() const { return type_; }
74 
75  const Blob&
76  getKeyData() const { return keyData_; }
77 
78  const Name&
79  getKeyName() const { return keyName_.get(); }
80 
81  Name&
82  getKeyName() { return keyName_.get(); }
83 
84  void
85  setType(ndn_KeyLocatorType type)
86  {
87  type_ = type;
88  ++changeCount_;
89  }
90 
91  void
92  setKeyData(const Blob& keyData)
93  {
94  keyData_ = keyData;
95  ++changeCount_;
96  }
97 
98  void
99  setKeyName(const Name &keyName)
100  {
101  keyName_.set(keyName);
102  ++changeCount_;
103  }
104 
110  bool
111  equals(const KeyLocator& other) const;
112 
118  bool
119  operator == (const KeyLocator& other) const { return equals(other); }
120 
126  bool
127  operator != (const KeyLocator& other) const { return !equals(other); }
128 
140  static bool
141  canGetFromSignature(const Signature* signature);
142 
150  static const KeyLocator&
151  getFromSignature(const Signature* signature);
152 
157  uint64_t
159  {
160  if (keyName_.checkChanged())
161  // A child object has changed, so update the change count.
162  // This method can be called on a const object, but we want to be able to update the changeCount_.
163  ++const_cast<KeyLocator*>(this)->changeCount_;
164 
165  return changeCount_;
166  }
167 
168 private:
169  ndn_KeyLocatorType type_;
170  Blob keyData_;
173  ChangeCounter<Name> keyName_;
174  uint64_t changeCount_;
175 };
176 
177 }
178 
179 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:36
static bool canGetFromSignature(const Signature *signature)
If the signature is a type that has a KeyLocator (so that getFromSignature will succeed), return true.
Definition: key-locator.cpp:53
bool operator==(const KeyLocator &other) const
Check if this is the same key locator as other.
Definition: key-locator.hpp:119
static const KeyLocator & getFromSignature(const Signature *signature)
If the signature is a type that has a KeyLocator, then return it.
Definition: key-locator.cpp:61
bool operator!=(const KeyLocator &other) const
Check if this is not the same key locator as other.
Definition: key-locator.hpp:127
A ChangeCounter keeps a target object whose change count is tracked by a local change count...
Definition: change-counter.hpp:37
A KeyLocatorLite holds a type and other info to represent the key which signs a Data packet...
Definition: key-locator-lite.hpp:34
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
A Signature is an abstract base class providing methods to work with the signature information in a D...
Definition: signature.hpp:35
A Blob holds a pointer to an immutable byte array implemented as const std::vector.
Definition: blob.hpp:42
void clear()
Clear the keyData and set the type to none.
Definition: key-locator.hpp:46
bool equals(const KeyLocator &other) const
Check if this is the same key locator as other.
Definition: key-locator.cpp:87
Definition: key-locator.hpp:35
uint64_t getChangeCount() const
Get the change count, which is incremented each time this object (or a child object) is changed...
Definition: key-locator.hpp:158
void set(const KeyLocatorLite &keyLocatorLite)
Clear this key locator, and set the values by copying from keyLocatorLite.
Definition: key-locator.cpp:42