Skip to content

Repository files navigation

Observable Library

Copyright (C) 2016-present David Capello

buildMIT Licensed

Library to use the observer pattern in C++11 programs with observable/observer classes or signals/slots.

Features

  • Generate an observable notification/signal from multiple threads
  • Add/remove observers/slots from multiple threads
  • Erase/disconnect an observer/slot from the same observable notification/signal
  • Reconnect an observer in the same notification

Observable

An observable Widget:

#include"obs.h"classWidgetObserver {
public:virtual~WidgetObserver() = 0;
virtualvoidonClick() { }
};
classWidget : publicobs::observable<WidgetObserver> {
public:voidprocessClick() {
notify_observers(&WidgetObserver::onClick);
}
};

An example

#include"obs.h"classObserveClick : publicWidgetObserver {
public:voidonClick() override {
// Do something...
}
};
...
ObserveClick observer;
Widget button;
button.add_observer(&observer);

Signal

#include"obs.h"intmain() {
obs::signal<void (int, int)> sig;
sig.connect([](int x, int y){ ... });
sig(1, 2); // Generate signal
}

Safe vs Fast

There are two variants of signals and observers:

  • obs::safe_signal vs obs::fast_signal
  • obs::safe_observers vs obs::fast_observers

The only difference between both is that the "fast" version uses obs::fast_list instead of obs::safe_list, where the former keeps track of slots with a std::vector and the later one is a thread-safe version list to connect/disconnect slots from different threads.

By default and for backward compatibility, obs::signal and obs::observers use the safe version, but you can enable the OBSERVABLE_FAST_LIST option to switch from obs::safe_list to obs::fast_list which is recommended for most cases (e.g. you don't need to do strange connections/disconnections as in tests).

About

Observer pattern and signals/slots for C++11 projects

Topics

Resources

Contributing

Stars

86 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages