The delegate has two instances: The delegate receiver and the delegate sender. The delegate receiver needs to have a function implementation, and then "please" someone else calls it. The delegate sender must have a "waiter" to accept the "delegate" request from the delegate receiver, and record the call entry. function pointers are used in non-object-oriented scenarios.
In object-oriented mode, you need to save this before calling it. Therefore, different languages use different implementation methods.
BCB, use the keyword _ Closure
It can be basically solved.
First declare what kind of application the "waiter" can accept:
Typedef
Bool _ fastcall (_ Closure
* Fsynchronizationcaller (synchronizationmanager)
* Synchronization_manager );
Delegate sender. exitcallers is used to save the call entry.
Class
Synchronizationchecker
{
Public:
Fsynchronizationcaller
Exitcallers;
};
The normal value assignment statement can be used to save the entry and implement the "please" operation.
Exitcallers = firstbranch; // The firstbranch function corresponds to the "waiter" <
/Span>
When calling, exitcallers is used like normal functions, but this is already delegated to the receiver.
Delphi is simpler, and _ closure keywords are not used.
First declare what kind of application the "waiter" can accept:
Type
Fsynchronizationcaller = Function
(Synchronization_manager: tobject): Boolean of object;
The subsequent practices are similar to those of BCB.
Here we will focus on VC's <
BR> vc6, vc7: Refer to fast
Delegate. But it is faster and easier to understand than it.
First, declare the delegate generic class, that is, all the generalization of this.
Class
Genclass {};
// Set it to a format similar to BCB
# DEFINE _ Closure
Genclass ::
Define Template
Template <class T, typename
Functiontype>
Class
Closure
{
Public:
Functiontype func;
T
* This;
};
Template <typename
Functiontype, typename rettype = void *>
Class
Delegate
{
Typedef
Closure <genclass, functiontype>
Closuretype;
Closuretype
* Closureclass;
Public:
Delegate () {closureclass = 0 ;}
~ Delegate () {Delete
Closureclass ;}
Public:
// -------------- For no Param
Function
Template <class
X>
Inline void BIND (x * pthis, void (X: * function_to_bind) () // for void
Return Function
{
Typedef void
(X: * localfunctiontype )();
Typedef closure <X, localfunctiontype>
X_closuretype;
X_closuretype * c = new
X_closuretype;
C-> This = pthis;
C-> func = function_to_bind;
Closureclass = (closuretype *) C;
}
Template <class
X>
Inline void BIND (x * pthis, rettype (X: * function_to_bind) () //
Other (not void) return Function
{
Typedef rettype
(X: * localfunctiontype )();
Typedef closure <X, localfunctiontype>
X_closuretype;
X_closuretype * c = new
X_closuretype;
C-> This = pthis;
C-> func = function_to_bind;
Closureclass = (closuretype
*) C;
}
Inline rettype
Operator ()()
{
Typedef rettype (genclass: * localfunctiontype) (); // convert to void
Void *, if it is
Appointed
Return
(Closureclass-> This-> * (localfunctiontype) closureclass-> func )();
}
// -------------- For 1 Param
Function
Template <class
X, typename
Param1>
Inline void BIND (x * pthis, void (X: * function_to_bind) (param1) //
Void return Function
{
Typedef void
(X: * localfunctiontype) (param1 );
Typedef closure <X, localfunctiontype>
X_closuretype;
X_closuretype * c = new
X_closuretype;
C-> This = pthis;
C-> func = function_to_bind;
Closureclass = (closuretype *) C;
}
Template <class
X, typename
Param1>
Inline void BIND (x * pthis, rettype (X ::*
Function_to_bind) (param1) // for other (not void) Return
Function
{
Typedef rettype
(X: * localfunctiontype) (param1 );
Typedef closure <X, localfunctiontype>
X_closuretype;
X_closuretype * c = new
X_closuretype;
C-> This = pthis;
C-> func = function_to_bind;
Closureclass = (closuretype *) C;
}
Template <typename
Param1>
Inline rettype operator () (param1
P1)
{
Typedef rettype (genclass: * localfunctiontype) (param1); // convert
Void to void *, if it is
Appointed
Return
(Closureclass-> This-> * (localfunctiontype) closureclass-> func) (P1 );
}
};
If there are more parameters, You need to extend the response template member functions.
When using
Xiansheng
What kind of applications can the "waiter" accept:
Typedef
Bool (_ closure * myfunc) (int );
Typedef
DeleGate <myfunc, bool>
Mydelegete_t;
# Include
<Stdio. h>
Struct
A
{
Bool F (int A) {printf ("% d", a); Return
True;} // corresponds to myfunc
};
Lresult callback
About (hwnd hdlg, uint message, wparam, lparam
Lparam)
{
Mydelegete_t
D;
A;
D. BIND (& A, A: F );
D (100 );
............
}
The rest is similar to BCB and will not be repeated.
Understanding
For how to use C ++ to implement delegation, refer to fastdelegate, and http:
// Www.x5dj.com/userforum/00100079/4259471.shtml
The above source code is from duceland
Designer
Open the example and generate the code, the template here download the http://duceland.com/Download/VC6.zip
<
BR>
C #:
What kind of application can the "waiter" accept is automatically completed, and only the place where the function entry is saved is needed. This is progress, though
It is not original.
Public Delegate bool
Dsynchronizationcaller (synchronizationmanager
Synchronization_manager );
The rest is similar and will not be repeated.
Java is not very proficient.
"Waiters" are uniform, default
It is troublesome to save the call entry.
Public class
Dsynchronizationcaller {
Public Method
M_method;
Public object
M_object;
Public
Boolean BIND (class CLs, object, string
Method_name ){
M_object = object;
// By obtaining a list of all declared
Methods.
Method [] Methods = Cls. getdeclaredmethods ();
For (INT I = 0; I <methods. length; I ++ ){
If (methods [I]. getname (). Equals (method_name )){
M_method = methods [I];
Return true;
}
}
Return false;
}
// It is not natural to call the API either.
, Parameters are transferred to the object [] Array
Public object
Invoke (synchronizationmanager
Synchronization_manager ){
Try {
Object result = m_method.invoke (m_object,
New object []
{Synchronization_manager });
Return result;
} Catch (exception E)
{System. Err. println (E );}
Return NULL;
}
};
The event is bound to bind and then invoke for execution.