imad
imad

Reputation: 195

Use macro define between module interface and implementation unit

I have a module interface unit in a SomeModule.ixx file, with some functions prototypes generated using a preprocessor macro:

export module SomeModule;

#define GENERATED_FUNCTION(ret, name, ...) typedef ret name##_prototype(__VA_ARGS__); \

#define GENERATED_FUNCTIONS_LIST \
GENERATED_FUNCTION(void, some_function, int param1, double param2) \
GENERATED_FUNCTION(void, some_function2, int param1, float param2) \
GENERATED_FUNCTION(void, some_function3, int* param1, int** param2) \

GENERATED_FUNCTIONS_LIST

#undef GENERATED_FUNCTION

export struct SomeStruct {
    void init();

#define GENERATED_FUNCTION(ret, name, ...) \
name##_prototype* name;

    GENERATED_FUNCTIONS_LIST
#undef GENERATED_FUNCTION
};

and an implementation unit in SomeModuleImpl.cpp :

module SomeModule;

void SomeStruct::init() {

#define GENERATED_FUNCTION(ret, name, ...) \
    name = NULL;

    GENERATED_FUNCTIONS_LIST
#undef GENERATED_FUNCTION

}

I want to set some value for the methods in SomeStruct, again using the macro, however I'm getting this error in the module implementation:

E0020   identifier "GENERATED_FUNCTIONS_LIST" is undefined  

Is there any way to share macro defines between module interface and implementation?

Upvotes: 1

Views: 692

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473272

Is there any way to share macro defines between module interface and implementation?

Yes, the same one you used to use: a header file. Put your GENERATED_FUNCTIONS_LIST macro in a header file. This is pretty standard for X-macro-style code.

Modules don't include macros, so it doesn't change much of what you have to do there.

Upvotes: 1

Related Questions