栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解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 sumrn");
    return a + b;
}

static int cal_sub(int a, int b)
{
    printf("now is subrn");
    return a - b;
}

static int cal_mul(int a, int b)
{
    printf("now is mulrn");
    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=%drn", result);
    }
    return result;
}

void app_main(void)
{
    printf("init donern");
    int m = 10, n = 1, ret;

    ret = test2_cal(m, n);
}

上述代码通过 test2_cal() 实现计算接口的统一。只需改变函数指针 s_cal 的值,就可以让 test2_cal()执行不同的功能。我们可以拷贝上述程序分别对 s_cal赋值 cal_sum、cal_sub、cal_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 sumrn");
    return a + b;
}

static int cal_sub(int a, int b)
{
    printf("now is subrn");
    return a - b;
}

static int cal_mul(int a, int b)
{
    printf("now is mulrn");
    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=%drn",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 donern");
    int m = 10, n = 2, ret;

    printf("test 1***************beginrn");
    test1_cal(m, n);
    printf("test 1***************endrn");

    printf("test 2***************beginrn");
    my_cal_calculate_register(cal_mul);
    test1_cal(m, n);
    printf("test 2***************endrn");

    printf("test 3***************beginrn");
    my_cal_calculate_unregister(cal_mul);
    test1_cal(m, n);
    printf("test 3***************beginrn");
}

上述代码通过在函数 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 sumrn");
    return a - b;
}

static int cal_add_self(int a)
{
    return a+1;
}

static void cal_send_to_printf(int a)
{
    printf("total is %drn", a);
}

static void cal_send_to_printf2(int a)
{
    printf("now total is %drn", 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 %drn", result);
    }

    if(s_test.m_add){
        result = s_test.m_add(result);
        printf("result1 is %drn", result);
    }

    if(s_test.m_printf) {
        s_test.m_printf(result);
    }
    
    return result;
}

void app_main(void)
{
    printf("init donern");
    int m = 10, n = 2;

    printf("test 1***************beginrn");
    test1_cal(m, n);
    printf("test 1***************endrn");

    printf("test 2***************beginrn");
    s_test.m_printf = cal_send_to_printf2;
    test1_cal(m, n);
    printf("test 2***************endrn");
}

上述程序中通过在结构体 s_test中使用三个函数指针 m_calculate、m_add、m_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)实现程序分层设计
(码字不易,谢谢点赞或收藏)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/783917.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号