MRPT  2.0.4
CObserver.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 <mrpt/system/mrptEvent.h>
12 #include <set>
13 
14 namespace mrpt::system
15 {
16 class CObservable;
17 
18 /** Inherit from this class to get notified about events from any CObservable
19  * object after subscribing to it.
20  *
21  * The main methods in this class are:
22  * - observeBegin(): To be called to start listening at a given object.
23  * - OnEvent(): Virtual functions to be implemented in your child class to
24  * receive all the notifications.
25  *
26  * Note that if custom (child) mrptEvent classes are used, you can tell
27  * between them in runtime with "dynamic_cast<>()".
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  * \ingroup mrpt_system_grp
32  * \sa CObservable, mrptEvent
33  */
34 class CObserver
35 {
36  friend class CObservable;
37 
38  public:
39  CObserver();
40  virtual ~CObserver();
41 
42  /** Starts the subscription of this observer to the given object. \sa
43  * observeEnd */
44  void observeBegin(CObservable& obj);
45 
46  /** Ends the subscription of this observer to the given object (note that
47  there is no need to call this method, since the destruction of the first
48  of observer/observed will put an end to the process
49  \sa observeBegin */
50  void observeEnd(CObservable& obj);
51 
52  private:
53  std::set<CObservable*> m_subscribed;
54  void internal_on_event(const mrptEvent& e);
55 
56  protected:
57  /** This virtual function will be called upon receive of any event after
58  * starting listening at any CObservable object.
59  */
60  virtual void OnEvent(const mrptEvent& e) = 0;
61 
62 }; // End of class def.
63 
64 } // namespace mrpt::system
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
std::set< CObservable * > m_subscribed
Definition: CObserver.h:53
void observeEnd(CObservable &obj)
Ends the subscription of this observer to the given object (note that there is no need to call this m...
Definition: CObserver.cpp:36
void internal_on_event(const mrptEvent &e)
Definition: CObserver.cpp:47
Inherit from this class for those objects capable of being observed by a CObserver class...
Definition: CObservable.h:31
virtual void OnEvent(const mrptEvent &e)=0
This virtual function will be called upon receive of any event after starting listening at any CObser...
void observeBegin(CObservable &obj)
Starts the subscription of this observer to the given object.
Definition: CObserver.cpp:26



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020