Simple C + + callback function Design Method __ function

Source: Internet
Author: User

Simple C + + callback function implementation method:
Recently upgraded the schematic editor for the company, the GUI part is implemented with QT, in order to prevent the GUI to adopt the change of the development library, decide to adopt
Gui+engine, the GUI part is implemented with QT, engine use C + + to implement as Library,gui Runtime link engine library. QT provides a signal/slot mechanism that provides a more convenient means of communication than a callback function, and is no longer to repeat here. Here is a brief introduction to the engine and GUI layer callback function implementation method.
In the project development found that many places need to be implemented through callbacks, there are many ways to implement, such as function pointers, C + + virtual functions, boost functor imitation functions, and so on, here to provide a use of C + + template to achieve the method, the following drawing as an example, the library provides drawing lines, Draws a rectangle, drawing the interface of the string.
Let's introduce the other ways to achieve this:
1: Function Pointers:
Understanding Linux kernel is certainly not unfamiliar with the function pointers to device-driven management:

interface defined in library

struct rgb{unsigned int r; unsigned int g; unsigned int b;}; typedef struct _drawfp_{int (*drawpoint) (int x,int y,rgb RGB) int (*drawline) (int sx,int sy,int ex,int ey,rgb RGB); int (*drawrect) (int left,int top,int width,int height,bool fill,rgb RGB); Int (*drawstring) (const char *str,int x,int y,int font,rgb RGB); }drawfpointer;

This requires the implementation of four functions (note the consistency of parameters and return values) in the GUI layer:

int gui_drawpoint (int x,int y,rgb RGB) {//painter} int gui_drawline (int sx,int sy,int ex,int ey,rgb RGB) {//painter} i NT Gui_drawrect (int left,int top,int width,int height,bool fill,rgb RGB) {//painter} int gui_drawstring (const char *STR, int x,int y,int Font,rgb RGB) {//painter}/* definition drawfpoint/drawfpointer gui_drawfpoint = {gui_drawpoint, gui_drawLine , Gui_drawrect, Gui_drawstring,};

Pass this gui_drawfpoint to the library.

2: Virtual functions
implementing interfaces in the library

Class Cdrawfpoint {public:cdrawfpoint (); ~cdrawfpoint (); virtual int drawpoint (int x,int y,rgb RGB) = 0; virtual int dra Wline (int sx,int sy,int ex,int ey,rgb RGB) = 0; virtual int drawrect (int left,int top,int width,int height,bool fill,rgb RGB) = 0; virtual int drawstring (const char *str,int x,int y,int font,rgb RGB) = 0; };

Implementing this class in the GUI

Class Guicdrawfpoint:public Cdrawfpoint {Public:guicdrawfpoint () {} ~guicdrawfpoint () {} int drawpoint (int x,int Y,RGB R GB) {/*painter */return 1;} int drawLine (int sx,int sy,int ex,int Ey,rgb RGB) {return 1;} int drawrect (int left,int top,int Width,int Height,bool Fill,rgb RGB) {return 1;} int drawstring (const char *str,int x,int y,int Font,rgb RGB) {return 1;}};

The

    also passes the instantiated pointer to the library, which, of course, can be inherited directly from the GUI.
    3:stl & boost functor
    STL and boost functor provide more powerful functionality, but are subject to the limitations of the number of parameters.
    in the previous 1,2 method, you can define the number of parameters, and the number of function pointers or member functions, depending on your requirements, but not
    Directly to the GUI layer of the member functions to help top and callback, the following describes the implementation of a template
    4: Manual write Template
    in the Libraray layer:

     Template <class t> class idrawpoint:public cdrawfpoint {public:typedef int (T::* draw_point) ( int x,int Y,rgb RGB); typedef int (T::* draw_line) (int sx,int sy,int ex,int ey,rgb RGB); typedef int (T::* draw_rect) (int left,int top,int width,int height,bool fill,rgb RGB); typedef int (T::* draw_string) (const char *str,int x,int y,int font,rgb RGB); Idrawpoint (T *obj,draw_point drawpoint,draw_line drawline,draw_rect drawrect,draw_string drawString) {m_guiObj = obj; m _drawpoint = Drawpoint; M_drawline = DrawLine; M_drawrect = DrawRect; m_drawstring = drawstring; * * Implement pure virtual function/int drawpoint (int x,int Y,rgb RGB) {return (M_guiobj->*m_drawpoint) (X,Y,RGB);} int drawLine (int sx,int s Y,int ex,int Ey,rgb RGB) {return (M_guiobj->*m_drawline) (SX,SY,EX,EY,RGB);} int DrawRect (int. left,int Top,int Width,i NT Height,bool Fill,rgb RGB) {return (M_guiobj->*m_drawrect) (LEFT,TOP,WIDTH,HEIGHT,FILL,RGB);} int DrawString ( const char *str,int x,int y,int Font,rgb RGB) {RETUrn (m_guiobj->*m_drawstring) (STR,X,Y,FONT,RGB); } private:t *m_guiobj; Draw_point M_drawpoint; Draw_line M_drawline; Draw_rect M_drawrect; Draw_string m_drawstring; }; Define void Accept (class Cdrawfpoint *FP) in the library's interface class {m_fp = FP;} Cdrawfpoint *M_FP;  

In the GUI layer, you can implement Cdrawfpoint interfaces directly in the member functions of a class without inheriting this class

Class myguilevel{Public:myguilevel (); ~myguilevel (); void init (); int drawpoint (int x,int Y,rgb RGB) {/*painter */return 1;} int drawLine (int sx,int sy,int ex,int Ey,rgb RGB) {return 1;} int drawrect (int left,int top,int width,int height,bool Fill, RGB RGB) {return 1;} int drawstring (const char *str,int x,int y,int font,rgb RGB) {return 1;}}; void Myguilevel::init () {/* the template/idrawpoint<myguilevel> *guidrawfpoint is instantiated here/* Guidrawfpoint = new idrawpoint< Myguilevel> (this,&myguilevel::d rawpoint, &myguilevel::d rawline, &myguilevel::d rawrect, & Myguilevel::d rawstring); /* Accept (Guidrawfpoint) */}

This design method library does not need to care about any specific implementation of the GUI layer, and the method of using the virtual function and template is implemented
Register a member function directly to the underlying method.

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.