VC,BCB,C#,Delphi,Java的委托方案 vcbcbcbcb

   2023-02-09 学习力0
核心提示:委托(delegate)有委托接收方和委托发出方两个实例。委托接收方需要有一个函数实现,然后“拜托”别人调用。委托发出方需要有一个“服务员”接受委托接收方的“委托”请求,并记录调用入口,在非面向对象的时候用的是函数指针,现在面向对象,需要保存this后调
委托(delegate)有委托接收方和委托发出方两个实例。委托接收方需要有一个函数实现,然后“拜托”别人调用。委托发出方需要有一个“服务员”接受委托接收方的“委托”请求,并记录调用入口,在非面向对象的时候用的是函数指针,现 在面向对象,需要保存this后调用才行,所以不同的语言用了不同的实现方法。

BCB, 使用关键字__closure 基本上可以解决。
先声明“服务员”能接受什么样的申请:
typedef BOOL __fastcall(__closure *FSynchronizationCaller)(SynchronizationManager *synchronization_manager);

委托发出方,这里用ExitCallers保存调用入口
class SynchronizationChecker
{
public:
FSynchronizationCaller ExitCallers;
};
普通赋值语句就可以保存入口,实现“拜托”的工作
ExitCallers=FirstBranch;//FirstBranch的函数与“服务员”对应< /span>
调用的时候ExitCallers象普通函数一样使用,但是this已经是委托接收方的了。

Delphi更简单, __closure关键字都不用
先声明“服务员”能接受什么样的申请:
type FSynchronizationCaller = function (synchronization_manager:TObject):Boolean of object;
后面的做法与BCB类似

这里着重讲VC的< br>VC6,VC7:参照fast delegate。但是比它还要快和易懂
首先声明委托通用类,即所有this的通用化
class GenClass{};
//设置成与BCB类似的形式
#define __Closure GenClass::
定义模板
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)())//for 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 to 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))//for 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 to void to void*,if it is appointed
        return (ClosureClass->This->*(LocalFunctionType)ClosureClass->Func)(p1);
    }
};
如果有更多的参数需要扩展响应的模板成员函数。
使用的时候
先声 明“服务员”能接受什么样的申请
typedef BOOL (__Closure *myfunc)(int a);
typedef Delegate<myfunc,BOOL> MyDelegete_t;

#include <stdio.h>
struct A
{
  BOOL f(int a){printf("%d",a);return TRUE;}//与 myfunc对应
};

LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  MyDelegete_t d;
  A a;
  d.bind(&a,A::f);
  d(100);
  ............
}

其余部分与BCB类似,不再赘述。
要理解 C++如何用实现委托的,可以参照fastdelegate,和http: //www.x5dj.com/UserForum/00100079/00009471.shtml
上述源代码从Duceland Designer 打开例子后生成代码所得,模板这里下载http://duceland.com/Download/VC6.zip
< br>
C#:
“服务员”能接受什么样的申请是自动完成的,只有保存要函数入口的地方就可以了,这就是进步啊,虽 然不是原创。
public delegate bool DSynchronizationCaller(SynchronizationManager synchronization_manager);
其余部分类似,不再赘述。

Java不是很精通,只好这样了
“服务员”是统一的,默认的
保存调用入口比较麻烦
public class DSynchronizationCaller{
    public Method m_Method;
    public Object m_Object;
    public boolean bind(Class cls,Object 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;
    }

//调用的时候也不是很自然,用invoke ,参数被转入Object[]数组
    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;
    }
};

事件通过bind绑定后用invoke执行。
 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • Delphi中的消息处理机制 delphi 方法
    每一个VCL都有一内在的消息处理机制,其基本点就是构件类接收到某些消息并把它们发送给适当的处理方法,如果没有特定的处理方法,则调用缺省的消息处理句柄。    其中mainwndproc是定义在Twincontrol类中的一个静态方法,不能被重载(Override)。它不直接处
    02-09
  • Delphi XE6 通过JavaScript API调用百度地图
    Delphi XE6 通过JavaScript API调用百度地图
    参考昨天的内容,有朋友还是问如何调用百度地图,也是,谁让咱都在国内呢,没办法,你懂的。 首先去申请个Key,然后看一下百度JavaScript的第一个例子:http://developer.baidu.com/map/jsdemo.htm下一步,就是把例子中的代码,移动TWebBrower中。 unit Unit
    02-09
  • Delphi编译/链接过程 delphi编程案例
    Delphi编译/链接过程 delphi编程案例
    下面展示了Delphi是怎样编译源文件,并且把它们链接起来,最终形成可执行文件。当Delphi编译项目(Project)时,将编译项目源文件、窗体单元和其他相关单元,在这个过程中将会发生好几件事情:首先,Object Pascal编译器把项目单元编译为二进制对象文件,然后
    02-09
  • Delphi CompilerVersion Constant / Compiler C
    http://delphi.wikia.com/wiki/CompilerVersion_Constant The CompilerVersion constant identifies the internal version number of the Delphi compiler.It is defined in the System unit and may be referenced either in code just as any other consta
    02-09
  • Delphi revelations #1 – kbmMW Smart client
    Delphi 启示 #1 – kbmMW Smart client on NextGen (Android) – 作用域问题以更高级的方式使用kbmMW smart client,在Android设备上,我遇到了问题。通过继承TInvokeableVariantType,kbmMW smart client可以使用Delphi支持的特殊类型的自定义Variant,从而可
    02-09
  • Delphi 调用DLL外部函数时的指针参数
    某项目需要调用设备厂家提供的DLL的函数,厂家给了一个VB的例子,有个参数是ByRef pBuffer As Single。于是在Delphi中用buffer:array of single代替:function func(buffer:array of single;count:integer):integer;far;stdcall;external 'func.dll';调用后bu
    02-09
  • 《zw版·Halcon-delphi系列原创教程》 Halcon分
    《zw版·Halcon-delphi系列原创教程》 Halcon分类函数012,polygon,多边形为方便阅读,在不影响说明的前提下,笔者对函数进行了简化::: 用符号“**”,替换:“procedure”:: 用大写字母“X”,替换:“IHUntypedObjectX”:: 省略了字符:“const”、“OleVa
    02-09
  • 最简单的delphi启动画面(转)
    首先做一窗体,然后将BorderStyle的属性设为bsnone,放image控件,align设为alclient 然后将主程序的修改为 uses Windows, Forms, Unit1 in 'Unit1.pas' {Form1}, Unit2 in 'Unit2.pas' {Form2}; {$ R *.res} begin Application.Initialize; Form2:=TForm2.Cre
    02-09
  • Delphi备忘三:TCollection的使用,用Stream保
     代码unit ufrmGetFunctionDefine;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,TypInfo,  Dialogs,ufrmStockBaseCalc, StdCtrls, ComCtrls,uQEFuncManager,uWnDataSet,uDataService;type  T
    02-09
  • Delphi Dcp 和BPL的解释
    dcp = delphi compiled package,是 package 编译时跟 bpl 一起产生出来的,记录着 package 中公开的 class、procedure、function、variable、const.... 等等的名称和相对位址。package英文翻译过来就是“包”。如果 某个控件包 A 引用了 控件包 B,当 控件包
    02-09
点击排行