Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
notifier.cc
Go to the documentation of this file.
1 
18 #include "ion/base/notifier.h"
19 
20 namespace ion {
21 namespace base {
22 
24 
26  if (receiver) {
27  NotifierPtr ptr(receiver);
28  WriteLock lock(&mutex_);
29  WriteGuard guard(&lock);
30  const size_t count = receivers_.size();
31  for (size_t i = 0; i < count; ++i)
32  if (ptr == receivers_[i])
33  return;
34  receivers_.push_back(ptr);
35  }
36 }
37 
39  if (receiver) {
40  WriteLock lock(&mutex_);
41  WriteGuard guard(&lock);
42  const size_t count = receivers_.size();
43  if (receiver->GetRefCount()) {
44  NotifierPtr ptr(receiver);
45  for (size_t i = 0; i < count; ++i) {
46  if (receivers_[i] == ptr) {
47  receivers_[i] = receivers_[receivers_.size() - 1U];
48  receivers_.pop_back();
49  break;
50  }
51  }
52  } else {
58  for (size_t i = 0; i < receivers_.size();) {
59  if (receivers_[i].GetUnderlyingRefCountUnsynchronized()) {
60  ++i;
61  } else {
62  receivers_[i] = receivers_[receivers_.size() - 1U];
63  receivers_.pop_back();
64  break;
65  }
66  }
67  }
68  }
69 }
70 
72  ReadLock lock(&mutex_);
73  ReadGuard guard(&lock);
74  return receivers_.size();
75 }
76 
78  return receivers_;
79 }
80 
81 void Notifier::Notify() const {
82  ReadLock lock(&mutex_);
83  ReadGuard guard(&lock);
84  for (size_t i = 0; i < receivers_.size();) {
85  ReferentPtr<Notifier>::Type receiver = receivers_[i].Acquire();
86  if (receiver.Get()) {
87  receiver->OnNotify(this);
88  ++i;
89  } else {
90  receivers_[i] = receivers_[receivers_.size() - 1U];
91  receivers_.pop_back();
92  }
93  }
94 }
95 
96 void Notifier::OnNotify(const Notifier* notifier) {}
97 
98 } // namespace base
99 } // namespace ion
void AddReceiver(Notifier *receiver)
Adds a Notifier to be notified.
Definition: notifier.cc:25
A Notifier both sends notifications to and receives notifications from other Notifiers.
Definition: notifier.h:35
void Notify() const
Notifies all contained Notifiers by calling their OnNotify().
Definition: notifier.cc:81
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
size_t GetReceiverCount() const
Returns the number of Notifiers that will be notified.
Definition: notifier.cc:71
A LockGuard locks a mutex when created, and unlocks it when destroyed.
Definition: lockguards.h:90
int GetRefCount() const
GetRefCount() is part of the interface necessary for SharedPtr.
Definition: shareable.h:34
~Notifier() override
The destructor is protected because this is derived from Referent.
Definition: notifier.cc:23
void RemoveReceiver(Notifier *receiver)
Removes a Notifier to be notified.
Definition: notifier.cc:38
A WriteLock obtains a write lock, but has a similar interface to a Mutex and can be used with a Write...
virtual void OnNotify(const Notifier *notifier)
Subclasses can override this to provide custom behavior on notifications.
Definition: notifier.cc:96
const NotifierPtrVector & GetReceivers() const
Returns the set of Notifiers that will be notified.
Definition: notifier.cc:77
A ReadLock obtains a read lock, but has a similar interface to a Mutex and can be used with a ReadGua...
Definition: readwritelock.h:86
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
A WeakReferentPtr is a weak reference to an instance of some class derived from Referent.
Definition: weakreferent.h:182