信號槽

以下代碼出自陳碩,是一個信號的槽的實現(xiàn)。

#define _CRT_SECURE_NO_WARNINGS
#include <functional>
#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
#include <string>
#include <cassert>
#include <stdio.h>
using namespace std;

template<typename Signature>
class SignalTrivial;
using placeholders::_1;
using placeholders::_2;


template<typename Callback>
struct SlotImpl;

template<typename Callback>
struct SignalImpl 
{
    typedef std::vector<weak_ptr<SlotImpl<Callback> > > SlotList;

    SignalImpl()
        : slots_(new SlotList)
    {
    }

    void copyOnWrite()
    {
        //mutex_.assertLocked();
        if (!slots_.unique())
        {
            slots_.reset(new SlotList(*slots_));
        }
        assert(slots_.unique());
    }

    void clean()
    {
        //MutexLockGuard lock(mutex_);
        copyOnWrite();
        SlotList& list(*slots_);
        typename SlotList::iterator it(list.begin());
        while (it != list.end())
        {
            if (it->expired())
            {
                it = list.erase(it);
            }
            else
            {
                ++it;
            }
        }
    }

    //MutexLock mutex_;
    shared_ptr<SlotList> slots_;
};

template<typename Callback>
struct SlotImpl
{
    typedef SignalImpl<Callback> Data;
    SlotImpl(const shared_ptr<Data>& data, Callback&& cb)
        : data_(data), cb_(cb), tie_(), tied_(false)
    {
    }

    SlotImpl(const shared_ptr<Data>& data, Callback&& cb,
        const shared_ptr<void>& tie)
        : data_(data), cb_(cb), tie_(tie), tied_(true)
    {
    }

    ~SlotImpl()
    {
        printf("~SlotImpl\n");
        shared_ptr<Data> data(data_.lock());
        if (data)
        {
            data->clean();
        }
    }

    weak_ptr<Data> data_;
    Callback cb_;
    weak_ptr<void> tie_;
    bool tied_;
};

/// This is the handle for a slot
///
/// The slot will remain connected to the signal fot the life time of the
/// returned Slot object (and its copies).
typedef shared_ptr<void> Slot;

template<typename Signature>
class Signal;

template <typename RET, typename... ARGS>
class Signal<RET(ARGS...)>
{
public:
    typedef std::function<void(ARGS...)> Callback;
    typedef SignalImpl<Callback> SignalImpl;
    typedef SlotImpl<Callback> SlotImpl;

    Signal()
        : impl_(new SignalImpl)
    {
    }

    ~Signal()
    {
    }
    // connect 其實為寫shared_ptr<SignalImpl> impl_.slots_;
    Slot connect(Callback&& func)
    {
        shared_ptr<SlotImpl> slotImpl(
            new SlotImpl(impl_, std::forward<Callback>(func)));
        add(slotImpl);
        return slotImpl;
    }

    Slot connect(Callback&& func, const shared_ptr<void>& tie)
    {
        shared_ptr<SlotImpl> slotImpl(new SlotImpl(impl_, func, tie));
        add(slotImpl);
        return slotImpl;
    }
    // call 其實為讀shared_ptr<SignalImpl> impl_.slots_;
    void call(ARGS&&... args)
    {
        SignalImpl& impl(*impl_);
        shared_ptr<typename SignalImpl::SlotList> slots;
        {
            //MutexLockGuard lock(impl.mutex_);
            slots = impl.slots_;  // 防止寫這塊內存;
        }
        typename SignalImpl::SlotList& s(*slots);
        for (typename SignalImpl::SlotList::const_iterator it = s.begin(); it != s.end(); ++it)
        {
            shared_ptr<SlotImpl> slotImpl = it->lock();
            if (slotImpl)
            {
                shared_ptr<void> guard;
                if (slotImpl->tied_)
                {
                    guard = slotImpl->tie_.lock();
                    if (guard)
                    {
                        slotImpl->cb_(args...);
                    }
                }
                else
                {
                    slotImpl->cb_(args...);
                }
            }
        }
    }

private:

    void add(const shared_ptr<SlotImpl>& slot)
    {
        SignalImpl& impl(*impl_);
        {
            //MutexLockGuard lock(impl.mutex_);
            //impl.copyOnWrite();
            impl.slots_->push_back(slot);
        }
    }

    const shared_ptr<SignalImpl> impl_;
};

class String
{
public:
    String(const char* str)
    {
        printf("String ctor this %p\n", this);
    }

    String(const String& rhs)
    {
        printf("String copy ctor this %p, rhs %p\n", this, &rhs);
    }

    String(String&& rhs)
    {
        printf("String move ctor this %p, rhs %p\n", this, &rhs);
    }
};


class Foo 
{
public:
    ~Foo(){
        i = 0;
        printf("~Foo()\n");
    }
    void zero();
    void zeroc() const;
    void one(int);
    void oner(int&);
    void onec(int) const;
    void oneString(const String& str);
    // void oneStringRR(String&& str);
    static void szero();
    static void sone(int);
    static void soneString(const String& str);
private:
    int i = 2;
};

void Foo::zero()
{
    ++i;
    printf("Foo::zero() = %d\n",i);
}

void Foo::zeroc() const
{
    printf("Foo::zeroc()\n");
}

void Foo::szero()
{
    printf("Foo::szero()\n");
}

void Foo::one(int x)
{
    printf("Foo::one() x=%d\n", x);
}

void Foo::onec(int x) const
{
    printf("Foo::onec() x=%d\n", x);
}

void Foo::sone(int x)
{
    printf("Foo::sone() x=%d\n", x);
}

void Foo::oneString(const String& str)
{
    printf("Foo::oneString\n");
}

void Foo::soneString(const String& str)
{
    printf("Foo::soneString\n");
}


void testSignalSlotZero()
{
    Signal<void()> signal;

    printf("==== testSignalSlotZero ====\n");
    signal.call();

    Slot s1 = signal.connect(&Foo::szero);

    printf("========\n");
    signal.call();

    Foo f;
    Slot s2 = signal.connect(bind(&Foo::zero, &f));

    printf("========\n");
    signal.call();

    Slot s3 = signal.connect(bind(&Foo::one, &f, 42));

    printf("========\n");
    signal.call();

    const Foo cf;
    Slot s4 = signal.connect(bind(&Foo::zeroc, &cf));

    printf("========\n");
    signal.call();

    Slot s5 = signal.connect(bind(&Foo::onec, &cf, 128));

    printf("========\n");
    signal.call();

    s1 = Slot();
    printf("========\n");
    signal.call();


    s4 = s3 = s2 = Slot();
    printf("========\n");
    signal.call();

}

void testSignalSlotOne()
{
    Signal<void(int)> signal;

    printf("========\n");
    signal.call(50);

    Slot s4;
    {
        Slot s1 = signal.connect(&Foo::sone);

        printf("========\n");
        signal.call(51);

        Foo f;
        Slot s2 = signal.connect(bind(&Foo::one, &f, _1));

        printf("========\n");
        signal.call(52);

        const Foo cf;
        Slot s3 = signal.connect(bind(&Foo::onec, &cf, _1));

        printf("========\n");
        signal.call(53);

        s4 = s3;
    }

    printf("========\n");
    signal.call(54);
}

void testSignalSlotLife()
{
    Slot s1;
    {
        Signal<void()> signal;
        signal.connect(&Foo::szero);

        printf("========\n");
        signal.call();

        Foo f;
        function<void()> func = bind(&Foo::zero, &f);

        s1 = signal.connect(bind(&Foo::zero, &f));

        printf("========\n");
        signal.call();
    }
    
}

Signal<void(int)> signal;

class Test
{
public:
    Test()
    {
        s1 = signal.connect(bind(&Test::hello, this, _1));
        s2 = signal.connect(bind(&Test::hello1, this, _1));
    }

    ~Test()
    {
        printf("~Test()\n");
    }
    void hello(int n)
    {
        printf("Test::hello\n");
    }

    void hello1(int n)
    {
        printf("Test::hello1\n");
    }
private:
    Slot s1;
    Slot s2;
};

int main()
{
    shared_ptr<Test>  p(make_shared<Test>());
    p.reset();
    signal.call(4);
    testSignalSlotZero();
    testSignalSlotOne();
    testSignalSlotLife();
    getchar();
    return 0;
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 快速創(chuàng)建qt工程 當我們熟悉基本的從空項目創(chuàng)建qt應用程序后,以后我們可以直接從模板中創(chuàng)建一個qt工程 注意,本節(jié)...
    zhangzq閱讀 444評論 0 0
  • 信號和槽(Signals and Slots) Qt庫第一個認識到在幾乎所有情況下,程序員不需要或甚至不想知道所有...
    珞珈村下山閱讀 10,146評論 0 23
  • PyQt5:PyQt5 信號與槽(PyQt5的事件處理機制) 一、事件 在事件模型,有三個參與者:事件源、事件目標...
    gongdiwudu閱讀 758評論 0 0
  • 信號和槽是用于對象之間的通信的,這是Qt的核心。為此Qt引入了一些關鍵字,他們是slots、signals、emi...
    詩人和酒閱讀 749評論 0 2
  • 1、概述 信號槽是 Qt 框架引以為豪的機制之一。所謂信號槽,實際就是觀察者模式。當某個事件發(fā)生之后,比如,按鈕檢...
    你的社交帳號昵閱讀 45,812評論 0 9

友情鏈接更多精彩內容