#include <iostream>
#include <vector>
#include <functional>
using namespace std;
void fun(int a, int b)
{
cout << a + b << endl;
}
void fun1(int a, int b)
{
cout << a * b << endl;
}
void fun2(int a, int b)
{
cout << a - b << endl;
}
struct SigTest {
void fun(int a, int b)
{
cout << a + b << endl;
}
void fun1(int a, int b)
{
cout << a * b << endl;
}
void fun2(int a, int b)
{
cout << a - b << endl;
}
};
namespace czh {
struct false_type {
static constexpr bool value = false;
};
struct true_type {
static constexpr bool value = true;
};
template<typename T>
struct is_pointer : czh::false_type{};
template<typename T>
struct is_pointer<T*> : czh::true_type {};
template<bool v, typename T>
struct enable_if {};
template<typename T>
struct enable_if<true, T> {
using type = T;
};
}
template<typename ...Args>
struct Signal {
void operator()(Args&& ...args) {
for (auto& slot : slots) {
slot(std::forward<Args>(args)...);
}
}
template<typename Func>
void connect(Func&& func) {
std::function<void(Args...)> f = [&] (Args ...args) { func(args...); };
slots.push_back(f);
}
template<typename Obj, typename Func, typename czh::enable_if<czh::is_pointer<Obj>::value, int>::type = 1>
void connect(Obj&& obj, Func&& func) {
std::function<void(Args...)> f = [&](Args ...args) { (obj->*func)(args...); };
slots.push_back(f);
}
template<typename Obj, typename Func, typename czh::enable_if<!czh::is_pointer<Obj>::value, int>::type = 1>
void connect(Obj&& obj, Func&& func) {
std::function<void(Args...)> f = [&](Args ...args) { (obj.*func)(args...); };
slots.push_back(f);
}
std::vector<std::function<void(Args...)>> slots;
};
int main()
{
Signal<int, int> sig;
sig.connect(fun);
sig.connect(fun1);
sig.connect(fun2);
sig(2, 3);
SigTest sigtest;
Signal<int, int> sig_mem_func;
sig_mem_func.connect(sigtest, &SigTest::fun);
sig_mem_func.connect(sigtest, &SigTest::fun1);
sig_mem_func.connect(sigtest, &SigTest::fun2);
sig_mem_func(3, 4);
Signal<int, int> sig_mem_func_pointer;
sig_mem_func_pointer.connect(&sigtest, &SigTest::fun);
sig_mem_func_pointer.connect(&sigtest, &SigTest::fun1);
sig_mem_func_pointer.connect(&sigtest, &SigTest::fun2);
sig_mem_func_pointer(4, 5);
getchar();
return 0;
}
C++模拟Qt信号槽简易实现
于 2024-01-17 13:37:13 首次发布