Skip to content

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

License

Notifications You must be signed in to change notification settings

aseprite/observable

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Observable Library

Copyright (C) 2016-2021 David Capello

build MIT 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"

class WidgetObserver {
public:
  virtual ~WidgetObserver() = 0;
  virtual void onClick() { }
};

class Widget : public obs::observable<WidgetObserver> {
public:
  void processClick() {
    notify_observers(&WidgetObserver::onClick);
  }
};

An example

#include "obs.h"

class ObserveClick : public WidgetObserver {
public:
  void onClick() override {
    // Do something...
  }
};

...
ObserveClick observer;
Widget button;
button.add_observer(&observer);

Signal

#include "obs.h"

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

Tested Compilers

  • Visual Studio 2015
  • Xcode 7.3.1 (-std=c++11)
  • GCC 4.8.4 (-std=c++11)

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 92.1%
  • CMake 7.0%
  • C 0.9%