[odb-users] Polymorphism through shared library

Arnaud Kapp arnaud.kapp at islog.com
Tue Nov 8 11:33:41 EST 2016


Hello Boris,

Thank you for your response.
I'm still playing with ODB, trying to correctly setup the infrastructure
for the project.

I've witness something that is cool, and I though it wouldn't be that 
straightforward for me to implement.
I expected that by default, calling db->load<Base>(id) would only 
instantiate the correct child type if it was called from a shared 
library that linked to the database support code.
However, it seems that dynamically loading a library that itself link to 
the support code is enough for ODB to instantiate the correct type.

Out of curiosity, how is this implemented? Does the database support 
code for each class register itself, saying "Hey I can handle object 
with typeid "MyNamespace::myType" and, from this, ODB is able to 
instantiate the correct type?


An exception is thrown when ODB tries to instantiate a type that is "not 
present".
I am not yet sure it makes sense, but would it be possible to tell ODB 
to fallback to instantiating an other type in case
the type is not available? If DerivedType cannot be instantiated,
fall-back to instantiating ParentType.

I have a use case for this, but on the other hand I wonder if I'm not 
opening a can of worm that should stay closed:
    + User loads a plugin that generates some data. Base table now has
some entries with typeid "MyPlugin::MyObject".
    + User decides it doesn't want to use the plugin after all. Base 
table still has those entries, and any query that would return those
would now throw an exception.

Thank you for your support,

On 11/04/2016 02:29 PM, Boris Kolpackov wrote:
> Hi Arnaud,
>
> Arnaud Kapp <arnaud.kapp at islog.com> writes:
>
>> Sorry, this mail is getting a bit long, and is kinda messy.
>
> Which is usually a good indication that even if you make it to work, it
> will probably come to bite your later.
>
> And the way you may be able to make it work is by taking the existing
> building blocks and finding a way to combine them to achieve what you
> want.
>
> Specifically, in ODB polymorphic inheritance is open-ended and the
> base schema does not need to be changed in order to add a new derived
> class. This is good for you.
>
> Now you need to find a way to place derived classes into a separated
> schema (--schema-name) with a separate version. I would start with
> something like this:
>
> // base.hxx
>
> #ifndef NO_BASE_VERSION
> #  pragma db model version(...)
> #endif
>
> #pragma db object polymorphic
> class base
> {
>   ...
> };
>
> // derived.hxx
>
> #define NO_BASE_VERSION
> #include "base.hxx"
>
> #pragma db model version(...)
>
> #pragma db object
> class derived: public base
> {
>   ...
> };
>
> Then I would try to compile them like so and see what I get:
>
> odb -s --schema-name base    base.hxx
> odb -s --schema-name derived derived.hxx
>
> One thing to keep in mind with this approach is that the derived schema
> will always expect to see the latest version of the base scheme. This
> might have implications.
>
> Boris
>

-- 
Best regards / Cordialement,

Arnaud KAPP
31, Avenue du Général de Gaulle
67000 Strasbourg
FRANCE
http://www.islog.com



More information about the odb-users mailing list