【转】神奇的xx宏

本文介绍了一种统一管理智能手机APP信息的方法,通过在token.h文件中定义宏xx和yy来集中管理每个token的相关信息,从而避免了在不同地方重复修改代码导致的编译错误。这种方法被应用于声明APP的主窗口类、窗口名、ID、名称、INF文件名、入口函数、资源和APP说明,简化了APP添加流程,减少了人工错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载时请注明出处和作者联系方式
作者联系方式:李先静 <xianjimli at hotmail dot com>


前段时间研究lcc,看见token.h里有这样一些代码:

xx(FLOAT,     1, 0,  0,    0,      CHAR,   "float")

xx(DOUBLE,    2, 0,  0,    0,      CHAR,   "double")

xx(CHAR,      3, 0,  0,    0,      CHAR,   "char")

xx(SHORT,     4, 0,  0,    0,      CHAR,   "short")

yy(0,           94,     7,      BXOR,   bittree,'^',    "^")

yy(0,           95,     0,      0,      0,      0,      0)

yy(0,           96,     0,      0,      0,      0,      0)

yy(0,           97,     0,      0,      0,      0,      0)


#undef yy

#undef xx

初看有点不解,xx到底是个什么东西呢?但从这些信息很容易想到,其中每一行描述一个token的信息。把代码大概看了一下,主要有以下几个地方引用到token.h:

在enode.c中:

Tree (*optree[])(int, Tree, Tree) = {

#define xx(a,b,c,d,e,f,g) e,

#define yy(a,b,c,d,e,f,g) e,

#include "token.h"

};
 

在expr.c中:

static int oper[] = {

#define xx(a,b,c,d,e,f,g) d,

#define yy(a,b,c,d,e,f,g) d,

#include "token.h"

};

在output.c中:

static char *tokens[] = {

#define xx(a,b,c,d,e,f,g) g,

#define yy(a,b,c,d,e,f,g) g,

#include "token.h"

};
 

看了这些代码,一下明了了作者的意图:把每个token相关的信息放在一起,便于集中管理,而在不同的地方,通过把xx/yy定义成不同功能的宏,以提取所需要的信息。称它为xx/yy,正是因为这两个宏在使用时还有没定义,在不同的上下文中,其功能是不同的。

我们做的是一个智能手机,在项目早期总会不断有新的AP加入进来,一个APP都很多描述信息,每次加入新AP时,总会遗漏某些信息没有加入,导致Autobuild失败,搞得我很烦,检查了一下相关的代码。发现AP具有下信息:

1.主窗口类

2.主窗口名

3.ID

4.名称

5.INF文件名

6.入口函数

7.资源

8.APP的说明

每一项的定义都在不同的地方,更麻烦的是,增加一个APP竟要4个人配合修改:模块负责人本人,负责修改PC和硬件的工程文件的人,负责加入APP的ID的人,负责加APP的入口函数及其它信息的人。尽管增加APP时用邮件通知了大家,但总有人忘了修改自己的负责的地方,所以常常让Autobuild编译失败。


看了lcc里的xx/yy的方式后,我想在一个地方统一管理这些信息可以避免不必要的麻烦,于用了这种方式去声明APP的信息,如:

#define ALL_APPS_LIST /

  xx("桌面",                 STATUSBAR_CLASS,         STATUSBAR_NAME,         DESKTOP_APPID,       DESKTOPAPPNAME,          DESKTOPWINMAININF,       DesktopWin,               desktop_res)/

  xx("名片",                 CARDCLASS,               CARDNAME,               CARD_APPID,          CARDAPPNAME,             CARDWINMAININF,          Card_WinMain,             card_res)/

  xx("电话",                 TELCLASS,                TELNAME,                TELEPHONE_APPID,     TELAPPNAME,              TELWINMAININF,           Telephone_WinMain,        tel_res)/

使用方式如下:

#undef  xx

#define xx(APP_NAME_COMMENT, WIN_CLASS, WIN_NAME, APP_ID, APP_NAME, APP_INF, APP_WINMAIN, APP_RES)  {(WIN_CLASS), (WIN_NAME)},

const AppClassWindow AppClassDefine[]=

{

       ALL_APPS_LIST

       {NULL,NULL},

};
 

#undef  xx

#define xx(APP_NAME_COMMENT, WIN_CLASS, WIN_NAME, APP_ID, APP_NAME, APP_INF, APP_WINMAIN, APP_RES)  APP_ID,

typedef enum tagAPPID_E

{

    ALL_APPS_LIST

    MAX_APPID

}APPID_E;

现在只要一个人维护这张表和工程文件,其它地方都不需要修改,至此之后一切平安无事。

最近研究ZX的手机开发平台,里面用了IMAGE_DEF/TEXT_DEF等实现了类似的功能,xx换成了更具明显意义的宏,给人感觉更直观一些。

Rebuild started: Project: stm32f407 *** Using Compiler 'V5.06 update 7 (build 960)', folder: 'D:\cjc\keil\ARM\ARMCC\Bin' Rebuild target 'stm32f407' assembling startup_stm32f40_41xxx.s... compiling main.c... ..\app\main.c(23): error: #167: argument of type "struct led_desc" is incompatible with parameter of type "led_desc_t" led_on(led1); ..\app\main.c(35): warning: #1-D: last line of file ends without a newline ..\app\main.c: 1 warning, 1 error compiling boad.c... compiling led.c... ..\driver\led.c(51): warning: #1-D: last line of file ends without a newline ..\driver\led.c: 1 warning, 0 errors compiling stm32f4xx_crc.c... compiling misc.c... compiling stm32f4xx_can.c... compiling stm32f4xx_cec.c... compiling system_stm32f4xx.c... compiling stm32f4xx_adc.c... compiling stm32f4xx_cryp.c... compiling stm32f4xx_cryp_des.c... compiling stm32f4xx_cryp_tdes.c... compiling stm32f4xx_cryp_aes.c... compiling stm32f4xx_dac.c... compiling stm32f4xx_dbgmcu.c... compiling stm32f4xx_dcmi.c... compiling stm32f4xx_dfsdm.c... compiling stm32f4xx_dma.c... compiling stm32f4xx_dma2d.c... compiling stm32f4xx_dsi.c... compiling stm32f4xx_exti.c... compiling stm32f4xx_flash_ramfunc.c... compiling stm32f4xx_flash.c... compiling stm32f4xx_fmpi2c.c... compiling stm32f4xx_fsmc.c... compiling stm32f4xx_gpio.c... compiling stm32f4xx_hash.c... compiling stm32f4xx_hash_md5.c... compiling stm32f4xx_hash_sha1.c... compiling stm32f4xx_i2c.c... compiling stm32f4xx_iwdg.c... compiling stm32f4xx_lptim.c... compiling stm32f4xx_ltdc.c... compiling stm32f4xx_pwr.c... compiling stm32f4xx_qspi.c... compiling stm32f4xx_rcc.c... compiling stm32f4xx_rng.c... compiling stm32f4xx_rtc.c... compiling stm32f4xx_sai.c... compiling stm32f4xx_sdio.c... compiling stm32f4xx_spdifrx.c... compiling stm32f4xx_syscfg.c... compiling stm32f4xx_spi.c... compiling stm32f4xx_tim.c... compiling stm32f4xx_usart.c... compiling stm32f4xx_wwdg.c... ".\Objects\stm32f407.axf" - 1 Error(s), 2 Warning(s). Target not created. Build Time Elapsed: 00:00:10
最新发布
06-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值