[odb-users] Mapping a templated wrapper class

Boris Kolpackov boris at codesynthesis.com
Tue Sep 10 08:35:42 EDT 2013


Hi Cort,

R. Cortland Tompkins <cort.tompkins at ipconfigure.com> writes:

> [...] went back to using value_traits instead of wrapper_traits,
> and modified the MS and Oracle callbacks as required.
> 
> On-the-fly serialization is an improvement, but the ODB compiler still
> cannot map my wrapper type automatically.

ODB won't be able to do it with the value_traits approach. That is,
there is no support for mapping type templates, only types.

The wrapper (in ODB sense) trick worked because ODB, after not
finding the type specification for the wrapper itself (which is
a template), will then look if there is one for the wrapped type
(which is not a template).

You can still make it work by combining the two approaches. Here
is an outline:

#pragma db value type("TEXT")
class archiveable_base
{
public:
  virtual std::string
  serialize () const = 0;

  virtual void
  deserialize (const std::string&) = 0;
};

// Provide the value_traits<archiveable_base, id_text> specialization
// using serialize()/deserialize() to get/set data.

template <typename T>
class archiveable: public archiveable_base
{  
public:
 
  ...

  // Implement these.
  //
  virtual std::string
  serialize () const;

  virtual void
  deserialize (const std::string&);
};

// Provide wrapper_traits partial specialization for archiveable. Note
// the wrapped_type typedef.
//
namespace odb
{
  template <typename T>
  class wrapper_traits<archiveable<T> >
  {
  public:
    typedef archiveable_base wrapped_type;
    typedef archiveable_base unrestricted_wrapped_type;

    typedef archiveable<T> wrapper_type;   

    static const bool null_handler = false;
    static const bool null_default = false;

    static const wrapped_type&
    get_ref (const wrapper_type& o)
    {
      return o; // Implicit conversion from derived to base.
    }

    static unrestricted_wrapped_type&
    set_ref (wrapper_type& o)
    {
      return o; // Implicit conversion from derived to base.
    }
  };
}

Boris



More information about the odb-users mailing list