Created
June 30, 2016 16:28
-
-
Save Lavesson/1ec16bdab2641d378b9640d7631881af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Pattern { | |
template <typename... TArgs> class Observable; | |
template <typename... TArgs> class Observer { | |
virtual void notify(TArgs... args) = 0; | |
friend class Observable<TArgs...>; | |
public: | |
virtual ~Observer() {} | |
}; | |
template <typename... TArgs> class Observable { | |
using TObserver = Observer<TArgs...>*; | |
std::unordered_set<TObserver> _observers; | |
protected: | |
void notifyAll(TArgs... args) { | |
for (auto o : _observers) { o->notify(args); } | |
} | |
public: | |
void subscribe(TObserver o) { _observers.insert(o); } | |
void unsubscribe(TObserver o) { _observers.erase(o); }; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment