Main MRPT website > C++ reference
MRPT logo
CObserver.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 CObserver_H
29 #define CObserver_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 CObservable;
39 
40  /** Inherit from this class to get notified about events from any CObservable object after subscribing to it.
41  *
42  * The main methods in this class are:
43  * - observeBegin(): To be called to start listening at a given object.
44  * - OnEvent(): Virtual functions to be implemented in your child class to receive all the notifications.
45  *
46  * Note that if custom (child) mrptEvent classes are used, you can tell between them in runtime with "dynamic_cast<>()".
47  *
48  * \note The pairs CObservable / CObserver automatically notify each other the destruction of any of them, effectively ending the subscription of events.
49  * \ingroup mrpt_base_grp
50  * \sa CObservable, mrptEvent
51  */
53  {
54  friend class CObservable;
55 
56  public:
57  CObserver();
58  virtual ~CObserver();
59 
60  /** Starts the subscription of this observer to the given object. \sa observeEnd */
61  void observeBegin(CObservable &obj);
62 
63  /** Ends the subscription of this observer to the given object (note that there is no need to call this method, since the destruction of the first of observer/observed will put an end to the process
64  \sa observeBegin */
65  void observeEnd(CObservable &obj);
66 
67  private:
68  std::set<CObservable*> m_subscribed;
69  void internal_on_event(const mrptEvent &e);
70 
71  protected:
72  /** This virtual function will be called upon receive of any event after starting listening at any CObservable object.
73  */
74  virtual void OnEvent(const mrptEvent &e) = 0;
75 
76  }; // End of class def.
77 
78  } // End of namespace
79 } // end of namespace
80 #endif



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