Boost logo

Boost :

From: Alexander Nasonov (alnsn-mycop_at_[hidden])
Date: 2003-06-20 04:51:46


Below is a copy of my post to comp.lang.c++.moderated
http://groups.google.co.uk/groups?q=author:alnsn-mycop%40yandex.ru&hl=en&lr=&ie=UTF-8&selm=3eef18d6%40news.fhg.de&rnum=1

--- cut ---

Thomas Hansen wrote:
> BTW!
> Serialization of objects in C++ or any other language for that reason
> is one of the "hardest" part to keep up with your "OOP" mantras...
> The reason is that all that nice "polymorphisme" and your beautifule
> "inheritence tree" basically becomes "pure C" since you'll end up with
> a file stuffed with "enums" and "magic numbers" anyway...
> No man alive today has been able to solve this problem completely...

I took some ideas from boost archive and played around with a code several
days ago.

The idea is to set relations between pointers to data members (or to get/set
member functions) and names of serialized fields.
Then, during object loading/saving it's possible to access the member using
member pointer. The framework will find appropriate field in the storage by
name and static_cast it automatically. So, you don't need to make those
tricky casts.
Realtions can be built using describe function:

// Parent.hpp
struct Parent : Person
{
    Parent* partner;
    std::list<Person*> children;

   static void describe(db::type<Parent>& t)
   {
        // Looks like class definition
        t & db::class_("Parent")
          & db::derived_from<Person>()
          & db::field(&Parent::partner, "partner")
          & db::field(&Parent::children, "children");
   }
};

It should be registered in Parent.cpp file using static object:

namespace {
  db::register_type<Parent,&Parent::describe> r;
}

Access to members is easy:

template<class Class, class T>
T db::get(T Class::* p, db::raw_object& obj);

db::raw_object father = find_my_father(parents_database);
Parent* mother = db::get(&Parent::partner, father);

In addition to better static type control you can track pointers or a list
of pointers at compile-time and then use it at runtime to set up the schema
and to load an object with all its relations.

--- cut ---

-- 
Alexander Nasonov
Remove minus and all between minus and at from my e-mail for timely response

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk