Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2004-01-03 11:42:51

Li Lirong wrote:

>Im trying to use the serialization library to implement the undo/redo
>functionality. Basically, the idea is like this:

>Object* pObject;
>redo_archive ar;

>// create the object...

>// before modify the object, save it first.
>ar << pObject;

>// modify the object...

>// undo the modification from the archive
>ar >> pObject;

>The problem is that after the undo, pObject will point to a newly
>created object instead of the originally. (The same applies some pointer
>fields of the object.)

>Any quick solution? (Maybe, when loading a pointer, the library will not
>create a new object if the pointer is not equal to 0?)


A clever idea I had never thought of.

The easiest thing is to use:

ar >> * pObject
ar << *pObject

which won't create any new objects but just serialize to the existing ones.
This might be all you need if all pointer objects aren't created after
the initial class construction.

On the other hand, this could make would create problems for
serialization for saving and loading files.

Another idea might be to create new type of archive called a
"memento archive" (name lifted from Design Patterns book)

//code sketch

template<class BaseArchive>
class memento_oarchive : public BaseArchive<memento_oarchive>
    template<class T>
    struct pointer_type {
        static void invoke(const T & t){
            BaseArchive::operator<<(* t),
    struct non_pointer_type {
        static void invoke(const T & t){
    // the << operator
    template<class T>
    memento_oarchive & operator<<(const T & t){
        return * this;

Note that this depends upon the ability to derive from an existing
archive. This capability only exists in revision #14 which is not
yet uploaded. I am waiting for the "official release" of 1.31
I will then retest and upload - and submit for revew.

Robert Ramey


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