Version: 0.6.0
observable.h
1 /*
2  * Written by Moskvichev A.V.
3  * PUBLIC DOMAIN
4  */
5 
6 #ifndef PATTERNS_OBSERVABLE_H
7 #define PATTERNS_OBSERVABLE_H
8 
9 #include <vector>
10 
14 template<class O> class Observable
15 {
16 public:
17  typedef O Observer;
18  typename std::vector<O*>::iterator OIt;
19 
20  Observable()
21  {
22  }
23 
24  void AddObserver(O *_observer)
25  {
26  for (typename std::vector<O*>::iterator it = observers.begin();
27  it < observers.end(); it++) {
28  if (*it == _observer)
29  return ;
30  }
31  observers.push_back(_observer);
32  }
33 
34  void RemoveObserver(O *_observer)
35  {
36  for (typename std::vector<O*>::iterator it = observers.begin();
37  it < observers.end(); it++) {
38  if (*it == _observer) {
39  observers.erase(it);
40  return ;
41  }
42  }
43  }
44 
45 protected:
46  std::vector<O*> observers;
47 };
48 
49 #define DECLARE_FIRE(proc) void Fire##proc();
50 
51 #define DEFINE_FIRE(cls, proc) void cls :: Fire##proc() \
52 { \
53  for (std::vector<Observer*>::iterator it = observers.begin(); \
54  it < observers.end(); it++) \
55  (*it)->proc(); \
56 }
57 
58 #define DECLARE_FIRE_WITH_VALUE(proc, type, value) void Fire##proc(type value);
59 
60 #define DEFINE_FIRE_WITH_VALUE(cls, proc, type, value) void cls :: Fire##proc(type value) \
61 { \
62  for (std::vector<Observer*>::iterator it = observers.begin(); \
63  it < observers.end(); it++) \
64  (*it)->proc(value); \
65 }
66 
67 #define FIRE_WITH_VALUE(proc, type, value) void Fire##proc(type value) \
68 { \
69  for (std::vector<Observer*>::iterator it = observers.begin(); \
70  it < observers.end(); it++) \
71  (*it)->proc(value); \
72 }
73 
74 #define FIRE_WITH_VALUE2(proc, type1, value1, type2, value2) void Fire##proc(type1 value1, type2 value2) \
75 { \
76  for (std::vector<Observer*>::iterator it = observers.begin(); \
77  it < observers.end(); it++) \
78  (*it)->proc(value1, value2); \
79 }
80 
81 
82 #define DECLARE_FIRE_WITH_THIS(proc) void Fire##proc();
83 
84 #define DEFINE_FIRE_WITH_THIS(cls, proc) void cls :: Fire##proc() \
85 { \
86  for (std::vector<Observer*>::iterator it = observers.begin(); \
87  it < observers.end(); it++) \
88  (*it)->proc(this); \
89 }
90 
91 #define FIRE_WITH_THIS(proc) void Fire##proc() \
92 { \
93  for (std::vector<Observer*>::iterator it = observers.begin(); \
94  it < observers.end(); it++) \
95  (*it)->proc(this); \
96 }
97 
98 #define FIRE_VOID(proc) void Fire##proc() \
99 { \
100  for (std::vector<Observer*>::iterator it = observers.begin(); \
101  it < observers.end(); it++) \
102  (*it)->proc(); \
103 }
104 
105 #define SAFE_REPLACE_OBSERVER(O, oldO, newO) do { \
106  if (oldO != NULL) { \
107  oldO->RemoveObserver(O); \
108  } \
109  \
110  if (newO != NULL) { \
111  newO->AddObserver(O); \
112  } \
113 } while (0)
114 
115 #define SAFE_REMOVE_OBSERVER(O, oldO) do { \
116  if (oldO != NULL) { \
117  oldO->RemoveObserver(O); \
118  } \
119 } while (0)
120 
121 #endif /* PATTERNS_OBSERVABLE_H */
Definition: observable.h:14