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

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

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

使用场景一(重定义):

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

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_sumcal_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

总结

本篇内容作为上一篇详解C语言中回调函数的含义与使用场景[1]的深化,重点讲述了回调函数的三种典型使用场景:
1)实现函数功能重定义
2)扩展函数功能
3)实现程序分层设计
(码字不易,谢谢点赞或收藏)

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

物联网老王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值