change-counter.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_CHANGE_COUNTER_HPP
23 #define NDN_CHANGE_COUNTER_HPP
24 
25 #include "../common.hpp"
26 
27 namespace ndn {
28 
36 template<class T>
38 public:
43  {
44  changeCount_ = target_.getChangeCount();
45  }
46 
52  ChangeCounter(const T& target)
53  : target_(target)
54  {
55  changeCount_ = target_.getChangeCount();
56  }
57 
62  const T&
63  get() const { return target_; }
64 
69  T&
70  get() { return target_; }
71 
76  void
77  set(const T& target)
78  {
79  target_ = target;
80  changeCount_ = target_.getChangeCount();
81  }
82 
91  bool
92  checkChanged() const
93  {
94  uint64_t targetChangeCount = target_.getChangeCount();
95  if (changeCount_ != targetChangeCount) {
96  // This method can be called on a const object, but we want to be able to update the changeCount_.
97  const_cast<ChangeCounter<T>*>(this)->changeCount_ = targetChangeCount;
98  return true;
99  }
100  else
101  return false;
102  }
103 
104 private:
105  T target_;
106  uint64_t changeCount_;
107 };
108 
116 template<class T>
118 public:
124  {
125  changeCount_ = 0;
126  }
127 
133  : target_(target)
134  {
135  if (target_)
136  changeCount_ = target_->getChangeCount();
137  else
138  changeCount_ = 0;
139  }
140 
145  const T*
146  get() const { return target_.get(); }
147 
152  T*
153  get() { return target_.get(); }
154 
159  void
160  set(const ptr_lib::shared_ptr<T>& target)
161  {
162  target_ = target;
163  if (target_)
164  changeCount_ = target_->getChangeCount();
165  else
166  changeCount_ = 0;
167  }
168 
176  bool
177  checkChanged() const
178  {
179  if (!target_)
180  return false;
181 
182  uint64_t targetChangeCount = target_->getChangeCount();
183  if (changeCount_ != targetChangeCount) {
184  // This method can be called on a const object, but we want to be able to update the changeCount_.
185  const_cast<SharedPointerChangeCounter<T>*>(this)->changeCount_ = targetChangeCount;
186  return true;
187  }
188  else
189  return false;
190  }
191 
192 private:
193  ptr_lib::shared_ptr<T> target_;
194  uint64_t changeCount_;
195 };
196 
197 }
198 
199 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
A ChangeCounter keeps a target object whose change count is tracked by a local change count...
Definition: change-counter.hpp:37
SharedPointerChangeCounter()
Create a new SharedPointerChangeCounter with a default a null shared_ptr for the target.
Definition: change-counter.hpp:123
void set(const T &target)
Set the target to the given target.
Definition: change-counter.hpp:77
ChangeCounter()
Create a new ChangeCounter with a default value for the target.
Definition: change-counter.hpp:42
ChangeCounter(const T &target)
Create a new ChangeCounter, calling the copy constructor on T with the given target.
Definition: change-counter.hpp:52
void set(const ptr_lib::shared_ptr< T > &target)
Set the shared_ptr to the target to the given value.
Definition: change-counter.hpp:160
A ChangeCounter keeps a shared_ptr to a target object whose change count is tracked by a local change...
Definition: change-counter.hpp:117
bool checkChanged() const
If the target's change count is different than the local change count, then update the local change c...
Definition: change-counter.hpp:177
SharedPointerChangeCounter(T *target)
Create a new SharedPointerChangeCounter with the given target.
Definition: change-counter.hpp:132
bool checkChanged() const
If the target's change count is different than the local change count, then update the local change c...
Definition: change-counter.hpp:92