MRPT  1.9.9
CObservable.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <set>
12 
13 namespace mrpt::system
14 {
15 class CObserver;
16 class mrptEvent;
17 
18 /** Inherit from this class for those objects capable of being observed by a
19  * CObserver class.
20  *
21  * The only thing to do in your child class is to call
22  * CObservable::publishEvent() whenever needed and all the
23  * observer classes will be notified.
24  *
25  * \note The pairs CObservable / CObserver automatically notify each other the
26  * destruction of any of them, effectively ending the subscription of events.
27  *
28  * \sa CObserver, mrptEvent
29  * \ingroup mrpt_system_grp
30  */
32 {
33  friend class CObserver;
34 
35  public:
36  CObservable();
37  virtual ~CObservable();
38 
39  private:
40  std::set<CObserver*> m_subscribers;
43 
44  protected:
45  /** Called when you want this object to emit an event to all the observers
46  * currently subscribed to this object. */
47  void publishEvent(const mrptEvent& e) const;
48 
49  /** Can be called by a derived class before preparing an event for
50  * publishing with \a publishEvent to determine if there is no one
51  * subscribed, so it can save the wasted time preparing an event that will
52  * be not read. */
53  inline bool hasSubscribers() const { return !m_subscribers.empty(); }
54 }; // End of class def.
55 
56 } // namespace mrpt::system
void internal_observer_end(CObserver *)
Definition: CObservable.cpp:43
std::set< CObserver * > m_subscribers
Definition: CObservable.h:40
The basic event type for the observer-observable pattern in MRPT.
Definition: mrptEvent.h:31
Inherit from this class to get notified about events from any CObservable object after subscribing to...
Definition: CObserver.h:34
Inherit from this class for those objects capable of being observed by a CObserver class...
Definition: CObservable.h:31
void internal_observer_begin(CObserver *)
Definition: CObservable.cpp:38
bool hasSubscribers() const
Can be called by a derived class before preparing an event for publishing with publishEvent to determ...
Definition: CObservable.h:53
void publishEvent(const mrptEvent &e) const
Called when you want this object to emit an event to all the observers currently subscribed to this o...
Definition: CObservable.cpp:57



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020