| 1 | /*
 | 
|---|
| 2 |  * Graveyard.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Sep 5, 2013
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef GRAVEYARD_HPP_
 | 
|---|
| 9 | #define GRAVEYARD_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | // include config.h
 | 
|---|
| 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 13 | #include <config.h>
 | 
|---|
| 14 | #endif
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <map>
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "CodePatterns/Observer/Zombie.hpp"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | class GraveyardUnitTest;
 | 
|---|
| 21 | class Observable;
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /** The Graveyard takes of Observables instances that are about to get
 | 
|---|
| 24 |  * destroyed but where it is necessary that all Observers that are still
 | 
|---|
| 25 |  * signOn()'ed signOff() before the instance actually vanishes.
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  * For this the Graveyard takes Observable and turns/wraps them into a
 | 
|---|
| 28 |  * Zombie class (that is basically just an Attorney pattern so far)
 | 
|---|
| 29 |  * informing the Observable of a present Graveyard.
 | 
|---|
| 30 |  *
 | 
|---|
| 31 |  * \section graveyard-usage Usage
 | 
|---|
| 32 |  *
 | 
|---|
| 33 |  *   The Graveyard is really easy to use. Have an instance that lives
 | 
|---|
| 34 |  *   long enough and use
 | 
|---|
| 35 |  *   \code
 | 
|---|
| 36 |  *    Observable *_obs = new Observable("_obs");
 | 
|---|
| 37 |  *    Graveyard::turnZombies(_obs);
 | 
|---|
| 38 |  *   \endcode
 | 
|---|
| 39 |  *   in place pf
 | 
|---|
| 40 |  *   \code
 | 
|---|
| 41 |  *   delete _obs;
 | 
|---|
| 42 |  *   \endcode
 | 
|---|
| 43 |  *   The instance is fully destroyed as soon as all Observers are signOff()'d
 | 
|---|
| 44 |  *
 | 
|---|
| 45 |  */
 | 
|---|
| 46 | class Graveyard
 | 
|---|
| 47 | {
 | 
|---|
| 48 |   //!> grant unit test access to private parts
 | 
|---|
| 49 |   friend class GraveyardUnitTest;
 | 
|---|
| 50 | public:
 | 
|---|
| 51 |   Graveyard();
 | 
|---|
| 52 |   ~Graveyard();
 | 
|---|
| 53 | 
 | 
|---|
| 54 |   /** This turns an Observable into a Zombie and takes over the ptr.
 | 
|---|
| 55 |    *
 | 
|---|
| 56 |    * \param _observable Observable to take control of
 | 
|---|
| 57 |    */
 | 
|---|
| 58 |   void turnZombie(Observable *&_observable);
 | 
|---|
| 59 | 
 | 
|---|
| 60 | private:
 | 
|---|
| 61 |   friend class Observable;
 | 
|---|
| 62 |   /** Function to be called by Observables if Observer's have signOff()ed.
 | 
|---|
| 63 |    *
 | 
|---|
| 64 |    * \param _observable calling Observable
 | 
|---|
| 65 |    */
 | 
|---|
| 66 |   void ObserverLeft(const Observable *_observable);
 | 
|---|
| 67 | 
 | 
|---|
| 68 | private:
 | 
|---|
| 69 |   //!> typedef for internal list of zombies
 | 
|---|
| 70 |   typedef std::map<const Observable*,Zombie::ptr> graveyard_t;
 | 
|---|
| 71 |   //!> the list of currently active zombies
 | 
|---|
| 72 |   graveyard_t graveyard;
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   //!> static function to inform observer
 | 
|---|
| 75 |   Observable::graveyard_informer_t graveyard_informer;
 | 
|---|
| 76 | };
 | 
|---|
| 77 | 
 | 
|---|
| 78 | #endif /* GRAVEYARD_HPP_ */
 | 
|---|