Main MRPT website > C++ reference
MRPT logo
CObservable.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CObservable_H
29 #define CObservable_H
30 
31 #include <mrpt/utils/utils_defs.h>
32 #include <mrpt/utils/mrptEvent.h>
33 
34 namespace mrpt
35 {
36  namespace utils
37  {
38  class CObserver;
39 
40  /** Inherit from this class for those objects capable of being observed by a CObserver class.
41  *
42  * The only thing to do in your child class is to call CObservable::publishEvent() whenever needed and all the
43  * observer classes will be notified.
44  *
45  * \note The pairs CObservable / CObserver automatically notify each other the destruction of any of them, effectively ending the subscription of events.
46  *
47  * \sa CObserver, mrptEvent
48  * \ingroup mrpt_base_grp
49  */
51  {
52  friend class CObserver;
53 
54  public:
55  CObservable();
56  virtual ~CObservable();
57 
58  private:
59  std::set<CObserver*> m_subscribers;
60  void internal_observer_begin(CObserver*);
61  void internal_observer_end(CObserver*);
62 
63  protected:
64  /** Called when you want this object to emit an event to all the observers currently subscribed to this object. */
65  void publishEvent(const mrptEvent &e) const;
66 
67  /** Can be called by a derived class before preparing an event for publishing with \a publishEvent to determine if there is no one
68  * subscribed, so it can save the wasted time preparing an event that will be not read. */
69  inline bool hasSubscribers() const { return !m_subscribers.empty(); }
70 
71  }; // End of class def.
72 
73  } // End of namespace
74 } // end of namespace
75 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013