C++11 實現簡易的訊號槽。

十萬個為什貓發表於2019-03-25

在Qt中物件間通訊,廣泛使用了訊號槽機制。Qt訊號機制是通過語法擴充套件實現的,因此定製性更好,使用更便利。 這篇文章所實現的訊號槽機制,則沒有擴充套件語法,通過簡單的模板實現。


#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;
}

Output:

相關文章