MRPT  2.0.3
mrptEvent.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/core/Clock.h>
12 
13 namespace mrpt::system
14 {
15 class CObservable;
16 
17 /** The basic event type for the observer-observable pattern in MRPT.
18  * You can sub-class this base class to create custom event types, then
19  * tell between them in runtime with isOfType<T>(), for example:
20  * \code
21  * if (e.isOfType<mrptEventOnDestroy>())
22  * {
23  * const mrptEventOnDestroy* ev = e.getAs<mrptEventOnDestroy>();
24  * ev-> ...
25  * }
26  * \endcode
27  *
28  * \sa CObserver, CObservable
29  * \ingroup mrpt_system_grp
30  */
31 class mrptEvent
32 {
33  protected:
34  /** Just to allow this class to be polymorphic */
35  virtual void do_nothing() {}
36 
37  public:
38  /** Default ctor */
39  inline mrptEvent() : timestamp(mrpt::Clock::now()) {}
40  template <class EVENTTYPE>
41  inline bool isOfType() const
42  {
43  return dynamic_cast<const EVENTTYPE*>(this) != nullptr;
44  }
45 
46  template <class EVENTTYPE>
47  inline const EVENTTYPE* getAs() const
48  {
49  return dynamic_cast<const EVENTTYPE*>(this);
50  }
51 
52  template <class EVENTTYPE>
53  inline EVENTTYPE* getAsNonConst() const
54  {
55  return const_cast<EVENTTYPE*>(dynamic_cast<const EVENTTYPE*>(this));
56  }
57 
59 
60 }; // End of class def.
61 
62 /** An event sent by any CObservable object (automatically) just before being
63  * destroyed and telling its observers to unsubscribe.
64  * \ingroup mrpt_base_grp
65  */
67 {
68  protected:
69  /** Just to allow this class to be polymorphic */
70  void do_nothing() override {}
71 
72  public:
73  inline mrptEventOnDestroy(const CObservable* obj) : source_object(obj) {}
75 
76 }; // End of class def.
77 
78 } // namespace mrpt::system
mrpt::system::mrptEvent
The basic event type for the observer-observable pattern in MRPT.
Definition: mrptEvent.h:31
mrpt::system::mrptEvent::do_nothing
virtual void do_nothing()
Just to allow this class to be polymorphic.
Definition: mrptEvent.h:35
mrpt::system::mrptEvent::timestamp
mrpt::Clock::time_point timestamp
Definition: mrptEvent.h:58
mrpt::system::mrptEventOnDestroy::mrptEventOnDestroy
mrptEventOnDestroy(const CObservable *obj)
Definition: mrptEvent.h:73
mrpt::system::now
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:86
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: BaseAppDataSource.h:15
mrpt::system::mrptEvent::isOfType
bool isOfType() const
Definition: mrptEvent.h:41
mrpt::Clock
Clock that is compatible with MRPT TTimeStamp representation.
Definition: Clock.h:18
mrpt::system::mrptEventOnDestroy
An event sent by any CObservable object (automatically) just before being destroyed and telling its o...
Definition: mrptEvent.h:66
mrpt::system::mrptEventOnDestroy::do_nothing
void do_nothing() override
Just to allow this class to be polymorphic.
Definition: mrptEvent.h:70
mrpt::system::mrptEvent::getAsNonConst
EVENTTYPE * getAsNonConst() const
Definition: mrptEvent.h:53
mrpt::Clock::time_point
std::chrono::time_point< Clock > time_point
Definition: Clock.h:25
mrpt::system::mrptEvent::mrptEvent
mrptEvent()
Default ctor.
Definition: mrptEvent.h:39
mrpt::system::mrptEvent::getAs
const EVENTTYPE * getAs() const
Definition: mrptEvent.h:47
Clock.h
mrpt::system::CObservable
Inherit from this class for those objects capable of being observed by a CObserver class.
Definition: CObservable.h:31
mrpt::system
Definition: backtrace.h:14
mrpt::system::mrptEventOnDestroy::source_object
const CObservable * source_object
Definition: mrptEvent.h:74



Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Fri May 15 15:49:54 UTC 2020