Signal callback library is an easy to use callback system for C++ applications.
Its purpose is to allow different objects to communicate between each other by emitting the callback functions to the class that has connected to the signal.
Name signal is inspired by Qt naming convention.
This method is very useful when C++ implements classes with has-a relation, where another object may want to notify the parent when some event occurs.
Main motivation behind the library is to simplify events data between GUI development and its UI widgets, where parent widget might want to get notified about events coming from the children object.
- Lightweight and easy to use, developed in C++
- Template classes for
0-3
parameters callback system - Available as CMake interface library
- User friendly MIT license
- In your producer class define new signal with its appropriate type:
sigback::Signal<int> signal_with_int_type;
- Connect your producer callback function to the signal:
signal_with_int_type.connect(&ClassName::method_name, &classobject);
- Call emit function in producer class
signal_with_int_type.emit(10);
Full example
#include <iostream>
#include <stdint.h>
#include <string.h>
#include "signal/signal.hpp"
class Producer {
public:
// Producer will emit callback when something happens...
sigback::Signal<int> signal_number_changed;
void
do_something_that_can_emit(int value) {
signal_number_changed.emit(value);
}
private:
};
// Consumer will link itself to producer
class Consumer {
public:
void
on_number_changed(int number) {
std::cout << "Number changed to " << number << std::endl;
}
};
int
main(void) {
Producer prod;
Consumer cons;
// Connect consumer's function to the producer signal
prod.signal_number_changed.connect(&Consumer::on_number_changed, &cons);
// Producer will change number
prod.do_something_that_can_emit(10);
prod.do_something_that_can_emit(11);
prod.do_something_that_can_emit(12);
return 0;
}
Running the above code will print:
Number changed to 10
Number changed to 11
Number changed to 12
Fresh contributions are always welcome. Simple instructions to proceed:
- Fork Github repository
- Follow C style & coding rules already used in the project
- Create a pull request to develop branch with new features or bug fixes
Alternatively you may:
- Report a bug
- Ask for a feature request