MRPT  2.0.1
CObservable.cpp
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 
10 #include "system-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/exceptions.h>
14 #include <mrpt/system/CObserver.h>
15 #include <iostream>
16 
17 using namespace mrpt::system;
18 using namespace std;
19 
20 CObservable::CObservable() = default;
22 {
23  try
24  {
25  // Notify my destruction:
26  this->publishEvent(mrptEventOnDestroy(this));
27 
28  // Tell observers to unsubscribe:
29  while (!m_subscribers.empty())
30  (*m_subscribers.begin())->observeEnd(*this);
31  }
32  catch (const std::exception& e)
33  {
34  std::cerr << "[~CObservable] Exception:\n" << mrpt::exception_to_str(e);
35  }
36 }
37 
39 {
40  m_subscribers.insert(o);
41 }
42 
44 {
46  auto it = m_subscribers.find(o);
47  ASSERTMSG_(
48  it != m_subscribers.end(),
49  "Ending subscription from an observer not subscribed to this object!");
50  m_subscribers.erase(it);
51  MRPT_END
52 }
53 
54 /** Called when you want this object to emit an event to all the observers
55  * currently subscribed to this object.
56  */
58 {
60  for (auto& s : m_subscribers)
61  if (s) s->internal_on_event(e);
62  MRPT_END
63 }
void internal_observer_end(CObserver *)
Definition: CObservable.cpp:43
#define MRPT_START
Definition: exceptions.h:241
The basic event type for the observer-observable pattern in MRPT.
Definition: mrptEvent.h:31
STL namespace.
Inherit from this class to get notified about events from any CObservable object after subscribing to...
Definition: CObserver.h:34
An event sent by any CObservable object (automatically) just before being destroyed and telling its o...
Definition: mrptEvent.h:66
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
void internal_observer_begin(CObserver *)
Definition: CObservable.cpp:38
#define MRPT_END
Definition: exceptions.h:245
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:57
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59



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