From c748c34cb6079af58a6e4bd2e5b7e53e456414e8 Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Sat, 1 Mar 2008 01:31:06 -0600 Subject: [PATCH] Properly maintain unique ID accross undo of deletion. --- Loggable.C | 103 +++++++++++++++++++++++++++++++++++++++++++++++--- Loggable.H | 19 +++++++++- Region.H | 2 - Tempo_Point.H | 2 - Time_Point.H | 2 - 5 files changed, 115 insertions(+), 13 deletions(-) diff --git a/Loggable.C b/Loggable.C index 876a145..2c4176d 100644 --- a/Loggable.C +++ b/Loggable.C @@ -106,6 +106,68 @@ void free_sa ( char **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 Loggable::undo ( void ) @@ -173,11 +235,11 @@ Loggable::undo ( void ) int ui = _undo_index; - if ( ! l ) - { - printf( "corrupt undo?\n" ); - abort(); - } +/* if ( ! l ) */ +/* { */ +/* printf( "corrupt undo?\n" ); */ +/* abort(); */ +/* } */ @@ -188,7 +250,12 @@ Loggable::undo ( void ) if ( ! _class_map[ string( classname ) ] ) printf( "error class %s is unregistered!\n", classname ); else - _class_map[ string( classname ) ]( sa ); + { + /* create */ + Loggable *l = _class_map[ string( classname ) ]( sa ); + l->update_id( id ); + l->log_create(); + } } else if ( ! strcmp( command, "set" ) ) @@ -220,6 +287,30 @@ Loggable::undo ( void ) 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 Loggable::log ( const char *fmt, ... ) diff --git a/Loggable.H b/Loggable.H index 91b7d92..b2fff02 100644 --- a/Loggable.H +++ b/Loggable.H @@ -75,6 +75,7 @@ public: static bool open ( const char *filename ); static void undo ( void ); static int undo_index ( void ) { return _undo_index; } + static bool snapshot( const char *file ); static void @@ -112,11 +113,26 @@ public: _loggables.push_back( this ); } - virtual ~Loggable ( ) + /** 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 ( ) + { + _loggables[ _id - 1 ] = NULL; } static @@ -148,6 +164,7 @@ public: int id ( void ) { return _id; } + friend class Logger; }; diff --git a/Region.H b/Region.H index a6c5a08..2dbf6f2 100644 --- a/Region.H +++ b/Region.H @@ -156,8 +156,6 @@ public: r->set( sa ); - r->log_create(); - return (Loggable *)r; } diff --git a/Tempo_Point.H b/Tempo_Point.H index 773c15f..4207be9 100644 --- a/Tempo_Point.H +++ b/Tempo_Point.H @@ -111,8 +111,6 @@ public: r->set( sa ); - r->log_create(); - return (Loggable *)r; } diff --git a/Time_Point.H b/Time_Point.H index 572fa89..f905e99 100644 --- a/Time_Point.H +++ b/Time_Point.H @@ -130,8 +130,6 @@ public: r->set( sa ); - r->log_create(); - return (Loggable *)r; }