C++ simple signal lib
//typedef function0 <int> FunctionType;
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <vector>
#include <algorithm>
using namespace boost;
using namespace std;
typedef int RType;
typedef RType (*FunctionType)();
class MySlot
{
public:
bool IsValid() {return fun_!=0;}
//bool operator == (MySlot const &rls) {return this==&rls;}
//protected:
//private:
//friend class MyConnection;
FunctionType fun_;
};
class MySignal0;
class MyConnection
{
public:
bool IsValid() {return slot_ && slot_->IsValid();}
void Disconnect();
bool operator == (MyConnection const &rls) {return signal_ == rls.signal_ && rls.slot_ == slot_;}
//protected:
//private:
//friend class MySignal0;
shared_ptr<MySlot> slot_; //for see connection's state
MySignal0* signal_;//for disconnect
};
class MySignal0
{
public:
//MyConnection Connect(const FunctionType & fun)
MyConnection Connect(FunctionType fun)
{
MyConnection con;
con.slot_.reset(new MySlot);
con.slot_->fun_ = fun;
con.signal_ = this;
cons_.push_back(con);
return con;
}
RType operator()()
{
vector <MyConnection>::iterator it = cons_.begin();
vector <MyConnection>::iterator itEnd = cons_.end();
RType r = RType();
for(;it!=itEnd;++it)
{
if((*it).slot_)
r = (*it).slot_->fun_();
}
return r;
}
//protected:
//private:
void Remove(MyConnection const &con)
{
vector <MyConnection>::iterator it = std::find(cons_.begin(),cons_.end(),con);
if(it!=cons_.end())
{
(*it).slot_.reset();
std::remove(cons_.begin(),cons_.end(),con);
cout<<"Remove Succeed.\n";
}
else
cout<<"Remove Fail.\n";
}
vector <MyConnection> cons_;
};
void MyConnection::Disconnect(){
signal_->Remove(*this);
}
int f() {
cout<<"f()\n";
return 0;
}
void main1()
{
MySignal0 sig;
MyConnection con = sig.Connect(f);
sig();
con.Disconnect();
sig();
}
void main()
{
MyConnection con;
cout<<"after init con:\n";
cout<<"con is_valid:"<<con.IsValid()<<endl;
{
MySignal0 sig;
cout<<"after connect to sig:\n";
con = sig.Connect(f);
sig();
cout<<"con is_valid:"<<con.IsValid()<<endl;
cout<<"after disconnect:\n";
con.Disconnect();
sig();
cout<<"con is_valid:"<<con.IsValid()<<endl;
}
cout<<"after signal destruct:\n";
cout<<"con is_valid:"<<con.IsValid()<<endl;
}
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <vector>
#include <algorithm>
using namespace boost;
using namespace std;
typedef int RType;
typedef RType (*FunctionType)();
class MySlot
{
public:
bool IsValid() {return fun_!=0;}
//bool operator == (MySlot const &rls) {return this==&rls;}
//protected:
//private:
//friend class MyConnection;
FunctionType fun_;
};
class MySignal0;
class MyConnection
{
public:
bool IsValid() {return slot_ && slot_->IsValid();}
void Disconnect();
bool operator == (MyConnection const &rls) {return signal_ == rls.signal_ && rls.slot_ == slot_;}
//protected:
//private:
//friend class MySignal0;
shared_ptr<MySlot> slot_; //for see connection's state
MySignal0* signal_;//for disconnect
};
class MySignal0
{
public:
//MyConnection Connect(const FunctionType & fun)
MyConnection Connect(FunctionType fun)
{
MyConnection con;
con.slot_.reset(new MySlot);
con.slot_->fun_ = fun;
con.signal_ = this;
cons_.push_back(con);
return con;
}
RType operator()()
{
vector <MyConnection>::iterator it = cons_.begin();
vector <MyConnection>::iterator itEnd = cons_.end();
RType r = RType();
for(;it!=itEnd;++it)
{
if((*it).slot_)
r = (*it).slot_->fun_();
}
return r;
}
//protected:
//private:
void Remove(MyConnection const &con)
{
vector <MyConnection>::iterator it = std::find(cons_.begin(),cons_.end(),con);
if(it!=cons_.end())
{
(*it).slot_.reset();
std::remove(cons_.begin(),cons_.end(),con);
cout<<"Remove Succeed.\n";
}
else
cout<<"Remove Fail.\n";
}
vector <MyConnection> cons_;
};
void MyConnection::Disconnect(){
signal_->Remove(*this);
}
int f() {
cout<<"f()\n";
return 0;
}
void main1()
{
MySignal0 sig;
MyConnection con = sig.Connect(f);
sig();
con.Disconnect();
sig();
}
void main()
{
MyConnection con;
cout<<"after init con:\n";
cout<<"con is_valid:"<<con.IsValid()<<endl;
{
MySignal0 sig;
cout<<"after connect to sig:\n";
con = sig.Connect(f);
sig();
cout<<"con is_valid:"<<con.IsValid()<<endl;
cout<<"after disconnect:\n";
con.Disconnect();
sig();
cout<<"con is_valid:"<<con.IsValid()<<endl;
}
cout<<"after signal destruct:\n";
cout<<"con is_valid:"<<con.IsValid()<<endl;
}
- 上一篇 C++ lazy evaluation
- 下一篇 Life zz 关于爱情.