MRPT  2.0.1
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
Clock that is compatible with MRPT TTimeStamp representation.
Definition: Clock.h:18
std::chrono::time_point< Clock > time_point
Definition: Clock.h:25
The basic event type for the observer-observable pattern in MRPT.
Definition: mrptEvent.h:31
void do_nothing() override
Just to allow this class to be polymorphic.
Definition: mrptEvent.h:70
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:86
EVENTTYPE * getAsNonConst() const
Definition: mrptEvent.h:53
virtual void do_nothing()
Just to allow this class to be polymorphic.
Definition: mrptEvent.h:35
mrpt::Clock::time_point timestamp
Definition: mrptEvent.h:58
An event sent by any CObservable object (automatically) just before being destroyed and telling its o...
Definition: mrptEvent.h:66
const EVENTTYPE * getAs() const
Definition: mrptEvent.h:47
Inherit from this class for those objects capable of being observed by a CObserver class...
Definition: CObservable.h:31
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
bool isOfType() const
Definition: mrptEvent.h:41
const CObservable * source_object
Definition: mrptEvent.h:74
mrptEventOnDestroy(const CObservable *obj)
Definition: mrptEvent.h:73
mrptEvent()
Default ctor.
Definition: mrptEvent.h:39



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020