Main MRPT website > C++ reference for MRPT 1.5.6
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 <mrpt/base/link_pragmas.h>
14 #include <set>
15 
16 namespace mrpt
17 {
18  namespace utils
19  {
20  class CObserver;
21  class mrptEvent;
22 
23  /** Inherit from this class for those objects capable of being observed by a CObserver class.
24  *
25  * The only thing to do in your child class is to call CObservable::publishEvent() whenever needed and all the
26  * observer classes will be notified.
27  *
28  * \note The pairs CObservable / CObserver automatically notify each other the destruction of any of them, effectively ending the subscription of events.
29  *
30  * \sa CObserver, mrptEvent
31  * \ingroup mrpt_base_grp
32  */
34  {
35  friend class CObserver;
36 
37  public:
38  CObservable();
39  virtual ~CObservable();
40 
41  private:
42  std::set<CObserver*> m_subscribers;
43  void internal_observer_begin(CObserver*);
44  void internal_observer_end(CObserver*);
45 
46  protected:
47  /** Called when you want this object to emit an event to all the observers currently subscribed to this object. */
48  void publishEvent(const mrptEvent &e) const;
49 
50  /** Can be called by a derived class before preparing an event for 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 be not read. */
52  inline bool hasSubscribers() const { return !m_subscribers.empty(); }
53 
54  }; // End of class def.
55 
56  } // End of namespace
57 } // end of namespace
58 #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:52
std::set< CObserver * > m_subscribers
Definition: CObservable.h:42
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:33



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018