在Qt中對象間通訊,普遍使用了信號槽機制。Qt信號機制是經過語法擴展實現的,所以定製性更好,使用更便利。 這篇文章所實現的信號槽機制,則沒有擴展語法,經過簡單的模板實現。ios
#include <iostream>
#include <memory>
#include <string>
#include <functional>
#include <vector>
#define signals public
#define slots
template<typename Signature> class Signal;
template<typename Ret, typename... Args>
class Signal<Ret(Args...)> final
{
using Slot = std::function<void(Args&&...)>;
public:
Signal(){}
void connect(const Slot &slot)
{
slots_.push_back(slot);
}
void emit(Args&&... args)
{
for(auto &slot : slots_){
slot(std::forward<Args>(args)...);
}
}
private:
std::vector<Slot> slots_;
};
template<typename Ret, typename Class, typename... Args>
class Signal<Ret(Class::*)(Args...)> final
{
using Slot = std::function<void(Class::*)(Args&&...)>;
public:
Signal(){}
void connect(const Slot &slot)
{
slots_.push_back(slot);
}
void emit(Args&&... args)
{
for(auto &slot : slots_){
slot(std::forward<Args>(args)...);
}
}
private:
std::vector<Slot> slots_;
};
//------------------------ 使用 -----------------------------------
struct Observable
{
public slots:
void onAction1(int v)
{
std::cout << "action1: " << v << std::endl;
}
void onAction2(int v, const std::string &s)
{
std::cout << "action2: " << v << " s: " << s << std::endl;
}
void onActionPass(int v, std::string s, double d)
{
std::cout << "action2: " << v << " s: " << s << " d: " << d << std::endl;
}
};
struct Observer
{
signals: //定義信號對象,必須和槽函數參數列表一致。
Signal<void(int)> action1;
Signal<void(int, std::string, double)> action2;
};
int main(int argc, char *argv[])
{
Observer observer;
Observable observable;
//方式1 使用observer的信號對象進行鏈接槽函數 function。
observer.action1.connect(std::bind(&Observable::onAction1, &observable, std::placeholders::_1));
observer.action2.connect(std::bind(&Observable::onAction2, &observable, std::placeholders::_1, std::placeholders::_2));
//方式2 使用observer的信號對象進行鏈接槽函數 lambda。
observer.action1.connect([&observable](int v) { observable.onAction1(v); });
observer.action2.connect([&observable](int v, std::string s, double d) { observable.onActionPass(v, s, d); });
observer.action1.emit(10);
observer.action2.emit(22, "hello world", 10.44);
return 0;函數
————————————————
版權聲明:本文爲CSDN博主「十萬個爲什貓」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接及本聲明。
原文連接:https://blog.csdn.net/zhiguangli123/article/details/88798961.net