目录
  • 详解C语言中回调函数的含义与使用场景(2)
    • 使用场景一(重定义):
    • 使用场景二(扩展函数功能):
    • 使用场景三(分层):
  • 总结

    详解C语言中回调函数的含义与使用场景(2)

    引言:在上一篇文章中介绍了回调函数的概念与使用方法,本节将深入地介绍回调函数典型的使用场景。通过使用回调函数可以实现驱动和应用程序的分离解耦,让程序更加地灵活。也可以借助回调函数实现插入自定义代码、分层设计程序的思想。

    使用场景一(重定义):

    在统一的接口中,动态地改变一个函数的功能。该函数的功能可以是加载参数、或者执行运算。示例如下:

    typedef int (*my_calculate_t)(int a, int b);
    static int cal_sum(int a, int b)
    {
        printf("now is sum\r\n");
        return a + b;
    }
    static int cal_sub(int a, int b)
    {
        printf("now is sub\r\n");
        return a - b;
    }
    static int cal_mul(int a, int b)
    {
        printf("now is mul\r\n");
        return a * b;
    }
    static my_calculate_t s_cal = cal_sum;
    static int test2_cal (int a, int b)
    {
        int result = 0;
        if(s_cal) {
            result = s_cal(a ,b);
            printf("result=%d\r\n", result);
        }
        return result;
    }
    void app_main(void)
    {
        printf("init done\r\n");
        int m = 10, n = 1, ret;
        ret = test2_cal(m, n);
    }
    

    上述代码通过 test2_cal() 实现计算接口的统一。只需改变函数指针 s_cal 的值,就可以让 test2_cal()执行不同的功能。我们可以拷贝上述程序分别对 s_cal赋值 cal_sumcal_subcal_mul实现在不改动其他代码的情况下,让 test2_cal 执行不同的运算。这种通过改变函数指针 s_cal 的值,让函数 test2_cal() 执行不同功能的特性,可以称之为重定义test2_cal()的功能。

    上述程序运行结果:

    init done
    now is sum
    result=11

    使用场景二(扩展函数功能):

    可以在程序中定义多个回调函数,若定义了就执行,否则就略过。实现在函数中扩展更多代码的目的(就像一个钩子函数一样)。示例如下:

    typedef int (*my_calculate_t)(int a, int b);
    static int cal_sum(int a, int b)
    {
        printf("now is sum\r\n");
        return a + b;
    }
    static int cal_sub(int a, int b)
    {
        printf("now is sub\r\n");
        return a - b;
    }
    static int cal_mul(int a, int b)
    {
        printf("now is mul\r\n");
        return a * b;
    }
    static my_calculate_t s_c_array[5] = {cal_sum, cal_sub};
    static int test1_cal(int a, int b)
    {
        volatile int result = 0;
        volatile size_t i = 0;
        for(i=0; i<(sizeof(s_c_array)/sizeof(my_calculate_t)); i++) {
            if (s_c_array[i] != NULL){
                result = s_c_array[i](a, b);
                printf("i=%d, result=%d\r\n",i, result);
            }
        }
        return result;
    }
    static void my_cal_calculate_register(my_calculate_t cal)
    {
        for(size_t i=0; i<(sizeof(s_c_array)/sizeof(my_calculate_t)); i++) {
            if (s_c_array[i] == NULL){
                s_c_array[i] = cal;
                return;
            }
        }
    }
    static void my_cal_calculate_unregister(my_calculate_t cal)
    {
        for(size_t i=0; i<(sizeof(s_c_array)/sizeof(my_calculate_t)); i++) {
            if (s_c_array[i] == cal){
                s_c_array[i] = NULL;
                return;
            }
        }
    }
    void app_main(void)
    {
        printf("init done\r\n");
        int m = 10, n = 2, ret;
        printf("test 1***************begin\r\n");
        test1_cal(m, n);
        printf("test 1***************end\r\n");
        printf("test 2***************begin\r\n");
        my_cal_calculate_register(cal_mul);
        test1_cal(m, n);
        printf("test 2***************end\r\n");
        printf("test 3***************begin\r\n");
        my_cal_calculate_unregister(cal_mul);
        test1_cal(m, n);
        printf("test 3***************begin\r\n");
    }
    

    上述代码通过在函数 test1_cal()增加一个指针数组 s_c_array[] 来控制函数 test1_cal()中实际执行调用哪些函数。默认的情况下,它仅调用 cal_sum 和 cal_sub两个函数,通过函数 my_cal_calculate_register()可以增加它调用的函数,示例中 my_cal_calculate_register(cal_mul);语句增加了其内部调用一个 cal_mul函数。

    运行结果:

    init done
    test 1***************begin
    now is sum
    i=0, result=12
    now is sub
    i=1, result=8
    test 1***************end
    test 2***************begin
    now is sum
    i=0, result=12
    now is sub
    i=1, result=8
    now is mul
    i=2, result=20
    test 2***************end
    test 3***************begin
    now is sum
    i=0, result=12
    now is sub
    i=1, result=8
    test 3***************begin
     

    使用场景三(分层):

    通过在结构体中使用 函数指针来实现程序的分层设计。分层带来的好处是方便维护与结构清晰。

    typedef int (*my_calculate_t)(int a, int b);
    typedef int (*add_self_t)(int a);
    typedef void (*send_to_printf_t)(int a);
    typedef struct my_test_struct_t
    {
        my_calculate_t m_calculate;
        add_self_t m_add;
        send_to_printf_t m_printf;
    }my_test_struct_t;
    static int cal_sub(int a, int b)
    {
        printf("now is sum\r\n");
        return a - b;
    }
    static int cal_add_self(int a)
    {
        return a+1;
    }
    static void cal_send_to_printf(int a)
    {
        printf("total is %d\r\n", a);
    }
    static void cal_send_to_printf2(int a)
    {
        printf("now total is %d\r\n", a);
    }
    my_test_struct_t s_test = {
        .m_calculate = cal_sub,
        .m_add = cal_add_self,
        .m_printf = cal_send_to_printf,
    };
    static int test1_cal(int a, int b)
    {
        int result = 0;
        if(s_test.m_calculate){
            result = s_test.m_calculate(a,b);
            printf("result1 is %d\r\n", result);
        }
        if(s_test.m_add){
            result = s_test.m_add(result);
            printf("result1 is %d\r\n", result);
        }
        if(s_test.m_printf) {
            s_test.m_printf(result);
        }
        return result;
    }
    void app_main(void)
    {
        printf("init done\r\n");
        int m = 10, n = 2;
        printf("test 1***************begin\r\n");
        test1_cal(m, n);
        printf("test 1***************end\r\n");
        printf("test 2***************begin\r\n");
        s_test.m_printf = cal_send_to_printf2;
        test1_cal(m, n);
        printf("test 2***************end\r\n");
    }
    

    上述程序中通过在结构体 s_test中使用三个函数指针 m_calculatem_addm_printf来实现三个步骤:计算、自增、打印,三层功能的分层。每个层都是一个函数指针,所以每一层都可以通过改变函数指针的值,实现重新定义。

    运行结果:

    init done
    test 1***************begin
    now is sum
    result1 is 8
    result1 is 9
    total is 9
    test 1***************end
    test 2***************begin
    now is sum
    result1 is 8
    result1 is 9
    now total is 9
    test 2***************end
     

    总结

    本篇内容作为上一篇文章的深化,重点讲述了回调函数的三种典型使用场景:

    • 实现函数功能重定义
    • 扩展函数功能
    • 实现程序分层设计

    本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!    

    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。