3
我正在尋找一種方法或一種方法來從列表中生成typedefs列表和對象實例化列表宏調用,定義這些對象的類類型和構造函數參數。C/C++宏:如何使用一個宏(boost預處理器庫?)生成兩個單獨的代碼段
它應該看起來像下面的(不工作)代碼。要解決的問題是從一個宏調用列表中生成兩個不同列表的方法。我想這是一個問題,需要使用boost預處理器庫部分來解決,但現在我已經粘貼瞭如何操作。
/////////////////////////////////////////////////////////////////////////////////
// MACRO-Definitions
#define DEF_OBJECT_TYPE(name, class, contructor_params) \
typedef class name ## type;
name ## type* name;
#define DEF_OBJECT_RUN(name, class, contructor_params) \
name ## type* name = new name ## type contructor_params; \
#define DEF_OBJECTS(definitions) \
/* Type-Header */ \
definitions \
/* Type-Footer */ \
/* Run-Header */ \
definitions \
/* Run-Footer */
#define OBJECT(name) (dynamic_cast<name ## type*>(name))
/////////////////////////////////////////////////////////////////////////////////
// Object-Definitions
DEF_OBJECTS(
DEF_OBJECT(Object1, CClass1, ("par1"))
DEF_OBJECT(Object2, CClass2, ("par1", "par1"))
)
/////////////////////////////////////////////////////////////////////////////////
// This shall be the result of the macro expansion
// shall expand to:
struct MyClass {
typedef class Object1type;
Object1type* Object1;
typedef class Object2type;
Object2type* Object2;
void Run();
}
void MyClass::Init() {
Object1type* Object1 = new Object1type("par1");
Object2type* Object2 = new Object2type("par1", "par2");
}
// end of expansion
/////////////////////////////////////////////////////////////////////////////////
// I want to use these automatic created objects in this way:
void MyClass::Run() {
OBJECT(Object1)->method_class1(1);
OBJECT(Object2)->method_class2(1,2);
}
感謝您的快速解決方案 - 它的工作原理! 生成TYPE0_DEF,TYPE1_DEF等.pp.宏接縫將成爲boost-preprocessor-library的候選者。我會盡快嘗試。 再次感謝! – Marcel 2010-03-02 13:19:58