[odb-users] indirect relatioship between objects with std::vector<boost::variant<...>>

MM finjulhich at gmail.com
Fri May 11 09:38:33 EDT 2018


odb handles transparently point-to relationships (one way or
bidirectional) like in the examples of employer/employee.

My case is a little more complex than a direct pointer relationship.

struct X {};
struct Y {};

struct Aelt {
 boost::variant<X*, Y*> elt_;
struct A {
  std::vector<Aelt> elements_;
using nstring = odb::nullable<std::string>;

#pragma db value(Aelt) definition
#pragma db member(Aelt::elt_) transient
#pragma db member(Aelt::X) virtual(nstring) get(...) set(...)   //// can be NULL
#pragma db member(Aelt::Y) virtual(nstring) get(...) set(...)   //// can be NULL
/// The getter and setter just stores the name of X or the name of Y

#pragma db object(A) definition
#pragma db member(A::elements_)

This however does not store the X/Y objects themselves if they haven't
been persisted.

If I want to have odb persist/load Xs and Ys when I persist A
instances, I would need to implement that myself in get(...) and
set(...), but I feel I'd be going about that incorrectly.

Can I leverage odb's transparent handling in this case?



More information about the odb-users mailing list