Functions of the callback function

Source: Internet
Author: User

Functions of the callback function

1. What is callback?

There are always some interfaces between software modules. In terms of calling methods, they can be divided into three types: Synchronous call, callback and asynchronous call. Synchronous call is a blocking call. The caller must wait for the execution of the other party to complete before returning. It is a one-way call. Callback is a two-way call mode, that is, the called party also calls the other party's interface when the interface is called. asynchronous call is a mechanism similar to messages or events, but its call direction is the opposite, when an interface service receives a message or an event, it proactively notifies the customer (that is, the customer's interface ). Callback and asynchronous call are closely related. Generally, callback is used to register asynchronous messages and message notifications are implemented through asynchronous calls. Synchronous calling is the simplest of the three, and callback is often the basis of asynchronous calling. Therefore, we will focus on the implementation of callback mechanisms in different software architectures.


For different types of languages (such as structured and object languages), platforms (Win32, JDK), or architectures (such as CORBA, DCOM, and WebService), the interaction between customers and services is not synchronized, A certain asynchronous notification mechanism is required for the service provider (or interface provider) to actively notify the customer in some cases, and callback is the simplest way to implement Asynchronization.

For general structured languages, callback can be implemented through callback functions. A callback function is also a function or process, but it is a special function implemented by the caller for the caller.

In an object-oriented language, callback is implemented through an interface or abstract class. We turn the class implementing this interface into a callback class, and the object of the callback class into a callback object. For Object languages such as C ++ or Object Pascal that are compatible with the process features, they not only provide callback objects, callback methods, and other features, but also support the callback function mechanism of the Process language.

The message mechanism on the Windows platform can also be seen as an application of callback. We use the interface provided by the system to register the message processing function (that is, the callback function) to receive and process messages. Because Windows APIs are built in C language, we can think of them as a special case of callback functions.

For the Distributed Component proxy system (CORBA), asynchronous processing involves multiple methods, such as Callback, event service, and notification service. Event Service and notification service are the standard services used by CORBA to process asynchronous messages. They are mainly responsible for message processing, distribution, and maintenance. Some simple asynchronous processing processes can be implemented through the callback mechanism.

The following describes representative languages (C, Object Pascal) and Architecture (CORBA) to analyze the implementation method and specific functions of callback.

2. Callback in process language (C)

2.1 function pointer

Callback is implemented by using the function pointer in the C language. The callback is implemented by passing the address of the callback function to the called function. Therefore, to implement callback, you must first define the function pointer. See the following example:

Void Func (char * s); // function prototype void (* pFunc) (char *); // function pointer

We can see that the definition of a function is very similar to that of a function pointer.

In general, to simplify the definition of variables of the function pointer type and improve the readability of the program, we need to customize the function pointer type.

typedef void(*pcb)(char *);

A callback function can be called by a program like a normal function, but it can be called a callback function only when it is passed as a parameter to the called function.

Example of the called function:

Void GetCallBack (pcb callback) {/* do something */} when calling the above function, you need to implement a pcb-type callback function: void fCallback (char * s) {/* do something */} then, you can directly pass fCallback as a variable to GetCallBack, GetCallBack (fCallback );

If different values are assigned to this parameter, the caller will call functions of different addresses. Assign values can occur at runtime, so that you can achieve dynamic binding.




Back to Top


2.2 parameter transfer rules

So far, we have only discussed function pointers and callbacks, but have not paid attention to the ansi c/C ++ compiler specifications. Many compilers have several call specifications. For example, in Visual C ++, you can add _ cdecl, _ stdcall, or _ pascal before the function type to indicate its call specifications (default value: _ cdecl ). C ++ Builder also supports the _ fastcall call specification. The call specification affects the given function name generated by the compiler, the sequence of parameter passing (from right to left or from left to right), and the responsibility for Stack cleaning (caller or called) and parameter transfer mechanism (stack, CPU register, etc ).

It is important to regard the call specification as part of the function type. Incompatible call specifications cannot be used to assign addresses to function pointers. For example:

// The called function uses int as the parameter and int as the return value _ stdcall int callee (int ); // call the function void caller (_ cdecl int (* ptr) (int) with the function pointer as the parameter )); // illegal operation of the called function address in p's enterprise graph storage _ cdecl int (* p) (int) = callee; // Error

The pointer p and callee () types are incompatible because they have different call specifications. Therefore, the caller's address cannot be assigned to the pointer p, although the two have the same return value and parameter column.

2.3 Application Example

In many parts of the standard library functions in C language, callback functions are used to allow users to customize the processing process. Such as common quick sorting functions and binary search functions.

Quick Sort function prototype:

Void qsort (void * base, size_t nelem, size_t width, int (_ USERENTRY * fcmp) (const void *, const void *); binary search function prototype: void * bsearch (const void * key, const void * base, size_t nelem, size_t width, int (_ USERENTRY * fcmp) (const void *, const void *));

Fcmp is a callback function variable.

The following is a specific example:

#include <stdio.h>#include <stdlib.h>int sort_function( const void *a, const void *b);int list[5] = { 54, 21, 11, 67, 22 };int main(void){   int  x;   qsort((void *)list, 5, sizeof(list[0]), sort_function);   for (x = 0; x < 5; x++)      printf("%i\n", list[x]);   return 0;}int sort_function( const void *a, const void *b){   return *(int*)a-*(int*)b;}

2.4 callback in object-oriented language (Delphi)

Like C ++, Dephi retains its previous structural features while introducing the object-oriented mechanism to maintain compatibility with Pascal. Therefore, there are two different callback modes: a structured callback mode and an object-oriented interface mode.

2.4.1 callback function

Callback Function Type Definition:

type   TCalcFunc=function (a:integer;b:integer):integer;

Customize the function implementation according to the callback function format, as shown in figure

function Add(a:integer;b:integer):integerbegin  result:=a+b;end;function Sub(a:integer;b:integer):integerbegin  result:=a-b;end;

Use of callback

function Calc(calc:TcalcFunc;a:integer;b:integer):integer

Next, we can call these two functions in our program as needed.

c:=calc(add,a,b);//c=a+bc:=calc(sub,a,b);//c=a-b

2.4.2 callback object

What is a callback object? In what scenarios is it used? First, let's compare it with the callback function. The callback function is a prototype that defines the function, and the function body is implemented by a third party in a dynamic application mode. To implement a callback function, we must know exactly the following points: the parameters required by the function and the type of values returned. Similarly, a callback object is an abstract class (that is, an interface) that defines the object interface but does not implement it ). To implement a callback object, we must know which methods it needs to implement, which parameters are included in each method, and what values the method needs to return.

Therefore, interfaces are used in the callback object application mode. An interface can be understood as a defined but not implemented class. It can only be implemented by other classes by means of inheritance. The interfaces in Delphi are similar to those in COM. All interfaces inherit from IInterface (equivalent to IUnknow) and implement three basic methods: QueryInterface, _ AddRef, and _ Release.

  • Define an Interface
    type IShape=interface(IInterface)procedure Draw;end

  • Implementation callback class
    type TRect=class(TObject,IShape)protected      function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;      function _AddRef: Integer; stdcall;function _Release: Integer; stdcall;    public  procedure Draw;end;type TRound=class(TObject,IShape)protected      function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;      function _AddRef: Integer; stdcall;function _Release: Integer; stdcall;    public  procedure Draw;end;

  • Use callback object
    procedure MyDraw(shape:IShape);var shape:IShape;beginshape.Draw; end;

If the input object is TRect, draw a rectangle; if it is TRound, It is a circle. Users can also implement the IShape interface according to their own intentions to draw their own graphics:

MyDraw(Trect.Create);MyDraw(Tround.Create);

2.4.3 callback Method

The Callback Method can be considered as a part of the Callback object. Delphi uses the Callback Method to encapsulate windows messages. In some cases, we do not need to implement the entire object according to the given requirements, but we only need to implement one of the methods. This is what we will use the callback method.

The callback method is defined as follows:

TNotifyEvent = procedure(Sender: TObject) of object; TMyEvent=procedure(Sender:Tobject;EventId:Integer) of object;

Tpolicyevent is the most common callback method in Delphi. Many events of forms and controls, such as click events and close events, use tpolicyevent. Variables of the callback method are generally defined by event attributes, such as the definition of the event created by TCustomForm:

property OnCreate: TNotifyEvent read FOnCreate write FOnCreate stored IsForm;

We can customize the event processor by assigning values to the event attribute variable.

User-defined objects (objects containing callback methods ):

type TCallback=Class    procedure ClickFunc(sender:TObject);end;procedure Tcallback.ClickFunc(sender:TObject);begin  showmessage('the caller is clicked!');end;

Form object:

type TCustomFrm=class(TForm)  publicprocedure RegisterClickFunc(cb:procedure(sender:Tobject) of object);end;procedure TcustomFrm..RegisterClickFunc(cb:TNotifyEvent);begin  self.OnClick=cb;end;

Usage:

var  frm:TcustomFrm;begin  frm:=TcustomFrm.Create(Application);  frm.RegisterClickFunc(Tcallback.Create().ClickFunc);end;

3. Application of callback in distributed computing (CORBA)

3.1 callback interface model

There are many kinds of message transmission mechanisms in CORBA, such as Callback interfaces, event services, and Notification Services. The principle of the callback interface is very simple. both the client and the server have a dual role, that is, acting as a server is also a customer.

The reverse call and forward call of the callback interface are usually performed at the same time. If the server calls the callback interface multiple times, the callback interface becomes an asynchronous interface. Therefore, the callback interface is often used for event registration in CORBA. When the client calls the registration function, the customer function is the callback function. In subsequent calls, this function implements an asynchronous mechanism because it does not require the active participation of the client.

We know from the CORBA specification that a CORBA interface has different forms of representation on the server and the client. The client generally uses the pile (Stub) file, and the server uses the framework (Skeleton) file, the interface specification is defined by IDL. With the introduction of callback functions, both the server and the client need to implement certain piles and frameworks. The following is the implementation model of the callback interface:


3.1.1 example

The following provides a CallBack interface file. The Server needs to implement the Server interface framework, and the client needs to implement the CallBack framework:

module cb{interface CallBack;interface Server;interface CallBack {    void OnEvent(in long Source,in long msg);};  interface Server {    long RegisterCB(in CallBack cb);void UnRegisterCB(in long hCb);};};

The client first calls the server interface RegistCB in synchronous mode to register the CallBack interface CallBack. After receiving the request, the server retains the interface reference. If an event needs to be notified to the client, the server uses this reference to call the OnEvent function of the client, so that the other party can process the request in time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.