[odb-users] Using Custom Container (with template) makes odb crash
Boris Kolpackov
boris at codesynthesis.com
Mon Dec 16 03:28:17 EST 2013
Hi Alexis,
Alexis Mestag <alexis.mestag at epitech.eu> writes:
> class Person
> {
> ...
>
> Friends *_friends;
> };
>
> class Friends
> {
> std::list<Person *> *_elts;
> };
Your wrapper approach is not exactly equivalent to the original version.
If we "unwrap" it, we will get std::list<Person*>** instead of
std::list<Person*>*. To me, it seems the equivalent will be:
class Person
{
...
Friends _friends;
};
> I implement the class odb::access::container_traits<Friends *>
The problem is, container_traits should be specialized for a container.
You are still specializing it for a pointer to a container. This won't
work.
Besides containers, ODB also has a notion of wrappers. That is, something
that wraps or holds the underlying value. I think this will be more inline
with what you are trying to do. For example, you can do something like this
out of the box:
class Person
{
...
std::unique_ptr<std::list<Person*>> _friends;
};
Because ODB includes a specialization of odb::warpper_traits for
std::unique_ptr. While there is no default specialization for the raw
pointer, you can add one yourself. In fact, the odb/wrapper-traits.hxx
file on libodb contains a commented-out implementation that has the
following comment:
// Sample specialization for raw pointers. It is not enabled by default
// since it makes many assumptions that may not always hold true (such
// as that instances are allocated with new and freed with delete).
// This makes it too dangerous to be enabled unconditionally. If you
// need this functionality, you can copy the below code into your
// application. Also consider changing it to only specialize for
// specific types instead of for any pointer (it will almost always
// do the wrong thing for char*).
Boris
More information about the odb-users
mailing list