[odb-users] Simple Raw Pointer Container

Boris Kolpackov boris at codesynthesis.com
Fri Feb 28 06:47:12 EST 2014


Hi Sachin,

Sachin Kumar <sachnk at gmail.com> writes:

> However, if I change the std::vector to be a vector of MyElement
> pointers, it doesn't work. I get a "error: unable to map C++ type"
> error from the ODB compiler.
> 
> I've tried several things to fix this:
> 
> 1. Use #pragma db object instead of #pragma db value for MyElement. This
> seems fix the ODB compile problem, except that the myholder_myelement table
> now contains the actual pointer value, which isn't at all useful. Is this
> expected?

Yes, if you make MyElement an object instead of a value, then it gets
its own table and the table corresponding to the vector contains this
object's ids instead of the data itself. In other words, what you get
is a vector of pointers to objects (a relationship).


> 2. I've also tried using boost::shared_ptr, thinking that perhaps using raw
> pointers causes problems, but to no avail.

That should work provided that you enable the Boost profile (libodb-boost,
'-p boost' ODB compiler option).


> What's the right way to do this?

If you want to keep MyElement a value, then the correct way is to
use something like boost::shared_ptr or std::unique_ptr (C++11).

Just for the background, in ODB, a smart pointer is treated
differently depending on whether it is a smart pointer to object
or value. If it is a pointer to object, then you get an object
pointer. If, however, it is a pointer to value, then it is
treated as a wrapper. A wrapper in ODB is an entity that
contains a value. For example, boost::optional is a wrapper.
So is shared_ptr, auto_ptr, unique_ptr, etc. Wrappers normally
provide additional functionality, such as NULL semantics. Note
that a raw pointer to object is recognized as an object pointer
but a raw pointer to value is not, at least by default (there is
a way to make raw pointers work; see libodb/odb/wrapper-traits.hxx)
You can read more about this in Section 7.3, "Pointers and NULL
Value Semantics".

Boris



More information about the odb-users mailing list