Call back routine

前端 未结 6 570
野的像风
野的像风 2021-02-01 10:14

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

What d

6条回答
  •  礼貌的吻别
    2021-02-01 11:08

    What is a Callback function?
    In simple terms, a Callback function is a function 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 an operation(function) takes a 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.

    A practical example:
    Windows event processing:
    virtually all windows programs set up an event loop, that makes the program respond to particular events (e.g 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.

    A source Code Illustration:

    //warning:  Mind compiled code, intended to illustrate the mechanism    
    #include 
    
    typedef void (*Callback)();
    std::map  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::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.

提交回复
热议问题