问题背景
在下面的代码中,Input输入器 输入数据,希望A和B 接收数据。但使用的赋值,导致in.a和a只是拷贝数据,而不是同一个对象,使得数据不同步。
#include <iostream>
struct A
{int age = 32;
};
struct B
{int age = 10;
};struct Input
{void fun(int i){a.age = i;b.age = i;}A a;B b;
};
int main(int argc, char *argv[])
{Input in;A a;B b;in.a = a;in.b = b;in.fun(3);std::cout<<a.age<<" "<<b.age<<std::endl;//32 10return 0;
}
解决方法1:如下所示,当希望修改in.a的age时能修改到A a的age,需要传指针A,而且还要手动指定in.a = &a
#include <iostream>
struct A
{int age = 32;
};
struct Input
{void fun(int i){a->age = i;std::cout<<a->age<<std::endl;}A* a;
};
int main(int argc, char *argv[])
{Input in;A a;in.a = &a;in.fun(4);//4return 0;
}
解决方法2:使用function实现回调函数,将fun函数的赋值操作写在回调函数中
#include <iostream>
#include<functional>
struct A
{int age = 32;
};
struct Input
{void fun(int i){callback(i);}std::function<void(int)> callback;
};
int main(int argc, char *argv[])
{Input in;A a;in.callback = [&a](int i){a.age = i;std::cout<<a.age<<std::endl;};in.fun(4);//4return 0;
}
方法3:将添加回调函数和执行回调函数抽离出来,实现成Signal信号的形式
#include <iostream>
#include<functional>
#include <vector>
struct Signal
{std::vector<std::function<void(int)>> m_callbacks;void connect(std::function<void(int i)> callback){m_callbacks.push_back(std::move(callback));}void emit(int i){for(auto&& callback: m_callbacks){callback(i);}}
};
struct A
{int age = 32;
};
struct Input
{void fun(int i){on_input.emit(i);}Signal on_input;
};
int main(int argc, char *argv[])
{Input in;A a;in.on_input.connect([&a](int i){a.age = i;std::cout<<a.age<<std::endl;});in.on_input.connect([&a](int i){a.age = i;std::cout<<a.age<<std::endl;});in.fun(4);//4return 0;
}
4:如果类A需要注册一个退出事件on_exit,有如下实现。但实际上我们并不希望在此信号传递参数int i。
#include <iostream>
#include <functional>
#include <vector>
struct Signal
{std::vector<std::function<void(int)>> m_callbacks;void connect(std::function<void(int i)> callback){m_callbacks.push_back(std::move(callback));}void emit(int i){for(auto&& callback: m_callbacks){callback(i);}}
};
struct A
{ void on_input(int i) const {std::cout<<"input "<<age<<std::endl;}void on_exit() const {std::cout<<"exit "<<std::endl;}int age = 32;
};
struct Input
{//调用该函数就发出 进入事件和退出事件的信号void fun(int i){on_input.emit(i);on_exit.emit(i);}Signal on_input;//进入事件Signal on_exit;//退出事件
};
int main(int argc, char *argv[])
{Input in;A a;in.on_input.connect([&a](int i){a.age = i;a.on_input(i);});in.on_exit.connect([&a](int i){a.on_exit();});in.fun(4);//4return 0;
}
5:为了信号更加通用,使用变长模板参数来实现。注意:…在左边表示定义,在右边表示使用
#include <iostream>
#include <functional>
#include <vector>
template<class ...T>//定义T
struct Signal
{std::vector<std::function<void(T...)>> m_callbacks;//使用Tvoid connect(std::function<void(T...)> callback){m_callbacks.push_back(std::move(callback));}void emit(T... t){//使用T, 定义tfor(auto&& callback: m_callbacks){callback(t...);//使用t}}
};
struct A
{ void on_input(int i) const {std::cout<<"input "<<age<<std::endl;}void on_exit() const {std::cout<<"exit "<<std::endl;}int age = 32;
};
struct Input
{//调用该函数就发出 进入事件和退出事件的信号void fun(int i){on_input.emit(i);on_exit.emit();}Signal<int> on_input;//进入事件Signal<> on_exit;//退出事件
};
int main(int argc, char *argv[])
{Input in;A a;in.on_input.connect([&a](int i){a.age = i;a.on_input(i);});in.on_exit.connect([&a](){a.on_exit();});in.fun(4);//4return 0;
}
6:上述代码main函数中connect时传入的lambda表达式,下面在Signal中将其封装为bind方法,并提供对应的connect函数,使其更类似于Qt信号的connect。实际上Qt中是使用字符串来查找匹配的类型名,而这里我们使用模板更加高效
#include <iostream>
#include <functional>
#include <vector>template<class Self, class MemFn>
auto bind(Self *self, MemFn memfn){//第2个参数为成员函数指针:void(A::*)(int i),这里使用模板来避免写成该复杂类型//调用成员函数指针(a->*memfn)()。如果是普通函数指针,就是(*memfn)()这样调用return [self, memfn](auto... t){(self->*memfn)(t...);};
}template<class ...T>//定义T
struct Signal
{std::vector<std::function<void(T...)>> m_callbacks;//使用Tvoid connect(std::function<void(T...)> callback){m_callbacks.push_back(std::move(callback));}//提供一个bind版本的connect,类似qt语法template<class Self, class MemFn>void connect(Self *self, MemFn memfn){m_callbacks.push_back(bind(self, memfn));}void emit(T... t){//使用T, 定义tfor(auto&& callback: m_callbacks){callback(t...);//使用t}}
};
struct A
{ void on_input(int i){age = i;std::cout<<"input "<<age<<std::endl;}void on_exit(std::string s) const {std::cout<<"exit "<<s<<std::endl;}int age = 32;
};
struct Input
{//调用该函数就发出 进入事件和退出事件的信号void fun(int i){on_input.emit(i);on_exit.emit("byebye");}Signal<int> on_input;//进入事件Signal<std::string> on_exit;//退出事件
};int main(int argc, char *argv[])
{Input in;A a;in.on_input.connect(&a, &A::on_input);in.on_exit.connect(&a, &A::on_exit);in.fun(4);//4return 0;
}
下面写了重载函数test_fun作为要connect的函数,此时必须写明要使用哪个函数,因此下面使用static_cast进行转换
void test_fun(int m){std::cout<<"int "<<m<<std::endl;
}
void test_fun(std::string m){std::cout<<"string "<<m<<std::endl;
}
//function要求必须有唯一的重载,这样必须指定使用哪个
in.on_input.connect(static_cast<void(*)(int)>(test_fun));
7:为了避免connect的对象提前析构,下面代码使用智能指针
template<class Self, class MemFn>
auto bind(Self self, MemFn memfn){return [self, memfn](auto... t){((*self).*memfn)(t...);};
}void test2(Input &input){auto a = std::make_shared<A>();//使用智能指针而不是A a,避免对象提前析构input.on_input.connect(a, &A::on_input);//这里智能指针a发生拷贝input.on_exit.connect(a, &A::on_exit);
}int main(int argc, char *argv[])
{Input in;test2(in);in.fun(3);return 0;
}
如果是connect lambda表达式,注意按值捕获,否则智能指针和a对象都会提前析构掉
void test2(Input &input){auto a = std::make_shared<A>();input.on_input.connect([a](int i){//注意按值捕获areturn a->on_input(i);});
}