[odb-users] Mapping to nullptr_t in templated class interface

Henri Schwarz il1k3pepsi at gmail.com
Tue Jun 20 10:14:07 EDT 2017


Hi Boris,

My data model defines several persistent classes which are ordered
hierarchically using 1-1 relationships. To model these relationships I
introduced two templated interface classes that look like this.

template <class T>
struct parent_interface {

using parent_type = T;

// accessor

parent_type const& get_parent() const {

return parent_;

}

// modifier
void set_parent(parent_type const &parent) {

parent_ = parent;

}


protected:

~parent_interface() {}


private:

parent_type parent_;

}


The other interface class is named child_interface and has an equivalent
implementation.
For the example I' ll call the hierarchical persistent classes class
Level_N, e.g. class Level_1, class Level_2, class Level_3 and so forth.

So now it's obvious that the class on the first Level has no parent and the
last one has no child but nevertheless I'd like all persistent classes to
stick to the two interfaces. Therefore I provided definitions like the
following.

#pragma db object
class Level_1 : public parent_interface<nullptr_t>, public
child_interface<Level_2> {
public:

//

// ... ctor, dtor, public methods
//

protected:

friend class odb::access;

private:

//

// ... some data members
//

#pragma db member(parent) virtual(parent_type)

#pragma db member(child) virtual(child_type)

}

#pragma db object
class Level_2 : public parent_interface<Level_1>, public
child_interface<Level_2> {

// ...

}

#pragma db object
class Level_3 : public parent_interface<Level_2>, public
child_interface<nullptr_t> {

// ...

}

So what I'm trying to achieve is a mapping of the parent of the Level_1
persistent class to a null pointer or something similar so that I can do
something like this

if (!get_parent()) { // do something }

So that the start and end points in my hierarchy would be marked with null
pointers.

But the odb compiler gives me an error "unable to map C++ type
'::parent_interface< ::std::nullptr_t >::parent_type' used in data member
'parent' to a SQLite database type". It also states to use "#pragma db
type" to specify the database type but all types I tried so far
also failed. Therefore I'd like to ask you if there is a way to implement
my design or circumvent this compiler error. If it's not possible I'll have
to think of something new.

Your advice is much appreciated.

Kind regards,

Henri

P.S.: The code compiles when I omit the parent_interface inheritance in the
top level class and the child_interface inheritance in the last class of
the hierarchy but that feels more like a hack than like clean code and a
sound design.


More information about the odb-users mailing list