6 #ifndef PATTERNS_OBSERVABLE_H
7 #define PATTERNS_OBSERVABLE_H
18 typename std::vector<O*>::iterator OIt;
24 void AddObserver(O *_observer)
26 for (
typename std::vector<O*>::iterator it = observers.begin();
27 it < observers.end(); it++) {
31 observers.push_back(_observer);
34 void RemoveObserver(O *_observer)
36 for (
typename std::vector<O*>::iterator it = observers.begin();
37 it < observers.end(); it++) {
38 if (*it == _observer) {
46 std::vector<O*> observers;
49 #define DECLARE_FIRE(proc) void Fire##proc();
51 #define DEFINE_FIRE(cls, proc) void cls :: Fire##proc() \
53 for (std::vector<Observer*>::iterator it = observers.begin(); \
54 it < observers.end(); it++) \
58 #define DECLARE_FIRE_WITH_VALUE(proc, type, value) void Fire##proc(type value);
60 #define DEFINE_FIRE_WITH_VALUE(cls, proc, type, value) void cls :: Fire##proc(type value) \
62 for (std::vector<Observer*>::iterator it = observers.begin(); \
63 it < observers.end(); it++) \
67 #define FIRE_WITH_VALUE(proc, type, value) void Fire##proc(type value) \
69 for (std::vector<Observer*>::iterator it = observers.begin(); \
70 it < observers.end(); it++) \
74 #define FIRE_WITH_VALUE2(proc, type1, value1, type2, value2) void Fire##proc(type1 value1, type2 value2) \
76 for (std::vector<Observer*>::iterator it = observers.begin(); \
77 it < observers.end(); it++) \
78 (*it)->proc(value1, value2); \
82 #define DECLARE_FIRE_WITH_THIS(proc) void Fire##proc();
84 #define DEFINE_FIRE_WITH_THIS(cls, proc) void cls :: Fire##proc() \
86 for (std::vector<Observer*>::iterator it = observers.begin(); \
87 it < observers.end(); it++) \
91 #define FIRE_WITH_THIS(proc) void Fire##proc() \
93 for (std::vector<Observer*>::iterator it = observers.begin(); \
94 it < observers.end(); it++) \
98 #define FIRE_VOID(proc) void Fire##proc() \
100 for (std::vector<Observer*>::iterator it = observers.begin(); \
101 it < observers.end(); it++) \
105 #define SAFE_REPLACE_OBSERVER(O, oldO, newO) do { \
106 if (oldO != NULL) { \
107 oldO->RemoveObserver(O); \
110 if (newO != NULL) { \
111 newO->AddObserver(O); \
115 #define SAFE_REMOVE_OBSERVER(O, oldO) do { \
116 if (oldO != NULL) { \
117 oldO->RemoveObserver(O); \
Definition: observable.h:14