-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
223 additions
and
62 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,3 +61,4 @@ packageProject( | |
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION} | ||
DEPENDENCIES "" | ||
) | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
|
||
namespace observe { | ||
|
||
template <typename... Args> class Event; | ||
|
||
/** | ||
* A generic event observer class | ||
*/ | ||
class Observer { | ||
public: | ||
/** | ||
* The base class for observer implementations | ||
*/ | ||
struct Base { | ||
/** | ||
* Observers should remove themselves from their events once destroyed | ||
*/ | ||
virtual ~Base() {} | ||
}; | ||
|
||
Observer() {} | ||
Observer(Observer &&other) = default; | ||
template <typename L> Observer(L &&l) : data(new L(std::move(l))) {} | ||
|
||
Observer &operator=(const Observer &other) = delete; | ||
Observer &operator=(Observer &&other) = default; | ||
|
||
template <typename L> Observer &operator=(L &&l) { | ||
data.reset(new L(std::move(l))); | ||
return *this; | ||
} | ||
|
||
/** | ||
* Observe an event with the callback | ||
*/ | ||
template <typename H, typename... Args> void observe(Event<Args...> &event, const H &handler) { | ||
data.reset(new typename Event<Args...>::Observer(event.createObserver(handler))); | ||
} | ||
|
||
/** | ||
* remove the callback from the event | ||
*/ | ||
void reset() { data.reset(); } | ||
|
||
/** | ||
* returns `true` if the event | ||
*/ | ||
operator bool() const { return bool(data); } | ||
|
||
private: | ||
std::unique_ptr<Base> data; | ||
}; | ||
|
||
} // namespace observe |
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
Oops, something went wrong.