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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019