C/C++编程-工程实践-编程规范

变量

auto全局变量 type g_xxx_xxx 跨文件引用,作用于整个工程 多用来表示状态,标志位等。
static全局变量 type xxx_xxx 文件内引用,作用于模块,多用于模块内的关键数据,模块主体作用对象。

auto局部变量 type xxx_xxx 函数内部引用,内生临时性,二次解释。
static局部变量 ,,,,,大家懂得。

对象类型封装

/* 底层模块 - 被调用者*/

typedef struct{
	char			*name;
	attribute		attr;
	status			state;
	function		handler;
	......
}object;

static object obj_noa = {.name = "nodeA", ..};
static object obj_nob = {.name = "nodeB", ..};
static object obj_noc = {.name = "nodeC," ..};
static object *obj_admin[] = {&obj_noa,  &obj_nob, &obj_noc};


void init_all_object(void)
{
}

/* 修改对象 */
void init_obj_object(object  *obj)
{
}

/* 获取对象 */
object *get_obj(const char *name)
{
	int i = 0;
	int n = 0;
	
	n = sizeof(obj_admin)/sizeof(obj_admin[0];
	for(; i < n; i++))
	{
		if(strcmp(name, obj_admin[i].name) != 0) break;
		else
			return obj_admin[i];
	}
	if(i >= n)
	{
		/* 未找到此对象 */
		return Null;
	}
}

/* 修改对象属性 */
errcode set_attribute_object(attribute *obj_attr)
{
}

/* 获取对象属性 */
errcode get_attribute_object(attribute *obj_attr)
{
}

...
/* 上层模块 - 调用者 */
#include "obj.h"

/* 修改模块内原对象 */
void config_obj(void)
{
	object *obj_temp;

	obj_temp = get_obj("nodeA");	/* 获取模块内定义的原对象 */
	
	obj_temp->attr.x0 = 0;
	...
	obj_temp->attr.xn = 0;
	
	set_attribute_object(&obj_temp->attr);
	init_obj_object(obj_temp);
}

static object *obj_new = {0};
/* 获取模块对象信息,做应用 */
void fun(void)
{
	object *obj_temp = {0};
	
	obj_temp = get_obj("nodeA");	/* 获取模块内定义的原对象 */
	memcpy(obj_new , obj_temp);		/* memcpy有读取,写入功能 */
	obj_new.handler();
}

/* 获取模块对象信息,写进原对象 */
void config_obj(void)
{
	object *obj_temp;

	obj_temp = get_obj("nodeA");	/* 获取模块内定义的原对象 */
	
	obj_temp->attr.x0 = 0; 修改
	....
}

接口

文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值