[odb-users] PRAGMA_DB in preprocessor define

Boris Kolpackov boris at codesynthesis.com
Thu Nov 5 04:21:48 EST 2015


Hi Zhao,

Zhao Xiang <xiang.zhao at gamegou.com> writes:

> I'm trying to put a "#pragma db column" in a preprocessor macro [...]
>
> [...]
>
> However the odb compiler gives me two errors:

Yes this is a limitation/bug in the ODB compiler. The "position 
pragmas" (i.e., those that apply to the declaration that follow
them) are implemented using source locations. For macros we happen
to use the macro expansion point as the location of the pragmas
inside the macro. And fixing this properly won't be simple. On
the other hand, there is a pretty simple workaround. It makes the
macro slightly more verbose but is probably not a big deal in this
context:

#include <odb/core.hxx>

/*
#define ODB_COUNT_EXISTS(obj, key)                     \
        PRAGMA_DB(view object(obj))                    \
        struct obj##_count                             \
        {                                              \
          PRAGMA_DB(column("count(" + obj::key + ")")) \
          int count;                                   \
        };
*/

#define ODB_COUNT_EXISTS(obj, key)                     \
        struct obj##_count                             \
        {                                              \
          int count;                                   \
        };                                             \
        PRAGMA_DB(view(obj##_count) object(obj))       \
        PRAGMA_DB(member(obj##_count::count)           \
                  column("count(" + obj::key + ")"))   \

#pragma db object
struct test
{
  #pragma db id
  int id;
};

ODB_COUNT_EXISTS(test, id)

Boris



More information about the odb-users mailing list