您的位置:首页 > 编程语言 > C语言/C++

读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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: