Katabatic - Routing Toolbox


Observer.h
1 // -*- C++ -*-
2 //
3 // This file is part of the Coriolis Software.
4 // Copyright (c) UPMC 2008-2018, All Rights Reserved
5 //
6 // +-----------------------------------------------------------------+
7 // | C O R I O L I S |
8 // | K a t a b a t i c - Routing Toolbox |
9 // | |
10 // | Author : Jean-Paul CHAPUT |
11 // | E-mail : Jean-Paul.Chaput@lip6.fr |
12 // | =============================================================== |
13 // | C++ Header : "./katabatic/Observer.h" |
14 // +-----------------------------------------------------------------+
15 
16 
17 #ifndef KATABATIC_OBSERVER_H
18 #define KATABATIC_OBSERVER_H
19 
20 #include "hurricane/Error.h"
21 
22 
23 namespace Katabatic {
24 
25 
26 // -------------------------------------------------------------------
27 // Classes : "BaseObserver" & "Observer".
28 
29  class BaseObserver {
30  public:
31  virtual void notify ( unsigned int flags );
32  };
33 
34 
35  template< typename T>
36  class Observer : public BaseObserver {
37  public:
38  inline Observer ( const T* owner );
39  inline T* getOwner () const;
40  private:
41  Observer ( const Observer& );
42  private:
43  static int _ownerOffset;
44  };
45 
46 
47  template< typename T>
49 
50  template< typename T>
51  inline Observer<T>::Observer ( const T* owner )
52  : BaseObserver()
53  {
54  if (owner == NULL)
55  throw Hurricane::Error( "Observer::Observer(), attempt to create with NULL owner." );
56 
57  if (_ownerOffset < 0)
58  _ownerOffset = (unsigned long)this - (unsigned long)owner;
59  }
60 
61 
62  template< typename T>
63  inline T* Observer<T>::getOwner () const { return reinterpret_cast<T*>((unsigned long)this - _ownerOffset); }
64 
65 
66 // -------------------------------------------------------------------
67 // Class : "Observable".
68 
69  class Observable {
70  public:
71  inline Observable ();
72  template< typename T >
73  inline T* getObserver ();
74  inline void addObserver ( BaseObserver* );
75  inline void removeObserver ( BaseObserver* );
76  inline void notify ( unsigned int flags );
77  private:
78  Observable ( const Observable& );
79  private:
80  BaseObserver* _observer;
81  };
82 
83 
85  : _observer(NULL)
86  { }
87 
88 
89  template< typename T >
91  {
92  if (_observer) return (dynamic_cast< Observer<T>* >( _observer ))->getOwner();
93  return NULL;
94  }
95 
96  inline void Observable::addObserver ( BaseObserver* observer )
97  {
98  if (_observer)
99  throw Hurricane::Error( "Observable::addObserver(), trying to add a second observer which is unsupported." );
100  _observer = observer;
101  }
102 
103  inline void Observable::removeObserver ( BaseObserver* observer )
104  {
105  if (_observer != observer)
106  throw Hurricane::Error( "Observable::removeObserver(), trying to remove wrong observer." );
107  _observer = NULL;
108  }
109 
110 
111  inline void Observable::notify ( unsigned int flags )
112  {
113  if (_observer) _observer->notify( flags );
114  }
115 
116 
117 } // Katabatic namespace.
118 
119 #endif
void removeObserver(BaseObserver *)
Definition: Observer.h:103
virtual void notify(unsigned int flags)
Definition: Observer.cpp:26
T * getOwner() const
Definition: Observer.h:63
Observer Design Pattern, Subject part.
Definition: Observer.h:69
Observable()
Definition: Observer.h:84
Observer(const T *owner)
Definition: Observer.h:51
void notify(unsigned int flags)
Definition: Observer.h:111
void addObserver(BaseObserver *)
Definition: Observer.h:96
Observer Design Pattern, Observer part.
Definition: Observer.h:29
Observer Design Pattern, Observer part.
Definition: Observer.h:36
T * getObserver()
Definition: Observer.h:90


Generated by doxygen 1.8.5 on Mon Oct 1 2018 Return to top of page
Katabatic - Routing Toolbox Copyright © 2008-2016 UPMC. All rights reserved