[odb-users] How to represent member double* in a persistent class

Boris Kolpackov boris at codesynthesis.com
Fri Aug 12 09:33:31 EDT 2011


Sum, Eric B <eric.b.sum at lmco.com> writes:

> What is the best way to represent a pointer to a double in odb?

In the upcoming release of ODB we have added the concept of wrappers
which would make handling this case quite easy. In the meantime, you
will need to map double* to a suitable database type (REAL for SQLite)
by, for example, adding something like this into your header:

typedef double* double_ptr;
#pragma db value(double_ptr) type("REAL")

You will also need to create a value_traits specialization for double*.
The following example shows how it could look (again for the SQLite
database) assuming double* points to a dynamically allocated value:

namespace odb
{
  namespace sqlite
  {
    template <>
    struct value_traits<double*, id_real>
    {
      typedef double* value_type;
      typedef double query_type;
      typedef double image_type;

      static void
      set_value (double*& v, double i, bool is_null)
      { 
        if (is_null)
        {
          delete v;
          v = 0;
        }      
        else
        {
          if (v == 0)
            v = new double;
         
          *v = i;
        }
      }

      static void
      set_image (double& i, bool& is_null, double* v)
      {
        is_null = (v == 0);

        if (!is_null)
          i = *v;
      }
    };
  }
}

See the 'mapping' example for information on where to place this traits
specialization and how to include it into the generated code.

Boris



More information about the odb-users mailing list