[odb-users] Is this possible?

Boris Kolpackov boris at codesynthesis.com
Wed Jun 27 09:45:56 EDT 2012


Hi Miguel,

Miguel Revilla Rodríguez <yo at miguelrevilla.com> writes:

> I have a polymorphic class called 'node':
> 
> #pragma db object polymorphic
> class node { ... } ;
> 
> Now, node is inherited by several other classes (user, group, etc).
> And one of those classes (group) is a node that defines a group of
> nodes. I want to set a many-to-many relationship where a group
> contains a number of nodes and a node belongs to a number of groups.

This is pretty easy to arrange if both node and group are in the same
header file (generally a good idea when you have such a tight integration
between the two). The idea is to forward-declare one of the classes:

class group;

class node
{
  #pragma db value_not_null inverse(nodes_)
  std::vector<odb::lazy_weak_ptr<group> > groups_ ;
};

class group: public node
{
  #pragma db value_not_null
  std::vector<odb::lazy_shared_ptr<node> > nodes_ ;
};

If you want to place node and group in separate headers, things are
a bit trickier, but the idea is pretty much the same. You most likely
need to include the definition of group into node in any case (e.g.,
for destructors to work, etc). In this case simply #include group.h
after the node definition:

class group;

class node
{
  #pragma db value_not_null inverse(nodes_)
  std::vector<odb::lazy_weak_ptr<group> > groups_ ;
};

#include "group.h"

If your application doesn't need group.h in node.h (but the ODB compiler
does), then you can make the include conditional:

#ifdef ODB_COMPILER
#  include "group.h"
#endif

Boris



More information about the odb-users mailing list