• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language CMake
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ“£ Hey listen! A simple general-purpose event-listener system for C++17.

Actions Status Actions Status Actions Status Actions Status Actions Status codecov

Observe

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

API

The core API is best illustrated by an example.

#include <string>
#include <iostream>

#include <observe/event.h>

void example() {
  // events can be valueless
  observe::Event<> eventA;

  // or have arguments
  observe::Event<std::string, float> eventB;
  
  // connect will always trigger when an event is triggered
  eventA.connect([](){
    std::cout << "A triggered" << std::endl;
  });
  
  // observers will remove themselves from the event on destroy or reset
  observe::Observer observer = eventB.createObserver([](const std::string &str, float v){ 
    std::cout << "B triggered with " << str << " and " << v << std::endl;
  });

  // call emit to trigger all observers
  eventA.emit();
  eventB.emit("meaning of life", 42);

  // `observe::Observer` can store any type of observer
  // previous observers will be removed
  observer.observe(eventA, [](){ std::cout << "I am now observing A" << std::endl; });

  // to remove an observer without destroying the object, call reset
  observer.reset();
}

Note that events and observers are thread and exception safe, as long as the handlers manage their own resources. Handlers can safely remove observers (including themselves) from the event when beeing called. Thrown exceptions will propagate out of the event.emit() call.

Using observe::Value

The project also includes a header observe/value.h with an experimental observable value implementation. The API is still subject to change, so use with caution.

observe::Value a = 1;
observe::Value b = 2;

// contains the sum of `a` and `b`
observe::DependentObservableValue sum([](auto a, auto b){ return a+b; },a,b);

// all observable values contain an `Event` `onChange`
sum.onChange.connect([](auto &v){ 
  std::cout << "The result changed to " << r << std::endl;
});

// access the value by dereferencing
std::cout << "The result is " << *sum << std::endl; // -> the result is 3

// changes will automatically propagate through dependent values
a.set(3); // -> The result changed to 5

Installation and usage

With CPM.cmake you can easily add the headers to your project.

CPMAddPackage(
  NAME Observe
  VERSION 3.0
  GITHUB_REPOSITORY TheLartians/Observe
)

target_link_libraries(myProject Observe)

More Repositories

1

ModernCppStarter

πŸš€ Kick-start your C++! A template for modern C++ projects using CMake, CI, code coverage, clang-format, reproducible dependency management and much more.
CMake
4,417
star
2

PEGParser

πŸ’‘ Build your own programming language! A C++17 PEG parser generator supporting parser combination, memoization, left-recursion and context-dependent grammars.
C++
240
star
3

modern-wasm-starter

πŸ›Έ Run C++ code on web and create blazingly fast websites! A starter template to easily create WebAssembly packages using type-safe C++ bindings with automatic TypeScript declarations.
TypeScript
239
star
4

Format.cmake

πŸ’… Stylize your code! Automatic clang-format and cmake-format targets for CMake.
Python
159
star
5

EasyIterator

πŸƒ Iterators made easy! Zero cost abstractions for designing and using C++ iterators.
C++
138
star
6

PackageProject.cmake

πŸ›οΈ Help other developers use your project. A CMake script for packaging C/C++ projects for simple project installation while employing best-practices for maximum compatibility.
CMake
103
star
7

MiniCppStarter

🧸 A tiny single-file version of the ModernCppStarter project for exploring libraries or playing with C++ code. Reproducible dependency management included, so the code will work the same everywhere.
CMake
75
star
8

StaticTypeInfo

πŸ€ Up your type-game. A small C++ library for compile-time type names and type indices.
CMake
70
star
9

Ccache.cmake

πŸš… Compile faster with Ccache! A Ccache integration for CMake with Xcode support.
CMake
58
star
10

Glue

⛓️ Bindings that stick. A simple and generic API for C++ to other language bindings supporting bidirectional communication, inheritance and automatic declarations.
C++
56
star
11

TypeScriptXX

🧷 Stay safe! Type-safe scripting for C++ using TypeScriptToLua and CMake with auto-generated declarations.
CMake
40
star
12

GroupSourcesByFolder.cmake

Automatically group sources by folder structure for Visual Studio/Xcode generators
CMake
32
star
13

BitLens

πŸ”Ž Have your bits and eat them too! A C++17 bit lens container for vector types.
C++
21
star
14

EmGlue

πŸ•ΈοΈ Glue C++ to your browser! Universal bindings for JavaScript/Wasm using Glue and Embind.
C++
21
star
15

StaticHash

Constexpr hash functions for C++
C++
18
star
16

substitute

🍿Enjoy the the big screen! Watch and synchronise subtitles externally in any browser. On mobile and desktop.
TypeScript
16
star
17

TypeScript2Python

πŸšƒ Transpile TypeScript types to Python! A TypeScript to Python type transpiler.
TypeScript
15
star
18

Revisited

πŸ§‘β€πŸ€β€πŸ§‘ The visitor pattern revisited. An inheritance-aware acyclic visitor template, any and any-function templates.
C++
15
star
19

react-native-simple-transition

🌠 An easy to use transition component for React Native
Java
13
star
20

Generator

A generator class emulating coroutines' yield functionality through std::thread
C++
9
star
21

PyPropagate

A paraxial wave propagation framework for python
Jupyter Notebook
5
star
22

NDArray

[legacy project] A fast n-dimensional array template for C++
C++
5
star
23

LuaGlue

Lua bindings for the Glue library
C++
4
star
24

LHC

[legacy project] Some single-header helper libraries I use in some projects
C++
4
star
25

IndexSet

A class for manipulating large sets of indices with optimal performance and memory use
CMake
3
star
26

denon-cli

A simple command line interface for controlling Denon AVR receivers.
JavaScript
2
star
27

Presentations

https://thelartians.github.io/Presentations
JavaScript
2
star
28

Saphira

Our submission for the #WirVsVirus Hackathron: https://youtu.be/OWfDj2fOfVk | https://thelartians.github.io/Saphira/
TypeScript
2
star
29

NDArrayOld

A fast n-dimensional array template for c++
C++
2
star
30

Lars

A cmake enabled git-submodule collection of my C++ toolbox
C++
1
star
31

advent-of-code-2020

β˜ƒοΈπŸŽ„πŸ‘¨β€πŸ’» Solving Advent of Code 2020 in Rust πŸ¦€
Rust
1
star