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 
89  bool
90  checkChanged() const
91  {
92  uint64_t targetChangeCount = target_.getChangeCount();
93  if (changeCount_ != targetChangeCount) {
94  // This method can be called on a const object, but we want to be able to update the changeCount_.
95  const_cast<ChangeCounter<T>*>(this)->changeCount_ = targetChangeCount;
96  return true;
97  }
98  else
99  return false;
100  }
101 
102 private:
103  T target_;
104  uint64_t changeCount_;
105 };
106 
114 template<class T>
116 public:
122  {
123  changeCount_ = 0;
124  }
125 
131  : target_(target)
132  {
133  if (target_)
134  changeCount_ = target_->getChangeCount();
135  else
136  changeCount_ = 0;
137  }
138 
143  const T*
144  get() const { return target_.get(); }
145 
150  T*
151  get() { return target_.get(); }
152 
157  void
158  set(const ptr_lib::shared_ptr<T>& target)
159  {
160  target_ = target;
161  if (target_)
162  changeCount_ = target_->getChangeCount();
163  else
164  changeCount_ = 0;
165  }
166 
174  bool
175  checkChanged() const
176  {
177  if (!target_)
178  return false;
179 
180  uint64_t targetChangeCount = target_->getChangeCount();
181  if (changeCount_ != targetChangeCount) {
182  // This method can be called on a const object, but we want to be able to update the changeCount_.
183  const_cast<SharedPointerChangeCounter<T>*>(this)->changeCount_ = targetChangeCount;
184  return true;
185  }
186  else
187  return false;
188  }
189 
190 private:
191  ptr_lib::shared_ptr<T> target_;
192  uint64_t changeCount_;
193 };
194 
195 }
196 
197 #endif
Copyright (C) 2013-2015 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:121
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:158
A ChangeCounter keeps a shared_ptr to a target object whose change count is tracked by a local change...
Definition: change-counter.hpp:115
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:175
SharedPointerChangeCounter(T *target)
Create a new SharedPointerChangeCounter with the given target.
Definition: change-counter.hpp:130
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:90