[odb-users] raw pointer to application's global array

Boris Kolpackov boris at codesynthesis.com
Wed May 28 09:45:14 EDT 2014


Hi,

MM <finjulhich at gmail.com> writes:

> What can be the repr of that field as a column in the sql table?

Storing the value itself is probably the most straightforward option.
But if the array does not change, or if you will only add new elements
at the end, then you could store the index into this array instead.


> Loading would load the row from the table and somehow make the pointer
> point to right element of the static global array.
> Storing would store the persistent object in the table.
> 
> I assume the pointee (string) could be stored in the table but i'm
> not sure how to implement that?

The best way to handle such cases (i.e., storing something other that
the value of the data member) is to use virtual data members (see
Section 14.4.13, "virtual" in the ODB manual). With the virtual data
member you can specify your own accessors/modifiers that will do
the translation that you want. For example:

#pragma db object
class object
{
  ...

  #pragma db transient
  std::string* foo_;

  #pragma db member(foo) virtual(std::string) get(*this.foo_) set(set_foo)

  void set_foo (const std::string& str)
  {
    // Find str in the array and set foo_ to point to that element.
  }
};

If you would rather store the array index, then the same class would
look along these lines:

extern std::string array[N]; // Global array.

#pragma db object
class object
{
  ...

  #pragma db transient
  std::string* foo_;

  #pragma db member(foo) virtual(std::size_t) \
    get(this.foo_ - array)                    \
    set(this.foo_ = array + (?))
};

Here we don't even need any functions since the accessor and modifier
expressions are really simple and we can just write them inline.

Boris



More information about the odb-users mailing list