If you want to write code in a reactive style or just implement the observer pattern, this is for you.
The library is header-only and has no dependencies; copy the
include/observable
directory into your include path and you're set.
Example:
#include <iostream>
#include <observable/observable.hpp>
using namespace std;
using namespace observable;
int main()
{
auto sub = subject<void(string)> { };
sub.subscribe([](auto const & msg) { cout << msg << endl; });
// "Hello world!" will be printed on stdout.
sub.notify("Hello world!");
auto a = value<int> { 5 };
auto b = value<int> { 5 };
auto avg = observe(
(a + b) / 2.0f
);
auto eq_msg = observe(
select(a == b, "equal", "not equal")
);
avg.subscribe([](auto val) { cout << val << endl; });
eq_msg.subscribe([](auto const & msg) { cout << msg << endl; });
// "10" and "not equal" will be printed on stdout in an
// unspecified order.
b = 15;
return 0;
}
You can access the documentation here: https://danieldinu.com/observable/.
The library is using CMake to build the tests, benchmarks and documentation. You won't need CMake if you don't want to work on the library as a developer.
Boost.Signals2 and Qt are pretty cool libraries and do their jobs well.
This library is not meant to replace signals and slots, it focuses more on providing easy to use observable objects and expressions that can help with patterns like MVC and reactive programming.
Choose whichever library works best for your case; you can even choose them both (for example, have your models use this library and your views use Qt).
Bug reports, feature requests, documentation and code contributions are welcome and highly appreciated.
The library is licensed under the Apache License version 2.0.
Please note that all contributions are considered to be provided under the terms of this license.
Any relatively recent compiler with C++14 support should work.
The code has been tested with the following compilers:
- MSVC 15 (Visual Studio 2017)
- MSVC 14 (Visual Studio 2015)
- GCC 5.4, 6.2
- Clang 3.6, 3.7, 3.8, 3.9
Visual Studio 2017 builds:
Visual Studio 2015 builds:
Linux (GCC, Clang) and OS X (Clang) builds: