garlix
garlix

Reputation: 596

Using a macro to create Q_INVOKABLE functions

I'm trying to simplify (i.e. get rid of loads of boilerplate code) the creation of functions in a class that have to be marked as "INVOKABLE".

(very much like this other question other question)

To start small, I'm just trying it with one function:

#define CONCAT_NOEXPAND(A, B) A ## B
#define CONCAT(A, B) CONCAT_NOEXPAND(A, B)

#define HANDLER_PREFIX handler_
#define HANDLER_SIGNATURE (QString action, QString parameters)

#define GENERATE_HANDLER_SIGNATURE(ACTION) CONCAT(HANDLER_PREFIX, ACTION) HANDLER_SIGNATURE

#define GENERATE_HANDLERS(NAME) void GENERATE_HANDLER_SIGNATURE(NAME);

class Test : public QObject
{
    Q_OBJECT

public:
    explicit Test(QObject *parent = nullptr);

private:

    Q_INVOKABLE void handler_Test1 (QString, QString);  // Ok
    Q_INVOKABLE GENERATE_HANDLERS(Test2)  // Error!!
}

Using the MACRO GENERATE_HANDLERS produce this errors:

I've also tried to include Q_INVOKABLE directly in the MACRO GENERATE_HANDLERS, this results in a code that compile but the functions are not being exported.

Any ideas?

Upvotes: 2

Views: 489

Answers (1)

GAVD
GAVD

Reputation: 2134

Q_INVOKABLE is processed by Meta-Object Compiler (moc), which does not expand macros itself and is run before macros are expanded, so it doesn't see your macro.

Upvotes: 3

Related Questions