A thread-safe event-listener template and observable value implementation for C++17.
Full examples can be found in the examples directory.
lars::Event<float,float> onClick;
auto observer = onClick.createObserver([](auto x, auto y){ handleClick(x,y); });
onClick.emit(0,0); // emits event to all observers
observer.reset(); // removes observer from event
lars::ObservableValue a = 1;
lars::ObservableValue b = 2;
lars::DependentObservableValue sum([](auto a, auto b){ return a+b; },a,b);
sum.onChange.connect([](auto &v){ std::cout << "The result changed to " << r << std::endl; });
std::cout << "The result is " << *sum << std::endl; // -> the result is 3
a.set(3); // -> The result changed to 5
With CPM, lars::Event can be used in a CMake project simply by adding the following to the project's CMakeLists.txt
.
CPMAddPackage(
NAME LarsEvent
VERSION 2.1
GIT_REPOSITORY https://github.com/TheLartians/Event.git
)
target_link_libraries(myProject LarsEvent)
Alternatively, the repository can be cloned locally and included it via add_subdirectory
. Installing lars::Event will make it findable in CMake's find_package
.