,
ecco shoes
| Back to logs list
2556587 2009 年 10 月 20 日 16:10 Reading (loading. ..) Comments (0) Category: Program Development
/*********************************************** ***/
/ / testMain.cpp
/********************************************** ****/
# include
# include \
# include \
using namespace xt;
//--------------------------------------------- ---------------------------------
/ / XTDelegate test
//---- -------------------------------------------------- ------------------------
//--------------------------------------------- ---------------------------------
/ / generic function
//---- -------------------------------------------------- ------------------------
//--------------------------------------------- ---------------------------------
/ / no parameters
//----- -------------------------------------------------- -----------------------
void Func1 ()
{
std:: cout d1 = Func1;
d1 ();
XTDelegate d2 = Func2;
d2 (\
XTDelegate d3 = Func3;
std:: cout <<d3 (4, 5) <<std:: endl;
XTDelegate d4 (& TestClass:: StaticFunc1);
d4 (\
XTDelegate d5 = d4;
d5 (\
TestClass tc1;
XTDelegate d6 (& tc1,
ecco shoes clearance, & TestClass:: MemberFunc1);
TestStruct ts = d6 (12, 18.0);
std:: cout <<ts.m1 <<\
XTDelegate d7 = std:: make_pair (& tc1, & TestClass:: MemberFunc1);
ts = d7 (12, 18.0);
std:: cout <<ts.m1 <<\: endl;
XTDelegate d8;
d8 = std:: make_pair (& tc1, & TestClass:: MemberFunc2);
if (d8 == std:: make_pair (& tc1,
ecco mens shoes, & TestClass:: MemberFunc2))
{
std:: cout <<d8 ( 10, 20) <<std:: endl;
}
XTDelegate d9;
d9 = Func3;
if (d9 == Func3)
{
std:: cout event1;
event1 + = Func4;
event1 + = Func4;
event1 + = & TestClass:: StaticFunc2;
event1 + = std:: make_pair (& tc1,
ecco shoes online, & TestClass:: MemberFunc3);
event1 -= Func4;
event1 (\
/ /
/ / / / win form
/ /
Form form1;
form1.Initialize ();
form1.SendMessage (1);
form1.SendMessage (2);
getchar ();
}
/********************************************** * /
/ form.cpp
/********************************************** * /
# include \
namespace xt
{
//------------------------------------------- -----------------------------------
/ / Button
/ / -------------------------------------------------- ----------------------------
void Button:: Click ()
{
; OnClick ();
}
//------------------------------------------- -----------------------------------
/ / Form
/ / -------------------------------------------------- ----------------------------
void Form:: Initialize ()
{
; mButton1.OnClick + = std:: make_pair (this, & Form:: Button1Clicked);
mButton2.OnClick + = std:: make_pair (this, & Form:: Button2Clicked);
}
void Form:: Button1Clicked ()
{
std:: cout }
void Form:: Button2Clicked ()
{
std:: cout }
void Form:: SendMessage (int msg)
{
if (msg == 1)
{
; mButton1.Click ();
}
else if (msg == 2)
{
mButton2.Click ();
}
}
}
/********************************************** ********/
/ form.h
/********************************************** ********/
# ifndef _Form_H_
# define _Form_H_
# include \
namespace xt
{
//---------------------------------- --------------------------------------------
/ / Button
//----------------------------------------- -------------------------------------
class Button
{
public:
void Click ();
XTEvent OnClick;
};
//------------------------------------------- -----------------------------------
/ / Form
/ / -------------------------------------------------- ----------------------------
class Form
{
public:
void Initialize ();
void Button1Clicked ();
void Button2Clicked ();
void SendMessage (int msg);
protected:
Button mButton1;
Button mButton2;
};
}
# endif
/********************************************** ****/
/ / XTDelegate.h
/********************************************** ****/
# ifndef _XTDelegate_H_
# define _XTDelegate_H_
//--------------------------------------------- ---------------------------
/ / author: Dengtao
/ / time: 2007-4-29
//----------------------------------------------- -------------------------
# include
# include
//--------------------------------------------- ---------------------------
/ / Summary:
/ / Showing the usages of XTDelegate and XTEvent by simple example,
/ / and the example code in XTDelegateGuide.txt
//------------------------ ------------------------------------------------
namespace xt
{
//---------------------------------- --------------------------------------
/ / Summary:
/ / XTDelegate is a template class.
/ / It simulates c #-style delegate.
//----------- -------------------------------------------------- -----------
template class XTDelegate;
//------------------------------------------- -----------------------------
/ / Summary:
/ / XTEvent is a template class.
/ / Is acts as a wrpper of functions. (includes normal functions, static
/ / functions, and member functions.)
/ / Remarks:
/ / That is a empty class, and its presentation in the header file named 'XTEventTemplet.h'
//------ -------------------------------------------------- ----------------
template class XTEvent;
} / / end namespace xt
//--------------------------------------------- ---------------------------
/ / Helper macros
//----------- -------------------------------------------------- -----------
//--------------------------------------------- ---------------------------
/ / Macros for making parameters.
/ / Example:
/ / ; XT_MAKE_PARAMETERS1 (T, t) - T1 t1
/ / XT_MAKE_PARAMETERS2 (T, t) - T1 t1, T2 t2
//------------ -------------------------------------------------- ----------
# define XT_MAKE_PARAMETERS0 (t1, t2)
# define XT_MAKE_PARAMETERS1 (t1, t2) t1 # # 1 t2 # # 1
# define XT_MAKE_PARAMETERS2 (t1, t2) XT_MAKE_PARAMETERS1 (t1, t2), t1 # # 2 t2 # # 2
# define XT_MAKE_PARAMETERS3 (t1, t2) XT_MAKE_PARAMETERS2 (t1, t2), t1 # # 3 t2 # # 3
# define XT_MAKE_PARAMETERS4 (t1, t2) XT_MAKE_PARAMETERS3 (t1, t2), t1 # # 4 t2 # # 4
# define XT_MAKE_PARAMETERS5 (t1, t2) XT_MAKE_PARAMETERS4 (t1, t2), t1 # # 5 t2 # # 5
# define XT_MAKE_PARAMETERS6 (t1, t2) XT_MAKE_PARAMETERS5 (t1, t2), t1 # # 6 t2 # # 6
# define XT_MAKE_PARAMETERS7 (t1, t2) XT_MAKE_PARAMETERS6 (t1, t2), t1 # # 7 t2 # # 7
# define XT_MAKE_PARAMETERS8 (t1, t2) XT_MAKE_PARAMETERS7 (t1, t2), t1 # # 8 t2 # # 8
# define XT_MAKE_PARAMETERS9 (t1, t2) XT_MAKE_PARAMETERS8 (t1, t2), t1 # # 9 t2 # # 9
# define XT_MAKE_PARAMETERS10 (t1, t2) XT_MAKE_PARAMETERS9 (t1 , t2), t1 # # 10 t2 # # 10
# define XT_MAKE_PARAMETERS11 (t1, t2) XT_MAKE_PARAMETERS10 (t1, t2), t1 # # 11 t2 # # 11
# define XT_MAKE_PARAMETERS12 (t1, t2 ) XT_MAKE_PARAMETERS11 (t1, t2), t1 # # 12 t2 # # 12
//--------------------------------------------- ---------------------------
/ / Macros for making arguments.
/ / Example:
/ / ; XT_MAKE_ARGUMENTS1 (T) T1
/ / XT_MAKE_ARGUMENTS1 (class T) class T1
/ / XT_MAKE_ARGUMENTS2 (class T) class T1, class T2
//-------------------------------------- ----------------------------------
# define XT_MAKE_ARGUMENTS0 (t1)
# define XT_MAKE_ARGUMENTS1 ( t1) t1 # # 1
# define XT_MAKE_ARGUMENTS2 (t1) XT_MAKE_ARGUMENTS1 (t1), t1 # # 2
# define XT_MAKE_ARGUMENTS3 (t1) ; XT_MAKE_ARGUMENTS2 (t1), t1 # # 3
# define XT_MAKE_ARGUMENTS4 (t1) XT_MAKE_ARGUMENTS3 (t1), t1 # # 4
# define XT_MAKE_ARGUMENTS5 (t1) XT_MAKE_ARGUMENTS4 (t1), t1 # # 5
# define XT_MAKE_ARGUMENTS6 (t1) XT_MAKE_ARGUMENTS5 (t1), t1 # # 6
# define XT_MAKE_ARGUMENTS7 (t1) XT_MAKE_ARGUMENTS6 (t1 ), t1 # # 7
# define XT_MAKE_ARGUMENTS8 (t1) XT_MAKE_ARGUMENTS7 (t1), t1 # # 8
# define XT_MAKE_ARGUMENTS9 (t1) XT_MAKE_ARGUMENTS8 (t1), t1 # # 9
# define XT_MAKE_ARGUMENTS10 (t1) XT_MAKE_ARGUMENTS9 (t1), t1 # # 10
# define XT_MAKE_ARGUMENTS11 (t1) XT_MAKE_ARGUMENTS10 (t1), t1 # # 11
# define XT_MAKE_ARGUMENTS12 (t1) XT_MAKE_ARGUMENTS11 (t1), t1 # # 12
//--------------------------------------------- ---------------------------
/ / Advanced macros.
//---------- -------------------------------------------------- ------------
# define XT_JOIN (a, b) a # # b
# define XT_MAKE_PARAMETERS (NUM, t1, t2) XT_JOIN (XT_MAKE_PARAMETERS, NUM ) (t1, t2)
# define XT_MAKE_ARGUMENTS (NUM, t1) XT_JOIN (XT_MAKE_ARGUMENTS, NUM) (t1)
//--------------------------------------------- ---------------------------
/ / Creating XTDelegate
//----------- -------------------------------------------------- -----------
# define XT_DELEGATE_PARAMS_NUM 0
# include \
# define XT_DELEGATE_PARAMS_NUM 1
# include \
# define XT_DELEGATE_PARAMS_NUM 2
# include \
# define XT_DELEGATE_PARAMS_NUM 3
# include \
# define XT_DELEGATE_PARAMS_NUM 4
# include \
# define XT_DELEGATE_PARAMS_NUM 5
# include \
# define XT_DELEGATE_PARAMS_NUM 6
# include \
# define XT_DELEGATE_PARAMS_NUM 7
# include \
# define XT_DELEGATE_PARAMS_NUM 8
# include \
# define XT_DELEGATE_PARAMS_NUM 9
# include \
# define XT_DELEGATE_PARAMS_NUM 10
# include \
# define XT_DELEGATE_PARAMS_NUM 11
# include \
# define XT_DELEGATE_PARAMS_NUM 12
# include \
# endif
/********************************************** ****/
/ / delegateImplementation
/********************************************** ****/
# ifdef XT_DELEGATE_PARAMS_NUM
//--------------------------------------------- ---------------------------
/ / author: Tao
/ / time: 2007-4-28
/ / Remarks:
/ / By Creating a category of functions, gets the two class is below:
/ / Normal function: includes normal function, static function,
/ / static member function.
/ / Member functon: class member function.
//----------------- -------------------------------------------------- -----
//--------------------------------------------- ---------------------------
/ / Summary:
/ / Helper macros.
/ /------------------------------------------------- -----------------------
//--------------------------------------------- ---------------------------
/ / comma
//------------ -------------------------------------------------- ----------
# if XT_DELEGATE_PARAMS_NUM> 0
# define XT_COMMA,
# else
# define XT_COMMA
# endif
//--------------------------------------------- ---------------------------
/ / Template parameters template parameter
/ / class T1, class T2, class T3 ... class Tn
//-------------------------------------- ----------------------------------
# define XT_TEMPLATE_PARAMS XT_MAKE_ARGUMENTS (XT_DELEGATE_PARAMS_NUM, typename T)
//--------------------------------------------- ---------------------------
/ / Template arguments template argument
/ / T1, T2, T3 ... Tn
//-------------------------------------------- ----------------------------
# define XT_TEMPLATE_ARGS XT_MAKE_ARGUMENTS (XT_DELEGATE_PARAMS_NUM, T)
//--------------------------------------------- ---------------------------
/ / Function parameters function parameters
/ / T1 a1, T2 a2, T3 a3 ... Tn an
//---------------------------------------- --------------------------------
# define XT_DELEGATE_PARAMS XT_MAKE_PARAMETERS (XT_DELEGATE_PARAMS_NUM, T, a)
//--------------------------------------------- ---------------------------
/ / Function arguments template argument
/ / a1, a2, a3 ... an
//-------------------------------------------- ----------------------------
# define XT_DELEGATE_ARGS XT_MAKE_ARGUMENTS (XT_DELEGATE_PARAMS_NUM, a)
namespace xt
{
//---------------------------------- --------------------------------------
/ / Summary:
/ / Specializtion of XTDelegate.
/ / Parameters:
/ /
//----------- -------------------------------------------------- -----------
template
class XTDelegate
{
protected :
//---------------------------------------- --------------------------------
/ / Summary:
/ / XTInvoker is a helper class, it acts as a invoker of function.
/ / Remarks:
/ / Different type of function has different type of invoker.
//------------------------------------- -----------------------------------
template struct XTInvoker
{
/ / this invoker for invoking normal function.
static RT Invoke (const TFunctor & func XT_COMMA XT_DELEGATE_PARAMS)
; {
return func (XT_DELEGATE_ARGS);
}
};
//----------------------------------------- -------------------------------
/ / Summary:
/ / The specialized version for invoking member function.
//---------------------------- --------------------------------------------
; template
struct XTInvoker >
{
/ / this invoker for invoking member function.
static RT Invoke (const std:: pair & mf XT_COMMA XT_DELEGATE_PARAMS)
{
; return ((* mf.first) .* mf.second) (XT_DELEGATE_ARGS);
}
};
//----------------------------------------- -------------------------------
/ / Summary:
/ / XTDelegateImplBase is a pure virtual class. (interface class)
/ / It wraps the implementation of XTDelegate.
//----- -------------------------------------------------- -----------------
class XTDelegateImplBase
{
public:
/ / Invoke the wrapped function.
virtual RT Invoke (XT_DELEGATE_PARAMS) = 0;
/ / clone
virtual XTDelegateImplBase * Clone () = 0;
/ / equals
virtual bool Equals (XTDelegateImplBase *) = 0;
};
//----------------------------------------- -------------------------------
/ / Summary:
/ / XTDelegateImpl is a XTDelegateImplBase derived class, and it is a template class,
/ / It is used at fore-stage.
/ / - -------------------------------------------------- ---------------------
template
class XTDelegateImpl: public XTDelegateImplBase
; {
public:
/ / constructor
XTDelegateImpl (TFunctor func): m_pFunctor (func ){}
/ / Overriding Invoke ()
RT Invoke (XT_DELEGATE_PARAMS)
{
return XTInvoker :: Invoke (m_pFunctor XT_COMMA XT_DELEGATE_ARGS);
}
/ / clone
XTDelegateImpl * Clone ()
{
return new XTDelegateImpl (m_pFunctor);
}
/ / equals
bool Equals (XTDelegateImplBase * pOther)
{
return m_pFunctor == ((XTDelegateImpl *) pOther) -> m_pFunctor;
}
private:
TFunctor m_pFunctor;
};
public:
//---------------------------------- --------------------------------------
/ / Summary:
/ / Constructs a XTDelegate object.
//------------------------ ------------------------------------------------
XTDelegate (): m_pDelegateImpl (NULL) {}
//----------------------------------------- -------------------------------
/ / Summary:
/ / Copy constructor.
//--------------------------------- ---------------------------------------
XTDelegate (const XTDelegate & ; function)
{
m_pDelegateImpl = function.m_pDelegateImpl-> Clone ();
}
//----------------------------------------- -------------------------------
/ / Summary:
/ / Constructs a XTDelegate object.
/ / parameters:
/ / func - Pointer to a normal function.
/ /------------------------------------------------- -----------------------
template XTDelegate (TFunctor func)
{
m_pDelegateImpl = new XTDelegateImpl (func);
}
//----------------------------------------- -------------------------------
/ / Summary:
/ / Constructs a XTDelegate object.
/ / Parameters:
/ /
//------- -------------------------------------------------- ---------------
template XTDelegate (TPtr pObj, TFunctor func)
{
m_pDelegateImpl = new XTDelegateImpl > (std:: pair (pObj, func));
}
//----------------------------------------- -------------------------------
/ / Summary:
/ / Constructs a XTDelegate object.
/ / Parameters:
/ /
//------- -------------------------------------------------- ---------------
template XTDelegate (const std:: pair & mf)
{
m_pDelegateImpl = new XTDelegateImpl <std:: pair > (mf);
}
//-------------------------------------- ----------------------------------
/ / Summary:
/ / Destructs a XTDelegate object, manipulates cleanup and deallocation.
//----------------------- -------------------------------------------------
virtual ~ XTDelegate ()
{
if (m_pDelegateImpl)
{
; delete m_pDelegateImpl;
}
}
//----------------------------------------- -------------------------------
/ / Summary:
/ / operator =
/ / Remarks:
//----------------------- -------------------------------------------------
XTDelegate & operator = (const XTDelegate & func1)
{
if (m_pDelegateImpl)
{
delete m_pDelegateImpl;
}
m_pDelegateImpl = func1.m_pDelegateImpl-> Clone ();
return * this;
}
template
XTDelegate & operator = (TFunctor func)
{
if (m_pDelegateImpl)
{
delete m_pDelegateImpl;
}
m_pDelegateImpl = new XTDelegateImpl (func);
return * this;
}
template
XTDelegate & operator = (const std:: pair & mf)
{
if (m_pDelegateImpl)
{
delete m_pDelegateImpl;
}
m_pDelegateImpl = new XTDelegateImpl <std:: pair > (mf);
return * this;
}
//----------------------------------------- -------------------------------
/ / Summary:
/ / Call this operator to invoke the wrapped function.
//--------------------------- ---------------------------------------------
RT operator () (XT_DELEGATE_PARAMS)
{
return m_pDelegateImpl-> Invoke (XT_DELEGATE_ARGS);
}
//----------------------------------------- -------------------------------
/ / Summary:
/ / Call this member to determine if the function is valid.
/ / Remarks:
/ / Make sure the function is valid before calling it.
//----------------------------------------- -------------------------------
bool IsValid () const {return m_pDelegateImpl == 0? true: false;}
//----------------------------------------- -------------------------------------
/ / Summary:
/ / Determines tow Delegates whether equals.
//------------------------ -------------------------------------------------- ----
bool operator == (const XTDelegate & other)
{
if (m_pDelegateImpl == other.m_pDelegateImpl)
{
return true;
}
if (m_pDelegateImpl! = 0 & & other.m_pDelegateImpl! = 0)
{
return m_pDelegateImpl- > Equals (other.m_pDelegateImpl);
}
return false;
}
template
bool operator == (TFunctor func)
{
return (* this) == XTDelegate (func);
}
template
bool operator == (const std:: pair & mf)
{
return (* this) == XTDelegate (mf);
}
private:
XTDelegateImplBase * m_pDelegateImpl; / / the wrapped implementation.
}; / / end of XTDelegate
///////////////////////////////////////////// /////////////////////////////
/ / event
//////// ////////////////////////////////////////////////// ////////////////
//------------------------------------------- -----------------------------
/ / Summary:
/ / Specialization of XTDelegate.
/ / event is a Delegate, the event is no return value
//----------- -------------------------------------------------- -----------
template
class XTEvent
{
private :
//---------------------------------------- --------------------------------
/ / Summary:
/ / Type defines
//--------------------------------- ---------------------------------------
typedef XTDelegate Delegate;
typedef std:: vector DelegateList;
public:
//--- -------------------------------------------------- -------------------
/ / Summary:
/ / Constructs a XTEvent object.
//------------------------------------------- -----------------------------
XTEvent (){}
//----------------------------------------- -------------------------------
/ / Summary:
/ / Destructs a XTEvent object, manipulates cleanup and deallocation.
//--------------------------