#include
<
algorithm
>
#include < iostream >
#include < vector >
using namespace std;
template < class T1 >
class SlotBase
{
public :
virtual void Exec(T1 param1) = 0 ;
};
template < class T, class T1 >
class SlotImpl : public SlotBase < T1 >
{
public :
SlotImpl(T * pObj, void (T:: * func)(T1) )
{
m_pObj = pObj;
m_Func = func;
}
void Exec( T1 param1)
{
(m_pObj ->* m_Func)(param1);
}
private :
T * m_pObj;
void (T:: * m_Func)(T1);
};
template < class T1 >
class Slot
{
public :
template < class T >
Slot(T * pObj, void (T:: * func)(T1))
{
m_pSlotBase = new SlotImpl < T,T1 > (pObj, func);
}
~ Slot()
{
delete m_pSlotBase;
}
void Exec(T1 param1)
{
m_pSlotBase -> Exec(param1);
}
private :
SlotBase < T1 >* m_pSlotBase;
};
template < class T1 >
class Signal
{
public :
template < class T >
void Bind(T * pObj, void (T:: * func)(T1))
{
m_pSlotSet.push_back( new Slot < T1 > (pObj,func) );
}
~ Signal()
{
for ( int i = 0 ;i < ( int )m_pSlotSet.size();i ++ )
{
delete m_pSlotSet[i];
}
}
void operator ()(T1 param1)
{
for ( int i = 0 ;i < ( int )m_pSlotSet.size();i ++ )
{
m_pSlotSet[i] -> Exec(param1);
}
}
private :
vector < Slot < T1 >* > m_pSlotSet;
};
#define Connect( sender, signal, receiver, method) ( (sender)->signal.Bind(receiver, method) )
class A
{
public :
void FuncOfA( int param)
{
printf( " A::FuncOfA(%d)\n " , param);
}
};
class B
{
public :
void FuncOfB( int param)
{
printf( " B::FuncOfB(%d)\n " , param);
}
};
class C
{
public :
C()
{
m_Value = 0 ;
}
void SetValue( int value)
{
if (m_Value != value)
{
m_Value = value;
ValueChanged(m_Value);
}
}
public :
Signal < int > ValueChanged;
private :
int m_Value;
};
int main()
{
A * pA = new A;
B * pB = new B;
C * pC = new C;
Connect(pC, ValueChanged, pA, & A::FuncOfA);
Connect(pC, ValueChanged, pB, & B::FuncOfB);
pC -> SetValue( 10 );
pC -> SetValue( 5 );
pC -> SetValue( 5 );
delete pC;
delete pB;
delete pA;
scanf( " %*s " );
}
#include < iostream >
#include < vector >
using namespace std;
template < class T1 >
class SlotBase
{
public :
virtual void Exec(T1 param1) = 0 ;
};
template < class T, class T1 >
class SlotImpl : public SlotBase < T1 >
{
public :
SlotImpl(T * pObj, void (T:: * func)(T1) )
{
m_pObj = pObj;
m_Func = func;
}
void Exec( T1 param1)
{
(m_pObj ->* m_Func)(param1);
}
private :
T * m_pObj;
void (T:: * m_Func)(T1);
};
template < class T1 >
class Slot
{
public :
template < class T >
Slot(T * pObj, void (T:: * func)(T1))
{
m_pSlotBase = new SlotImpl < T,T1 > (pObj, func);
}
~ Slot()
{
delete m_pSlotBase;
}
void Exec(T1 param1)
{
m_pSlotBase -> Exec(param1);
}
private :
SlotBase < T1 >* m_pSlotBase;
};
template < class T1 >
class Signal
{
public :
template < class T >
void Bind(T * pObj, void (T:: * func)(T1))
{
m_pSlotSet.push_back( new Slot < T1 > (pObj,func) );
}
~ Signal()
{
for ( int i = 0 ;i < ( int )m_pSlotSet.size();i ++ )
{
delete m_pSlotSet[i];
}
}
void operator ()(T1 param1)
{
for ( int i = 0 ;i < ( int )m_pSlotSet.size();i ++ )
{
m_pSlotSet[i] -> Exec(param1);
}
}
private :
vector < Slot < T1 >* > m_pSlotSet;
};
#define Connect( sender, signal, receiver, method) ( (sender)->signal.Bind(receiver, method) )
class A
{
public :
void FuncOfA( int param)
{
printf( " A::FuncOfA(%d)\n " , param);
}
};
class B
{
public :
void FuncOfB( int param)
{
printf( " B::FuncOfB(%d)\n " , param);
}
};
class C
{
public :
C()
{
m_Value = 0 ;
}
void SetValue( int value)
{
if (m_Value != value)
{
m_Value = value;
ValueChanged(m_Value);
}
}
public :
Signal < int > ValueChanged;
private :
int m_Value;
};
int main()
{
A * pA = new A;
B * pB = new B;
C * pC = new C;
Connect(pC, ValueChanged, pA, & A::FuncOfA);
Connect(pC, ValueChanged, pB, & B::FuncOfB);
pC -> SetValue( 10 );
pC -> SetValue( 5 );
pC -> SetValue( 5 );
delete pC;
delete pB;
delete pA;
scanf( " %*s " );
}