0
我在我們的項目之一中使用了Boost Signals2。在這個我想要自動連接管理,爲此我正在測試Boost Signals2跟蹤,但我沒有獲得槽位調用。 運行以下代碼後,不會調用插槽。 環境: VS 2010,Windows 7中,提高1.54Boost Signals2跟蹤
#include <stdio.h>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/signals2/signal.hpp>
#include <boost/shared_ptr.hpp>
typedef boost::signals2::signal<void()> signal_test;
using namespace boost;
class SubjectTest
{
public:
void Connect(const signal_test::slot_type &subscriber)
{
m_Signal.connect(subscriber);
std::cout << "No of connections : " << m_Signal.num_slots() << std::endl;
}
void Notify()
{
m_Signal();
}
private:
signal_test m_Signal;
};
class Listner
{
public:
Listner(){}
~Listner(){}
Listner(std::string name)
:m_name(name)
{
}
void GotSignal()
{
std::cout << m_name << std::endl;
}
void ConnectWithTracking(SubjectTest *s)
{
shared_ptr<Listner> l = new Listner();
s->Connect(signal_test::slot_type(&Listner::GotSignal,l.get()).track(l));
}
void ConnectNormal(SubjectTest *s)
{
s->Connect(bind(&Listner::GotSignal,this));
}
private:
std::string m_name;
shared_ptr<Listner> l;
};
void main()
{
Listner l2("First");
SubjectTest sub;
try
{
l2.ConnectWithTracking(&sub);
//l2.ConnectNormal(&sub);
sub.Notify();
{
Listner l3("Second");
l3.ConnectWithTracking(&sub);
//l3.ConnectNormal(&sub);
sub.Notify();
}
sub.Notify();
}
catch(std::exception ex)
{
std::cout << ex.what() << std::endl;
}
std::cout << "Finish" <<std::endl;
}
更新時間: * 工作現在 *
#include <stdio.h>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/signals2/signal.hpp>
#include <boost/shared_ptr.hpp>
typedef boost::signals2::signal<void()> signal_test;
using namespace boost;
class SubjectTest
{
public:
void Connect(const signal_test::slot_type &subscriber)
{
m_Signal.connect(subscriber);
std::cout << "No of connections : " << m_Signal.num_slots() << std::endl;
}
void Notify()
{
m_Signal();
}
private:
signal_test m_Signal;
};
class Listner : public boost::enable_shared_from_this<Listner>
{
public:
Listner(){}
~Listner(){}
Listner(std::string name)
:m_name(name)
{
}
void GotSignal()
{
std::cout << m_name << std::endl;
}
void ConnectWithTracking(SubjectTest *s)
{
s->Connect(signal_test::slot_type(&Listner::GotSignal,shared_from_this().get()).track(shared_from_this()));
}
void ConnectNormal(SubjectTest *s)
{
s->Connect(bind(&Listner::GotSignal,this));
}
private:
std::string m_name;
};
void main()
{
shared_ptr<Listner> l2(new Listner("First"));
SubjectTest sub;
try
{
l2->ConnectWithTracking(&sub);
sub.Notify();
{
shared_ptr<Listner> l3(new Listner("Second"));
l3->ConnectWithTracking(&sub);
//l3.ConnectNormal(&sub);
sub.Notify();
}
sub.Notify();
}
catch(std::exception ex)
{
std::cout << ex.what() << std::endl;
}
std::cout << "Finish" <<std::endl;
}
現在,這是SIGNAL2自動連接管理的完整的例子
你可以建議,我應該在哪裏創建共享指針? – Rushikesh
@Rushikesh你應該改變設計:從'enable_shared_from_this'繼承'Listener',使用'new'來實例化它,並且僅通過'shared_ptr'來管理實例。然後你就可以跟蹤它的使用''shared_from_this()''''track()''函數的生命週期。 –
謝謝,更新的解決方案。如果錯了,請評論 – Rushikesh