Observer(Event) system (Signals and Slots), type and template based
Signals are represented as types. You can connect a member function of some instance to the Observer system. This connected function will be called whenever a signal of a type that is the same type as the function parameter is emitted.
ObserverSystem.hpp:
#include <vector>
class ObserverSystem
{
public:
template<typename SignalType>
inline static void emitSignal(const SignalType& signal)
{
for(auto f : callEverything<SignalType>)
{
f(signal);
}
}
template<typename SignalType, typename ObjectType>
inline static void connect(ObjectType& object, void (ObjectType::*function)(const SignalType& signal))
{
bool add = true;
for(auto& entry : functions<SignalType, ObjectType>)
{
if(entry.function == function && entry.object == &object)
{
add = false;
break;
}
}
if(add)
{
functions<SignalType, ObjectType>.emplace_back(ObjectFunction<SignalType, ObjectType>{&object, function});
}
add = true;
for(auto& entry : callEverything<SignalType>)
{
if(entry == callAll<SignalType, ObjectType>)
{
add = false;
break;
}
}
if(add)
{
callEverything<SignalType>.emplace_back(callAll<SignalType, ObjectType>);
}
}
template<typename SignalType, typename ObjectType>
inline static void disconnect(ObjectType& object, void (ObjectType::*function)(const SignalType& signal))
{
for(size_t i = 0; i < functions<SignalType, ObjectType>.size();)
{
if(functions<SignalType, ObjectType>[i].function == function && functions<SignalType, ObjectType>[i].object == &object)
{
functions<SignalType, ObjectType>.erase(functions<SignalType, ObjectType>.begin() + i);
}
else
{
i++;
}
}
}
private:
template<typename SignalType, typename ObjectType>
struct ObjectFunction
{
ObjectType* object;
void (ObjectType::*function)(const SignalType& signal);
void call(const SignalType& signal)
{
(object->*function)(signal);
}
};
template<typename SignalType, typename ObjectType>
inline static std::vector<ObjectFunction<SignalType, ObjectType>> functions;
template<typename SignalType, typename ObjectType>
inline static void callAll(const SignalType& signal)
{
for(auto function : functions<SignalType, ObjectType>)
{
function.call(signal);
}
}
template<typename SignalType>
inline static std::vector<void (*)(const SignalType& signal)> callEverything;
};
main.cpp
#include <iostream>
#include "ObserverSystem.hpp"
struct SignalTypeOne
{
int b;
};
struct SignalTypeTwo
{
int x;
int y;
};
struct Hello
{
int a = 123;
void f(const SignalTypeOne& signal)
{
std::cout << (int*)this << " : a: " << a << std::endl;
std::cout << "b: " << signal.b << std::endl;
}
};
struct Some
{
int vx = 0;
int vy = 0;
void f1(const SignalTypeTwo& signal)
{
vx += signal.x;
vy += signal.y;
}
void f2(const SignalTypeOne& signal)
{
std::cout << (int*)this << ": " << "vx = " << vx <<", vy = " << vy << std::endl;
}
};
int main()
{
Hello h;
Hello g = Hello{321};
Some v1;
Some v2;
ObserverSystem::connect(h, &Hello::f);
ObserverSystem::connect(g, &Hello::f);
ObserverSystem::connect(v1, &Some::f1);
ObserverSystem::connect(v2, &Some::f1);
ObserverSystem::connect(v1, &Some::f2);
ObserverSystem::connect(v2, &Some::f2);
ObserverSystem::emitSignal(SignalTypeTwo{1,1});
ObserverSystem::emitSignal(SignalTypeOne{444});
ObserverSystem::disconnect(h, &Hello::f);
ObserverSystem::disconnect(v1, &Some::f1);
ObserverSystem::emitSignal(SignalTypeTwo{1,2});
ObserverSystem::emitSignal(SignalTypeOne{555});
ObserverSystem::connect(h, &Hello::f);
ObserverSystem::emitSignal(SignalTypeTwo{2,1});
ObserverSystem::emitSignal(SignalTypeOne{666});
ObserverSystem::disconnect(h, &Hello::f);
ObserverSystem::disconnect(g, &Hello::f);
ObserverSystem::disconnect(v2, &Some::f1);
ObserverSystem::disconnect(v1, &Some::f2);
ObserverSystem::disconnect(v2, &Some::f2);
ObserverSystem::emitSignal(SignalTypeTwo{100,100});
ObserverSystem::emitSignal(SignalTypeOne{777});
return 0;
}
Exemplary output:
0x7ffe51692a18 : a: 123
b: 444
0x7ffe51692a10 : a: 321
b: 444
0x7ffe51692a08: vx = 1, vy = 1
0x7ffe51692a00: vx = 1, vy = 1
0x7ffe51692a10 : a: 321
b: 555
0x7ffe51692a08: vx = 1, vy = 1
0x7ffe51692a00: vx = 2, vy = 3
0x7ffe51692a10 : a: 321
b: 666
0x7ffe51692a18 : a: 123
b: 666
0x7ffe51692a08: vx = 1, vy = 1
0x7ffe51692a00: vx = 4, vy = 4
A function should always get disconnected when it gets destroyed.
c++ template event-handling observer-pattern
add a comment |
Signals are represented as types. You can connect a member function of some instance to the Observer system. This connected function will be called whenever a signal of a type that is the same type as the function parameter is emitted.
ObserverSystem.hpp:
#include <vector>
class ObserverSystem
{
public:
template<typename SignalType>
inline static void emitSignal(const SignalType& signal)
{
for(auto f : callEverything<SignalType>)
{
f(signal);
}
}
template<typename SignalType, typename ObjectType>
inline static void connect(ObjectType& object, void (ObjectType::*function)(const SignalType& signal))
{
bool add = true;
for(auto& entry : functions<SignalType, ObjectType>)
{
if(entry.function == function && entry.object == &object)
{
add = false;
break;
}
}
if(add)
{
functions<SignalType, ObjectType>.emplace_back(ObjectFunction<SignalType, ObjectType>{&object, function});
}
add = true;
for(auto& entry : callEverything<SignalType>)
{
if(entry == callAll<SignalType, ObjectType>)
{
add = false;
break;
}
}
if(add)
{
callEverything<SignalType>.emplace_back(callAll<SignalType, ObjectType>);
}
}
template<typename SignalType, typename ObjectType>
inline static void disconnect(ObjectType& object, void (ObjectType::*function)(const SignalType& signal))
{
for(size_t i = 0; i < functions<SignalType, ObjectType>.size();)
{
if(functions<SignalType, ObjectType>[i].function == function && functions<SignalType, ObjectType>[i].object == &object)
{
functions<SignalType, ObjectType>.erase(functions<SignalType, ObjectType>.begin() + i);
}
else
{
i++;
}
}
}
private:
template<typename SignalType, typename ObjectType>
struct ObjectFunction
{
ObjectType* object;
void (ObjectType::*function)(const SignalType& signal);
void call(const SignalType& signal)
{
(object->*function)(signal);
}
};
template<typename SignalType, typename ObjectType>
inline static std::vector<ObjectFunction<SignalType, ObjectType>> functions;
template<typename SignalType, typename ObjectType>
inline static void callAll(const SignalType& signal)
{
for(auto function : functions<SignalType, ObjectType>)
{
function.call(signal);
}
}
template<typename SignalType>
inline static std::vector<void (*)(const SignalType& signal)> callEverything;
};
main.cpp
#include <iostream>
#include "ObserverSystem.hpp"
struct SignalTypeOne
{
int b;
};
struct SignalTypeTwo
{
int x;
int y;
};
struct Hello
{
int a = 123;
void f(const SignalTypeOne& signal)
{
std::cout << (int*)this << " : a: " << a << std::endl;
std::cout << "b: " << signal.b << std::endl;
}
};
struct Some
{
int vx = 0;
int vy = 0;
void f1(const SignalTypeTwo& signal)
{
vx += signal.x;
vy += signal.y;
}
void f2(const SignalTypeOne& signal)
{
std::cout << (int*)this << ": " << "vx = " << vx <<", vy = " << vy << std::endl;
}
};
int main()
{
Hello h;
Hello g = Hello{321};
Some v1;
Some v2;
ObserverSystem::connect(h, &Hello::f);
ObserverSystem::connect(g, &Hello::f);
ObserverSystem::connect(v1, &Some::f1);
ObserverSystem::connect(v2, &Some::f1);
ObserverSystem::connect(v1, &Some::f2);
ObserverSystem::connect(v2, &Some::f2);
ObserverSystem::emitSignal(SignalTypeTwo{1,1});
ObserverSystem::emitSignal(SignalTypeOne{444});
ObserverSystem::disconnect(h, &Hello::f);
ObserverSystem::disconnect(v1, &Some::f1);
ObserverSystem::emitSignal(SignalTypeTwo{1,2});
ObserverSystem::emitSignal(SignalTypeOne{555});
ObserverSystem::connect(h, &Hello::f);
ObserverSystem::emitSignal(SignalTypeTwo{2,1});
ObserverSystem::emitSignal(SignalTypeOne{666});
ObserverSystem::disconnect(h, &Hello::f);
ObserverSystem::disconnect(g, &Hello::f);
ObserverSystem::disconnect(v2, &Some::f1);
ObserverSystem::disconnect(v1, &Some::f2);
ObserverSystem::disconnect(v2, &Some::f2);
ObserverSystem::emitSignal(SignalTypeTwo{100,100});
ObserverSystem::emitSignal(SignalTypeOne{777});
return 0;
}
Exemplary output:
0x7ffe51692a18 : a: 123
b: 444
0x7ffe51692a10 : a: 321
b: 444
0x7ffe51692a08: vx = 1, vy = 1
0x7ffe51692a00: vx = 1, vy = 1
0x7ffe51692a10 : a: 321
b: 555
0x7ffe51692a08: vx = 1, vy = 1
0x7ffe51692a00: vx = 2, vy = 3
0x7ffe51692a10 : a: 321
b: 666
0x7ffe51692a18 : a: 123
b: 666
0x7ffe51692a08: vx = 1, vy = 1
0x7ffe51692a00: vx = 4, vy = 4
A function should always get disconnected when it gets destroyed.
c++ template event-handling observer-pattern
add a comment |
Signals are represented as types. You can connect a member function of some instance to the Observer system. This connected function will be called whenever a signal of a type that is the same type as the function parameter is emitted.
ObserverSystem.hpp:
#include <vector>
class ObserverSystem
{
public:
template<typename SignalType>
inline static void emitSignal(const SignalType& signal)
{
for(auto f : callEverything<SignalType>)
{
f(signal);
}
}
template<typename SignalType, typename ObjectType>
inline static void connect(ObjectType& object, void (ObjectType::*function)(const SignalType& signal))
{
bool add = true;
for(auto& entry : functions<SignalType, ObjectType>)
{
if(entry.function == function && entry.object == &object)
{
add = false;
break;
}
}
if(add)
{
functions<SignalType, ObjectType>.emplace_back(ObjectFunction<SignalType, ObjectType>{&object, function});
}
add = true;
for(auto& entry : callEverything<SignalType>)
{
if(entry == callAll<SignalType, ObjectType>)
{
add = false;
break;
}
}
if(add)
{
callEverything<SignalType>.emplace_back(callAll<SignalType, ObjectType>);
}
}
template<typename SignalType, typename ObjectType>
inline static void disconnect(ObjectType& object, void (ObjectType::*function)(const SignalType& signal))
{
for(size_t i = 0; i < functions<SignalType, ObjectType>.size();)
{
if(functions<SignalType, ObjectType>[i].function == function && functions<SignalType, ObjectType>[i].object == &object)
{
functions<SignalType, ObjectType>.erase(functions<SignalType, ObjectType>.begin() + i);
}
else
{
i++;
}
}
}
private:
template<typename SignalType, typename ObjectType>
struct ObjectFunction
{
ObjectType* object;
void (ObjectType::*function)(const SignalType& signal);
void call(const SignalType& signal)
{
(object->*function)(signal);
}
};
template<typename SignalType, typename ObjectType>
inline static std::vector<ObjectFunction<SignalType, ObjectType>> functions;
template<typename SignalType, typename ObjectType>
inline static void callAll(const SignalType& signal)
{
for(auto function : functions<SignalType, ObjectType>)
{
function.call(signal);
}
}
template<typename SignalType>
inline static std::vector<void (*)(const SignalType& signal)> callEverything;
};
main.cpp
#include <iostream>
#include "ObserverSystem.hpp"
struct SignalTypeOne
{
int b;
};
struct SignalTypeTwo
{
int x;
int y;
};
struct Hello
{
int a = 123;
void f(const SignalTypeOne& signal)
{
std::cout << (int*)this << " : a: " << a << std::endl;
std::cout << "b: " << signal.b << std::endl;
}
};
struct Some
{
int vx = 0;
int vy = 0;
void f1(const SignalTypeTwo& signal)
{
vx += signal.x;
vy += signal.y;
}
void f2(const SignalTypeOne& signal)
{
std::cout << (int*)this << ": " << "vx = " << vx <<", vy = " << vy << std::endl;
}
};
int main()
{
Hello h;
Hello g = Hello{321};
Some v1;
Some v2;
ObserverSystem::connect(h, &Hello::f);
ObserverSystem::connect(g, &Hello::f);
ObserverSystem::connect(v1, &Some::f1);
ObserverSystem::connect(v2, &Some::f1);
ObserverSystem::connect(v1, &Some::f2);
ObserverSystem::connect(v2, &Some::f2);
ObserverSystem::emitSignal(SignalTypeTwo{1,1});
ObserverSystem::emitSignal(SignalTypeOne{444});
ObserverSystem::disconnect(h, &Hello::f);
ObserverSystem::disconnect(v1, &Some::f1);
ObserverSystem::emitSignal(SignalTypeTwo{1,2});
ObserverSystem::emitSignal(SignalTypeOne{555});
ObserverSystem::connect(h, &Hello::f);
ObserverSystem::emitSignal(SignalTypeTwo{2,1});
ObserverSystem::emitSignal(SignalTypeOne{666});
ObserverSystem::disconnect(h, &Hello::f);
ObserverSystem::disconnect(g, &Hello::f);
ObserverSystem::disconnect(v2, &Some::f1);
ObserverSystem::disconnect(v1, &Some::f2);
ObserverSystem::disconnect(v2, &Some::f2);
ObserverSystem::emitSignal(SignalTypeTwo{100,100});
ObserverSystem::emitSignal(SignalTypeOne{777});
return 0;
}
Exemplary output:
0x7ffe51692a18 : a: 123
b: 444
0x7ffe51692a10 : a: 321
b: 444
0x7ffe51692a08: vx = 1, vy = 1
0x7ffe51692a00: vx = 1, vy = 1
0x7ffe51692a10 : a: 321
b: 555
0x7ffe51692a08: vx = 1, vy = 1
0x7ffe51692a00: vx = 2, vy = 3
0x7ffe51692a10 : a: 321
b: 666
0x7ffe51692a18 : a: 123
b: 666
0x7ffe51692a08: vx = 1, vy = 1
0x7ffe51692a00: vx = 4, vy = 4
A function should always get disconnected when it gets destroyed.
c++ template event-handling observer-pattern
Signals are represented as types. You can connect a member function of some instance to the Observer system. This connected function will be called whenever a signal of a type that is the same type as the function parameter is emitted.
ObserverSystem.hpp:
#include <vector>
class ObserverSystem
{
public:
template<typename SignalType>
inline static void emitSignal(const SignalType& signal)
{
for(auto f : callEverything<SignalType>)
{
f(signal);
}
}
template<typename SignalType, typename ObjectType>
inline static void connect(ObjectType& object, void (ObjectType::*function)(const SignalType& signal))
{
bool add = true;
for(auto& entry : functions<SignalType, ObjectType>)
{
if(entry.function == function && entry.object == &object)
{
add = false;
break;
}
}
if(add)
{
functions<SignalType, ObjectType>.emplace_back(ObjectFunction<SignalType, ObjectType>{&object, function});
}
add = true;
for(auto& entry : callEverything<SignalType>)
{
if(entry == callAll<SignalType, ObjectType>)
{
add = false;
break;
}
}
if(add)
{
callEverything<SignalType>.emplace_back(callAll<SignalType, ObjectType>);
}
}
template<typename SignalType, typename ObjectType>
inline static void disconnect(ObjectType& object, void (ObjectType::*function)(const SignalType& signal))
{
for(size_t i = 0; i < functions<SignalType, ObjectType>.size();)
{
if(functions<SignalType, ObjectType>[i].function == function && functions<SignalType, ObjectType>[i].object == &object)
{
functions<SignalType, ObjectType>.erase(functions<SignalType, ObjectType>.begin() + i);
}
else
{
i++;
}
}
}
private:
template<typename SignalType, typename ObjectType>
struct ObjectFunction
{
ObjectType* object;
void (ObjectType::*function)(const SignalType& signal);
void call(const SignalType& signal)
{
(object->*function)(signal);
}
};
template<typename SignalType, typename ObjectType>
inline static std::vector<ObjectFunction<SignalType, ObjectType>> functions;
template<typename SignalType, typename ObjectType>
inline static void callAll(const SignalType& signal)
{
for(auto function : functions<SignalType, ObjectType>)
{
function.call(signal);
}
}
template<typename SignalType>
inline static std::vector<void (*)(const SignalType& signal)> callEverything;
};
main.cpp
#include <iostream>
#include "ObserverSystem.hpp"
struct SignalTypeOne
{
int b;
};
struct SignalTypeTwo
{
int x;
int y;
};
struct Hello
{
int a = 123;
void f(const SignalTypeOne& signal)
{
std::cout << (int*)this << " : a: " << a << std::endl;
std::cout << "b: " << signal.b << std::endl;
}
};
struct Some
{
int vx = 0;
int vy = 0;
void f1(const SignalTypeTwo& signal)
{
vx += signal.x;
vy += signal.y;
}
void f2(const SignalTypeOne& signal)
{
std::cout << (int*)this << ": " << "vx = " << vx <<", vy = " << vy << std::endl;
}
};
int main()
{
Hello h;
Hello g = Hello{321};
Some v1;
Some v2;
ObserverSystem::connect(h, &Hello::f);
ObserverSystem::connect(g, &Hello::f);
ObserverSystem::connect(v1, &Some::f1);
ObserverSystem::connect(v2, &Some::f1);
ObserverSystem::connect(v1, &Some::f2);
ObserverSystem::connect(v2, &Some::f2);
ObserverSystem::emitSignal(SignalTypeTwo{1,1});
ObserverSystem::emitSignal(SignalTypeOne{444});
ObserverSystem::disconnect(h, &Hello::f);
ObserverSystem::disconnect(v1, &Some::f1);
ObserverSystem::emitSignal(SignalTypeTwo{1,2});
ObserverSystem::emitSignal(SignalTypeOne{555});
ObserverSystem::connect(h, &Hello::f);
ObserverSystem::emitSignal(SignalTypeTwo{2,1});
ObserverSystem::emitSignal(SignalTypeOne{666});
ObserverSystem::disconnect(h, &Hello::f);
ObserverSystem::disconnect(g, &Hello::f);
ObserverSystem::disconnect(v2, &Some::f1);
ObserverSystem::disconnect(v1, &Some::f2);
ObserverSystem::disconnect(v2, &Some::f2);
ObserverSystem::emitSignal(SignalTypeTwo{100,100});
ObserverSystem::emitSignal(SignalTypeOne{777});
return 0;
}
Exemplary output:
0x7ffe51692a18 : a: 123
b: 444
0x7ffe51692a10 : a: 321
b: 444
0x7ffe51692a08: vx = 1, vy = 1
0x7ffe51692a00: vx = 1, vy = 1
0x7ffe51692a10 : a: 321
b: 555
0x7ffe51692a08: vx = 1, vy = 1
0x7ffe51692a00: vx = 2, vy = 3
0x7ffe51692a10 : a: 321
b: 666
0x7ffe51692a18 : a: 123
b: 666
0x7ffe51692a08: vx = 1, vy = 1
0x7ffe51692a00: vx = 4, vy = 4
A function should always get disconnected when it gets destroyed.
c++ template event-handling observer-pattern
c++ template event-handling observer-pattern
edited 19 mins ago
asked 44 mins ago
Darius Duesentrieb
1937
1937
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210554%2fobserverevent-system-signals-and-slots-type-and-template-based%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210554%2fobserverevent-system-signals-and-slots-type-and-template-based%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown