An open API service indexing awesome lists of open source software.

https://github.com/curve/ereignis

🎉 A thread-safe C++20 Event Library
https://github.com/curve/ereignis

cmake cpp cpp-library cpp20 cpp20-library events

Last synced: 10 months ago
JSON representation

🎉 A thread-safe C++20 Event Library

Awesome Lists containing this project

README

          





A thread-safe C++20 Event Library


## 📖 Description

_Ereignis_ is a library that implements an easy to use Event/Callback mechanism that allows for lazy-evaluation of the the results returned by `fire`.

## 📦 Installation

> [!NOTE]
> This library requires a C++20 capable compiler.
> See versions `< 2.0` for C++17 support.

* Using [CPM](https://github.com/cpm-cmake/CPM.cmake)
```cmake
CPMFindPackage(
NAME ereignis
VERSION 4.0
GIT_REPOSITORY "https://github.com/Curve/ereignis"
)
```

* Using FetchContent
```cmake
include(FetchContent)

FetchContent_Declare(ereignis GIT_REPOSITORY "https://github.com/Curve/ereignis" GIT_TAG v4.0)
FetchContent_MakeAvailable(ereignis)

target_link_libraries( cr::ereignis)
```

## 📃 Usage

* Basic callback

```cpp
#include

using ereignis::event;

ereignis::manager<
event<0, void(int)>
> manager;

manager.at<0>().add([](int i) { std::cout << i << std::endl; });
manager.at<0>().fire(1337);
```

* Result iteration

```cpp
#include

enum class window_event
{
close
};

using ereignis::event;

ereignis::manager<
event
> manager;

manager.at().add([]() -> bool { return true; });

manager.at().add([]() -> bool
{
std::cout << "Reached!" << std::endl;
return false;
});

// Lets fire all callbacks until we get `true`.

for (const auto& result : manager.at().fire())
{
if (result)
{
return;
}
}

// 'Reached!' will never be printed.
```