读u-boot深入学C语言框架 [2]
2015-11-02 03:07
519 查看
#include <stdio.h> #include <string.h> #include <stdlib.h> // 功能 定义集合结构体 // 注:定义时对应位置要匹配 // 可更具需要自行添加所需成员 struct peripheralname_device{ char name[20]; int (*socname_peripheralname_gongneng1)(void); int (*socname_peripheralname_gongneng2)(int) ; void (*socname_peripheralname_gongneng3)(void); void (*socname_peripheralname_gongneng4)(int) ; }; // 功能 声明和定义同类型的函数 (比如某个SOC有多个功能相同的外设 可以这样定义声明) // 框架 DECLARE_SOC名字_外设名字_FUNCTION(用于标识同类型的外设的号码) 可以再加入你需要的 #define DECLARE_SOCNAME_PERIPHERALNAME_FUNCTION(law_param) \ int socname_peripheralname_##law_param##_goneng1(void) {printf("gongneng 1\n");return 0;} \ int socname_peripheralname_##law_param##_goneng2(int a){printf("gongneng 2\n");return 0;} \ void socname_peripheralname_##law_param##_goneng3(void) {printf("gongneng 3\n");} \ void socname_peripheralname_##law_param##_goneng4(int a){printf("gongneng 4\n");} // 功能 初始化外设结构体成员 // 框架 INIT_外设名字_STRUCTURE(用于标识同类型的外设的号码, 一些你想加入的东西ETC) #define INIT_PERIPHERALNAME_STRUCTURE(law_param, name){ \ name, \ socname_peripheralname_##law_param##_goneng1, \ socname_peripheralname_##law_param##_goneng2, \ socname_peripheralname_##law_param##_goneng3, \ socname_peripheralname_##law_param##_goneng4, } // 描述 使用上面的框架 // 1 定义和上面函数, 2 初始化结构体成员。 // 优点 大大缩短了代码量和提高可移植性 DECLARE_SOCNAME_PERIPHERALNAME_FUNCTION(1); struct peripheralname_device peripheralname_1 = INIT_PERIPHERALNAME_STRUCTURE(1,"SOC_PER_1"); DECLARE_SOCNAME_PERIPHERALNAME_FUNCTION(2); struct peripheralname_device peripheralname_2 = INIT_PERIPHERALNAME_STRUCTURE(2,"SOC_PER_2"); DECLARE_SOCNAME_PERIPHERALNAME_FUNCTION(3); struct peripheralname_device peripheralname_3 = INIT_PERIPHERALNAME_STRUCTURE(3,"SOC_PER_3"); DECLARE_SOCNAME_PERIPHERALNAME_FUNCTION(4); struct peripheralname_device peripheralname_4 = INIT_PERIPHERALNAME_STRUCTURE(4,"SOC_PER_4"); int main(void){ // 调用结构体成员 peripheralname_1.socname_peripheralname_gongneng1(); peripheralname_2.socname_peripheralname_gongneng2(520); peripheralname_3.socname_peripheralname_gongneng3(); peripheralname_4.socname_peripheralname_gongneng4(520); return 0; }
相关文章推荐
- 读u-boot深入学C语言框架 [1]
- [c]C语言小练习题
- 判断1000年---2000年之间的闰年
- 输出乘法口诀表
- 打印100~200 之间的素数,两种方法
- C语言的分支语句解析
- C语言第二章
- C语言 第一章
- eclipse开发c++
- C++基础——tricks,让人惊艳的那些代码
- hiho 6 01背包
- C++ ofstream和ifstream
- C语言/操作系统学习笔记——管道通信
- C语言基础-#include
- C语言中的算术运算符和算术表达式
- leetcode笔记:Triangle
- c语言二进制八进制和十六进制的总结
- 判断闰年 2.0
- C++中的类所占内存空间总结
- C语言中指针与数组的差别