c++odb

Moving odb pragmas outside class header


Is way to move odb (c++ orm like framework) pragmas outside class header? For example I define class basic_object (abstract) with id only:

class basic_object  {
   int _id;
public:
    int get_id() const;
    void set_id(int _id);
};

And then create pragmas for that class in another file

#pragma db object(basic_object) abstract
#pragma db member(basic_object::_id) get(get_id) set(set_id) id auto

Solution

  • Yes, you can, it's called named pragmas.

    In your other file you would have to write

    #pragma db object(basic_object)
    #pragma db member(basic_object::_id) id
    

    Then you have to tell the odb compiler where to look for. You can either do this by adding

    #ifdef ODB_COMPILER
    #include "other_file.hxx"
    #endif
    

    to your original file OR by using

    --odb-epilogue '#include "other_file.hxx"'

    as parameter to the odb compiler.


    But there is one problem in the example class basic_object you had given: Your data field _id is private. You can fix this by

    declaring it public

    or

    by adding the odb access class as a friend in your class with:

    private:
    friend class odb::access;