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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019