I'd like to store some class info using Q_CLASSINFO macro. However I would like to wrap it in my own macro, for example:
#define DB_TABLE( TABLE ) \
Q_CLASSINFO( "db_table", #TABLE )
#define DB_FIELD( PROPERTY, COLUMN ) \
Q_CLASSINFO( "dbcol_" #PROPERTY, #COLUMN )
class Foo : public QObject
{
Q_OBJECT
DB_TABLE( some_table )
DB_FIELD( clientName, client_name )
}
Unfortunately, moc doesn't expand macros so the Q_CLASSINFO is not added.
I've tried to feed moc with already preprocessed source, but it failes on some included Qt classes.
Do you know any workaround for this?
Other than rolling your own pre-moc preprocessor, no. That is what MeeGo Touch does, for example. Since Nokia themselves are doing it, I believe there is no other way.
In your case, it would only involve translating your own declarations into Q_CLASSINFO, so it shouldn't be too hard. If you use qmake, it can be added to the build sequence, too.
The easy way to make that is modifing moc preprocessor.
Search the line:
Compile the new source code. The moc executable file is generated to /bin folder (if you use windows look at c:/bin/moc.exe)
Go to Qt bin (C:\Qt\Qt5.0.1\5.0.1\msvc2010\bin) folder and rename moc executable file e.g. moc.exe.bak
Copy new moc executable file to Qt bin folder.
In your current app you need to create a Macro for example:
Finally I let you my own source code of function replaceCustomMacros:
This function convert Q_SERVICE_INFO(method, path, type) macro to Q_CLASSINFO("srv://method", "type:path")
I have not found any specific solution on Internet then I have posted this solution.
Good Luck :)