adspro15 / Event

A thread-safe event-listener and observable value implementation for C++17.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status codecov Codacy Badge

lars::Event

A thread-safe event-listener template and observable value implementation for C++17.

Examples

Full examples can be found in the examples directory.

Using lars::Event

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

Using lars::ObservableValue

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

Installation and usage

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.

About

A thread-safe event-listener and observable value implementation for C++17.

License:MIT License


Languages

Language:C++ 78.0%Language:CMake 22.0%