2

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:

  • error: pasting "handler_" and "(" does not give a valid preprocessing token #define HANDLER_PREFIX handler_
  • error: expected unqualified-id before ‘void’ void GENERATE_HANDLER_SIGNATURE(NAME);

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?

garlix
  • 576
  • 9
  • 26
  • have you tried to expand the macro without code compilation e.g. in gnu compilers by using `-E` option to see what does really happen? – W.F. Sep 14 '17 at 14:24
  • Yes. I've tried my MACROs and they [work great](https://godbolt.org/g/5vXXMJ). The problem, imho, is that `Q_INVOKABLE` doesn't expand my MACRO – garlix Sep 14 '17 at 14:44
  • @garlix I don't know how to check it, but i suppose that the Q_INVOKABLE macro is executed first, does its job, and then your macros are executed, and they aren't agree with each other. – Macias Sep 15 '17 at 02:17
  • 1
    @garlix It compiles under msvc2013/15 Qt 5.6, 5.8, 5.9 – Macias Sep 15 '17 at 02:38
  • @Macias so maybe they fixed macro expansion in qt version > 4.8. Thank you very much to let me know this – garlix Sep 15 '17 at 07:24

1 Answers1

3

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.

GAVD
  • 1,977
  • 3
  • 22
  • 40