Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2004-01-06 01:21:35

Pavel Vozenilek wrote:

>Hello Robert,

>I have feature request :

>- often objects are created by object factories, instead
> of simple "new". Creating objects by "new" may not be
> option because of infrastructure/special location of object/
> object may be singleton/etc.

>Is it somehow possible to add support for factories into serialization?

>The saving won't change but loading would invoke factory method
>and this would allocate memory and invoke constructor.

This used to be possible but in the last iteration I was became convinced
that changing the override interface would have some advantages and it did.
Unfortunately, this issue didn't occur to me at the time so the
capability was lost. Also lost was the ability to implement a
system for plug - ins.

Oh well.


>Somehow related: what if I have factory wich creates objects identified
>by some other value. Say:

>struct color { int r, g, b; };

>#define RED 1
>#define CYAN 2
>#define BLACK 3

>struct color_factory {
> color* create(int color_id) { ... }

>When serializing I would like to save only color ID instead of all object
>content and when deserializing I would like to create object from ID
>by calling factory.


Anything wrong with this?

color color_from_id(int id);
int id_from_color(color);

template<class Archive>
void save(Archive & ar, color c){
        ar << id_from_color(c);

template<class Archive>
void load(Archive & ar, color & c){
        int id;
        ar >> id
        c = color_from_id(id);

Boost list run by bdawes at, gregod at, cpdaniel at, john at