Skip to content

Instantly share code, notes, and snippets.

@Lavesson
Created June 30, 2016 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lavesson/1ec16bdab2641d378b9640d7631881af to your computer and use it in GitHub Desktop.
Save Lavesson/1ec16bdab2641d378b9640d7631881af to your computer and use it in GitHub Desktop.
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