[odb-users] Can odb be used without touching the domain object files?

Boris Kolpackov boris at codesynthesis.com
Mon Sep 3 08:18:24 EDT 2012


Hi Candy,

Candy Chiu <candy.chiu.ad at gmail.com> writes:

> I'd like to add db capability to a project without modifying the existing
> .h's.  Is there another way to achieve mapping in this case?

Yes, especially with the upcoming 2.1.0 release which adds support for
accessor/modifier functions. Basically, such non-intrusive mapping
relies on the following ODB features:

1. Ability to specify mapping pragmas outside the class and even in
   a separate file (see Chapter 12, "ODB Pragma Language" for details).

2. Ability to use accessor/modifier functions (or, more generally,
   expressions) to access data members in a class (see Sections 12.4.5
   "get/set/access" in the upcoming ODB 2.1.0 release).

As an example, let's assume we have the following person class that
is saved in person.hxx and which we cannot modify:

// person.hxx
//
class person
{
public:
  person ();

  const std::string& email () const;
  void email (const std::string&);

  const std::string& get_name () const;
  std::string& set_name ();

  unsigned short getAge () const;
  void setAge (unsigned short);

private:
  std::string email_;
  std::string name_;
  unsigned short age_;
};

To convert this class to ODB persistent class, we can create a separate
file, person-mapping.hxx, with the following content:

// person-mapping.hxx
//
#pragma db object(person)
#pragma db member(person::email_) id

Next we compile person.hxx with the ODB compiler like this (assuming
ODB 2.1.0 or later):

odb ... --odb-epilogue "#include \"person-mapping.hxx\"" person.hxx

And now we have person-odb.?xx files that we can use in a normal way.

We plan to release ODB 2.1.0 in the next week or two, however, if you
want to give this a try, I can package a pre-release.

Boris



More information about the odb-users mailing list