回调程序 [英] Call back routine

查看:165
本文介绍了回调程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习OpenCV 书中,我来到回调,有时使用



当我们说 code> callback



谢谢。

解决方案

什么是Callbak函数?

简单来说,回调函数不是由程序员显式调用。相反,有一些机制继续等待事件发生,并且将调用选择的函数以响应特定事件。

此机制通常用于操作(函数)可能需要很长时间才能执行并且函数的调用者不希望等待操作完成,而是希望对操作的结果进行暗示。通常,回调函数帮助实现这样的异步机制 ,其中调用者注册以获得关于耗时处理和连续其他操作的结果的动画,而在稍后的点

一个实际的例子:

Windows事件处理:

几乎所有窗口程序都设置了一个事件循环,使程序通过调用函数来响应特定事件(例如按钮按下,选择复选框,窗口获得焦点)。方便的是,程序员可以指定当(例如)按下特定按钮时调用什么函数,即使不能指定按钮何时被按下。调用的函数称为回调。



源代码插图:

  //警告:注意编译代码,用于说明机制
#include< map&

typedef void(* Callback)();
std :: map< int,Callback> callback_map;

void RegisterCallback(int event,Callback function)
{
callback_map [event] = function;
}

bool finished = false;

int GetNextEvent()
{
static int i = 0;
++ i;
if(i == 5)completed = false;
}

void EventProcessor()
{
int event;
while(!finished)
{
event = GetNextEvent();
std :: map< int,Callback> :: const_iterator it = callback_map.find(event);
if(it!= callback_map.end())//如果为事件注册了回调
{
回调函数= * it;
if(function)
{
(* function)();
}
else
{
std :: cout< 没有回调found\\\
;
}
}
}
}

void Cat()
{
std :: cout< Cat\\\
;
}

void Dog()
{
std :: cout< Dog\\\
;
}

void Bird()
{
std :: cout< Bird \\\
;
}

int main()
{
RegisterCallBack(1,Cat);
RegisterCallback(2,Dog);
RegisterCallback(3,Cat);
RegisterCallback(4,Bird);
RegisterCallback(5,Cat);

EventProcessor();
return 0;
}

上述输出结果如下:

  Cat 
Dog
Cat
Bird
Cat

希望这有助于!






注意:

从我的旧答案中导入此答案 此处


In the Learning OpenCV book, I came to the term callback, and sometimes used with routine as callback routine.

What do we mean when we saycallback?

Thanks.

解决方案

What is a Callbak function?
In simple terms, a Callback function is one that is not called explicitly by the programmer. Instead, there is some mechanism that continually waits for events to occur, and it will call selected functions in response to particular events.
This mechanism is typically used when a operation(function) can take long time for execution and the caller of the function does not want to wait till the operation is complete, but does wish to be intimated of the outcome of the operation. Typically, Callback functions help implement such an asynchronous mechanism, wherein the caller registers to get inimated about the result of the time consuming processing and continuous other operations while at a later point of time, the caller gets informed of the result.

An practical example:
Windows event processing:
virtually all windows programs set up an event loop, that makes the program respond to particular events (eg button presses, selecting a check box, window getting focus) by calling a function. The handy thing is that the programmer can specify what function gets called when (say) a particular button is pressed, even though it is not possible to specify when the button will be pressed. The function that is called is referred to as a callback.

An source Code Illustration:

//warning:  Mind compiled code, intended to illustrate the mechanism    
#include <map>

typedef void (*Callback)();
std::map<int, Callback>  callback_map;

void RegisterCallback(int event, Callback function)
{
    callback_map[event] = function;
}

bool finished = false;

int GetNextEvent()
{
    static int i = 0;
    ++i;
    if (i == 5) finished = false;
}

void EventProcessor()
{
    int event;
    while (!finished)
    {
        event = GetNextEvent();
        std::map<int, Callback>::const_iterator it = callback_map.find(event);
        if (it != callback_map.end())    // if a callback is registered for event
        {
            Callback function = *it;
            if (function)   
            {
                (*function)();
            }
            else
            {
               std::cout << "No callback found\n";
            }
        }
    }
}

void Cat()
{
   std::cout << "Cat\n";
}

void Dog()
{
    std::cout << "Dog\n";
}

void Bird()
{
    std::cout << "Bird\n";
}

int main()
{
    RegisterCallBack(1, Cat);
    RegisterCallback(2, Dog);
    RegisterCallback(3, Cat);
    RegisterCallback(4, Bird);
    RegisterCallback(5, Cat);

    EventProcessor(); 
    return 0;
}

The above would output the following:

Cat  
Dog   
Cat  
Bird  
Cat  

Hope this helps!


Note:
Imported this answer from one of my old answers here.

这篇关于回调程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆