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
mrpt::system::mrptEvent
The basic event type for the observer-observable pattern in MRPT.
Definition: mrptEvent.h:33
mrpt::system::CObservable::~CObservable
virtual ~CObservable()
Definition: CObservable.cpp:20
mrpt::system::CObservable::publishEvent
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
mrpt::system::CObservable::internal_observer_begin
void internal_observer_begin(CObserver *)
Definition: CObservable.cpp:29
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::system::CObservable::CObservable
CObservable()
Definition: CObservable.cpp:19
mrpt::system::CObservable::hasSubscribers
bool hasSubscribers() const
Can be called by a derived class before preparing an event for publishing with publishEvent to determ...
Definition: CObservable.h:55
mrpt::system::CObservable::internal_observer_end
void internal_observer_end(CObserver *)
Definition: CObservable.cpp:34
mrpt::system::CObserver
Inherit from this class to get notified about events from any CObservable object after subscribing to...
Definition: CObserver.h:36
mrpt::system::CObservable
Inherit from this class for those objects capable of being observed by a CObserver class.
Definition: CObservable.h:33
mrpt::system::CObservable::m_subscribers
std::set< CObserver * > m_subscribers
Definition: CObservable.h:42



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST