我想要一组宏来声明如下内容:
#define DECL_ITEM( var_name, type, array, flags, comment )
type var_name array, ///< comment
不幸的是,预处理器将剥离///< comment
.有什么技巧可以让我的宏输出变量声明及其注释吗?
我希望
DECL_ITEM( var1, int, [ 10 ], 0, "What var1 stands for." )
输出如下所示:
int var1[ 10 ], ///< What var1 stands for.
谢谢!
我理解你的想法,但建议你使用像PHP这样的脚本语言作为你的代码生成器,而不是CPP。
一个例子是:
class MetaInfo
{
public $name;
public $type;
public $arr_w;
public $flags;
public $comment;
public function __construct( $n, $t, $a, $f, $c )
{
$this->name = $n;
$this->type = $t;
$this->arr_w = $a;
$this->flags = $f;
$this->comment = $c;
}
};
function decl_db( $db_defs )
{
echo '
struct dataBase
{
';
foreach( $db_defs as $def )
{
if ( $def->arr_w == "" )
$decl="t$def->type $def->name; ///< $def->commentn";
else
$decl="t$def->type $def->name[ $def->arr_w ]; ///< $def->commentn";
print $decl;
}
echo '
};
';
}
// ------------------------------------------------------------
// Custom DB definitions.
$db_defs = array(
new MetaInfo( "var1", "int", "10", "0", "What var1 stands for." ),
);
decl_db( $db_defs );
它应该输出:
struct dataBase
{
int var1[ 10 ], ///< What var1 stands for.
};
预处理器不打算在编译器输入阶段以外的任何环境中运行,因此不提供仅对独立使用有意义的功能。