vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Observer.h
Go to the documentation of this file.
1 #pragma once
2 #include "Export"
3 #include <vector>
4 #include <algorithm>
5 
6 namespace vreckoUtils
7 {
8 
10 class Observable;
11 
12 class VRECKO_UTILS_EXPORT Observer
13 {
14 public:
15  virtual void notify(Observable* object, void* userData) = 0;
16 protected:
17  Observer() {};
18 };
19 
20 class VRECKO_UTILS_EXPORT Observable
21 {
22 public:
23  void addObserver(Observer* observer) {
24  _observers.push_back(observer);
25  }
26  void removeObserver(Observer* observer) {
27  auto it = std::find(_observers.begin(), _observers.end(), observer);
28  if(it != _observers.end()) _observers.erase(it);
29  }
30  void notifyObservers(void* userData = NULL) {
31  for(auto it = _observers.begin(); it != _observers.end(); ++it)
32  (*it)->notify(this, userData);
33  }
34 
35 protected:
36  Observable() {};
37  std::vector<Observer*> _observers;
38 };
39 
40 }