[odb-users] indirect relatioship between objects with
std::vector<boost::variant<...>>
Boris Kolpackov
boris at codesynthesis.com
Mon May 14 11:04:40 EDT 2018
MM <finjulhich at gmail.com> writes:
> 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.
While I am fuzzy on what exactly you are trying to achieve, if you
want the object pointer/relationship functionality, then you will
need to make Alert::X and Alert::Y object pointers.
Boris
More information about the odb-users
mailing list