Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2004-01-03 08:19:12


Li Lirong wroite:

>I'm trying to use the serialization library to implement the undo/redo
>functionality. Basically, the idea is like this:
>
>[snip]
>
>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.)

The problem is that with serialization, you cannot garantee that the pointer
values will be the same on the read operation. This is because the read may
occur at any point in the future, long after the original object has been
destroyed. In this way, serialization is good at saving/restoring data
between application runs.

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

Would it not be better to use a list of objects like this:

class ObjectList: private std::list< std::auto_ptr< Object > >
{
   public:
      typedef std::list< std::auto_ptr< Object > > list_type;
      typedef typename list_type::iterator iterator;
   private:
      iterator i;
   public:
      inline ObjectList(): i( end())
      {
      }
   public:
      inline void add( Object * o ) // add an Object
      {
         if( i == end())
            push_back( o );
         else
            erase( i, end()); // clear undo/redo history after insert point
         i = end();
      }
      inline void undo()
      {
         --i; // go back an object
         update(); // refresh the changes
      }
      inline void undo( int n )
      {
         for( ; n > 0; --n ) --i; // undo n Objects
         update();
      }
      inline void redo()
      {
         if( i != end()) ++i;
         update();
      }
      inline void redo( int n )
      {
         for( ; ( n > 0 ) && ( i != end()); --n )
            ++i; // redo n changes (if possible!)
         update();
      }
      inline void update()
      {
         // call Object::update() for all active objects
         std::for_each( begin(), i, std::mem_ptr( &Object::update ));
      }
};

Regards,
Reece

_________________________________________________________________
Sign-up for a FREE BT Broadband connection today!
http://www.msn.co.uk/specials/btbroadband


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