Properly maintain unique ID accross undo of deletion.

pull/3/head
Jonathan Moore Liles 2008-03-01 01:31:06 -06:00
parent 67f9b029bd
commit c748c34cb6
5 changed files with 117 additions and 15 deletions

View File

@ -106,6 +106,68 @@ void free_sa ( char **sa )
free( sa ); free( sa );
} }
/* void */
/* Loggable::redo ( const char *s ) */
/* { */
/* int id; */
/* sscanf( s, "%*s %X ", &id ); */
/* Loggable *l = find( id ); */
/* // assert( l ); */
/* char classname[40]; */
/* char command[40]; */
/* char *arguments; */
/* sscanf( s, "%s %*X %s %*[^\n<] << %a[^\n]", classname, command, &arguments ); */
/* int ui = _undo_index; */
/* if ( ! l ) */
/* { */
/* printf( "corrupt undo?\n" ); */
/* abort(); */
/* } */
/* if ( ! strcmp( command, "destroy" ) ) */
/* { */
/* char **sa = parse_alist( arguments ); */
/* if ( ! _class_map[ string( classname ) ] ) */
/* printf( "error class %s is unregistered!\n", classname ); */
/* else */
/* { */
/* /\* create *\/ */
/* Loggable *l = _class_map[ string( classname ) ]( sa ); */
/* l->update_id( id ); */
/* } */
/* } */
/* else */
/* if ( ! strcmp( command, "set" ) ) */
/* { */
/* printf( "got set command.\n" ); */
/* char **sa = parse_alist( arguments ); */
/* l->log_start(); */
/* l->set( sa ); */
/* l->log_end(); */
/* } */
/* else */
/* if ( ! strcmp( command, "create" ) ) */
/* { */
/* int id = l->id(); */
/* delete l; */
/* _loggables[ id ] = NULL; */
/* } */
/* } */
void void
Loggable::undo ( void ) Loggable::undo ( void )
@ -173,11 +235,11 @@ Loggable::undo ( void )
int ui = _undo_index; int ui = _undo_index;
if ( ! l ) /* if ( ! l ) */
{ /* { */
printf( "corrupt undo?\n" ); /* printf( "corrupt undo?\n" ); */
abort(); /* abort(); */
} /* } */
@ -188,7 +250,12 @@ Loggable::undo ( void )
if ( ! _class_map[ string( classname ) ] ) if ( ! _class_map[ string( classname ) ] )
printf( "error class %s is unregistered!\n", classname ); printf( "error class %s is unregistered!\n", classname );
else else
_class_map[ string( classname ) ]( sa ); {
/* create */
Loggable *l = _class_map[ string( classname ) ]( sa );
l->update_id( id );
l->log_create();
}
} }
else else
if ( ! strcmp( command, "set" ) ) if ( ! strcmp( command, "set" ) )
@ -220,6 +287,30 @@ Loggable::undo ( void )
delete buf; delete buf;
} }
/** write a snapshot of the state of all loggable objects, sufficient
* for later reconstruction, to /file/ */
bool
Loggable::snapshot( const char *file )
{
FILE *ofp = _fp;
if ( ! ( _fp = fopen( file, "a" ) ) )
{
_fp = ofp;
return false;
}
for ( int i = 0; i < _log_id; ++i )
{
_loggables[ i ]->log_create();
}
fclose( _fp );
_fp = ofp;
return true;
}
void void
Loggable::log ( const char *fmt, ... ) Loggable::log ( const char *fmt, ... )

View File

@ -75,6 +75,7 @@ public:
static bool open ( const char *filename ); static bool open ( const char *filename );
static void undo ( void ); static void undo ( void );
static int undo_index ( void ) { return _undo_index; } static int undo_index ( void ) { return _undo_index; }
static bool snapshot( const char *file );
static static
void void
@ -112,11 +113,26 @@ public:
_loggables.push_back( this ); _loggables.push_back( this );
} }
/** must be called after construction in create() methods */
void
update_id ( int id )
{
assert( _id == _log_id );
_loggables[ _id - 1 ] = NULL;
--_log_id;
_id = id;
assert( ! _loggables[ _id - 1 ] );
_loggables[ _id - 1 ] = this;
}
virtual ~Loggable ( ) virtual ~Loggable ( )
{ {
_loggables[ _id - 1 ] = NULL;
} }
static static
@ -148,6 +164,7 @@ public:
int id ( void ) { return _id; } int id ( void ) { return _id; }
friend class Logger; friend class Logger;
}; };

View File

@ -156,8 +156,6 @@ public:
r->set( sa ); r->set( sa );
r->log_create();
return (Loggable *)r; return (Loggable *)r;
} }

View File

@ -111,8 +111,6 @@ public:
r->set( sa ); r->set( sa );
r->log_create();
return (Loggable *)r; return (Loggable *)r;
} }

View File

@ -130,8 +130,6 @@ public:
r->set( sa ); r->set( sa );
r->log_create();
return (Loggable *)r; return (Loggable *)r;
} }