变量
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; 修改
....
}