Revision: 8233 http://playerstage.svn.sourceforge.net/playerstage/?rev=8233&view=rev Author: rtv Date: 2009-08-26 19:30:17 +0000 (Wed, 26 Aug 2009)
Log Message: ----------- more STLing and type safety to hopefully reduce bugs Modified Paths: -------------- code/stage/trunk/libstage/ancestor.cc code/stage/trunk/libstage/block.cc code/stage/trunk/libstage/canvas.cc code/stage/trunk/libstage/model.cc code/stage/trunk/libstage/model_draw.cc code/stage/trunk/libstage/model_getset.cc code/stage/trunk/libstage/region.cc code/stage/trunk/libstage/region.hh code/stage/trunk/libstage/stage.hh code/stage/trunk/libstage/world.cc code/stage/trunk/webstage/webstage.cc Modified: code/stage/trunk/libstage/ancestor.cc =================================================================== --- code/stage/trunk/libstage/ancestor.cc 2009-08-26 17:30:15 UTC (rev 8232) +++ code/stage/trunk/libstage/ancestor.cc 2009-08-26 19:30:17 UTC (rev 8233) @@ -20,8 +20,16 @@ void Ancestor::AddChild( Model* mod ) { + // if the child is already there, this is a serious error + if( std::find( children.begin(), children.end(), mod ) != children.end() ) + { + PRINT_ERR2( "Attempting to add child %s to %s - child already exists", + mod->Token(), this->Token() ); + exit( -1 ); + } + // poke a name into the child - char* buf = new char[TOKEN_MAX]; + static char* buf = new char[TOKEN_MAX]; // allocated once // printf( "adding child of type %d token %s\n", mod->type, mod->Token() ); @@ -41,18 +49,17 @@ mod->SetToken( buf ); - children.insert( mod ); + children.push_back( mod ); child_type_counts[mod->type]++; - delete[] buf; + //delete[] buf; // no need to free the statically allocated buffer } void Ancestor::RemoveChild( Model* mod ) { child_type_counts[mod->type]--; - - children.erase( mod );//std::remove( children.begin(), children.end(), mod ) ); + EraseAll( mod, children ); } Pose Ancestor::GetGlobalPose() Modified: code/stage/trunk/libstage/block.cc =================================================================== --- code/stage/trunk/libstage/block.cc 2009-08-26 17:30:15 UTC (rev 8232) +++ code/stage/trunk/libstage/block.cc 2009-08-26 19:30:17 UTC (rev 8233) @@ -206,13 +206,30 @@ void Block::RemoveFromCellArray( CellPtrVec *cells ) { FOR_EACH( it, *cells ) - (*it)->RemoveBlock( this); + { + Cell* cell = *it; + + // remove me from the cell + EraseAll( this, cell->blocks ); + --cell->region->count; + --cell->region->superregion->count; + } } void Block::AddToCellArray( CellPtrVec *cells ) { FOR_EACH( it, *cells ) - (*it)->AddBlock( this); + { + Cell* cell = *it; + + // record that I am rendered in this cell + rendered_cells->push_back( cell ); + + // store me in the cell + cell->blocks.push_back( this ); + ++cell->region->count; + ++cell->region->superregion->count; + } } void Block::SwitchToTestedCells() Modified: code/stage/trunk/libstage/canvas.cc =================================================================== --- code/stage/trunk/libstage/canvas.cc 2009-08-26 17:30:15 UTC (rev 8232) +++ code/stage/trunk/libstage/canvas.cc 2009-08-26 19:30:17 UTC (rev 8233) @@ -304,7 +304,7 @@ { if( mod ) { - selected_models.erase( std::remove( selected_models.begin(), selected_models.end(), mod )); + EraseAll( mod, selected_models ); redraw(); } } @@ -577,7 +577,7 @@ void Canvas::RemoveModel( Model* mod ) { printf( "removing model %s from canvas list\n", mod->Token() ); - models_sorted.erase( std::remove( models_sorted.begin(), models_sorted.end(), mod )); + EraseAll( mod, models_sorted ); } void Canvas::DrawGlobalGrid() Modified: code/stage/trunk/libstage/model.cc =================================================================== --- code/stage/trunk/libstage/model.cc 2009-08-26 17:30:15 UTC (rev 8232) +++ code/stage/trunk/libstage/model.cc 2009-08-26 19:30:17 UTC (rev 8233) @@ -277,8 +277,8 @@ // remove myself from my parent's child list, or the world's child // list if I have no parent - ModelPtrSet& vec = parent ? parent->children : world->children; - vec.erase( this );//std::remove( vec.begin(), vec.end(), this )); + ModelPtrVec& vec = parent ? parent->children : world->children; + EraseAll( this, vec ); modelsbyid.erase(id); @@ -332,7 +332,7 @@ { if( flag ) { - flag_list.erase( remove( flag_list.begin(), flag_list.end(), flag )); + EraseAll( flag, flag_list ); CallCallbacks( &hooks.flag_decr ); } } Modified: code/stage/trunk/libstage/model_draw.cc =================================================================== --- code/stage/trunk/libstage/model_draw.cc 2009-08-26 17:30:15 UTC (rev 8232) +++ code/stage/trunk/libstage/model_draw.cc 2009-08-26 19:30:17 UTC (rev 8233) @@ -246,28 +246,27 @@ if( ! world_gui ) return; - //save visual instance - //cv_list = g_list_append(cv_list, cv ); + //save visual instance cv_list.push_back( cv ); - - //register option for all instances which share the same name - Canvas* canvas = world_gui->GetCanvas(); - std::map< std::string, Option* >::iterator i = canvas->_custom_options.find( cv->GetMenuName() ); - if( i == canvas->_custom_options.end() ) { - Option* op = new Option( cv->GetMenuName(), - cv->GetWorldfileName(), - "", - on_by_default, - world_gui ); - canvas->_custom_options[ cv->GetMenuName() ] = op; - RegisterOption( op ); - } + + //register option for all instances which share the same name + Canvas* canvas = world_gui->GetCanvas(); + std::map< std::string, Option* >::iterator i = canvas->_custom_options.find( cv->GetMenuName() ); + if( i == canvas->_custom_options.end() ) { + Option* op = new Option( cv->GetMenuName(), + cv->GetWorldfileName(), + "", + on_by_default, + world_gui ); + canvas->_custom_options[ cv->GetMenuName() ] = op; + RegisterOption( op ); + } } void Model::RemoveVisualizer( Visualizer* cv ) { if( cv ) - cv_list.erase( remove( cv_list.begin(), cv_list.end(), cv )); + EraseAll( cv, cv_list ); //TODO unregister option - tricky because there might still be instances attached to different models which have the same name } Modified: code/stage/trunk/libstage/model_getset.cc =================================================================== --- code/stage/trunk/libstage/model_getset.cc 2009-08-26 17:30:15 UTC (rev 8232) +++ code/stage/trunk/libstage/model_getset.cc 2009-08-26 19:30:17 UTC (rev 8233) @@ -49,9 +49,9 @@ // non-zero values mean we need to be in the world's set of // detectable models if( val == 0 ) - world->models_with_fiducials.erase( this ); + world->FiducialErase( this ); else - world->models_with_fiducials.insert( this ); + world->FiducialInsert( this ); CallCallbacks( &vis.fiducial_return ); } @@ -135,17 +135,13 @@ } int Model::SetParent( Model* newparent) -{ - +{ // remove the model from its old parent (if it has one) if( parent ) - //this->parent->children = g_list_remove( this->parent->children, this ); - //parent->children.erase( remove( parent->children.begin(), parent->children.end(), this ) ); - parent->children.erase( this ); - + EraseAll( this, parent->children ); + if( newparent ) - //newparent->children = g_list_append( newparent->children, this ); - newparent->children.insert( this ); + newparent->children.push_back( this ); // link from the model to its new parent this->parent = newparent; Modified: code/stage/trunk/libstage/region.cc =================================================================== --- code/stage/trunk/libstage/region.cc 2009-08-26 17:30:15 UTC (rev 8232) +++ code/stage/trunk/libstage/region.cc 2009-08-26 19:30:17 UTC (rev 8233) @@ -11,26 +11,25 @@ // collection // std::set<Region*> Region::empty_regions; -Region::Region() : - cells( NULL ), +Region::Region( SuperRegion* sr) : + cells(), + superregion(sr), count(0) { } Region::~Region() { - if( cells ) - delete[] cells; } SuperRegion::SuperRegion( World* world, stg_point_int_t origin ) - : regions( new Region[ SUPERREGIONSIZE ] ), + : regions(), origin(origin), world(world), count(0) { - for( int i=0; i<SUPERREGIONSIZE; i++ ) - regions[i].superregion = this; + // populate the regions + regions.insert( regions.begin(), SUPERREGIONSIZE, Region( this ) ); //static int srcount=0; //printf( "created SR number %d\n", ++srcount ); @@ -39,9 +38,6 @@ SuperRegion::~SuperRegion() { - if( regions ) - delete[] regions; - //printf( "deleting SR %p at [%d,%d]\n", this, origin.x, origin.y ); } @@ -64,12 +60,12 @@ for( int y=0; y<SUPERREGIONWIDTH; y++ ) { const Region* r = GetRegion(x,y); - + if( r->count ) // outline regions with contents glRecti( x<<RBITS, y<<RBITS, (x+1)<<RBITS, (y+1)<<RBITS ); - else if( r->cells ) + else if( ! r->cells.empty() ) { double left = x << RBITS; double right = (x+1) << RBITS; @@ -81,35 +77,22 @@ // draw little corner markers for regions with memory // allocated but no contents glBegin( GL_LINES ); - glVertex2f( left, bottom ); glVertex2f( left+d, bottom ); - glVertex2f( left, bottom ); glVertex2f( left, bottom+d ); - - glVertex2f( left, top ); glVertex2f( left+d, top ); - glVertex2f( left, top ); glVertex2f( left, top-d ); - - - glVertex2f( right, top ); glVertex2f( right-d, top ); - glVertex2f( right, top ); glVertex2f( right, top-d ); - - glVertex2f( right, bottom ); glVertex2f( right-d, bottom ); - glVertex2f( right, bottom ); glVertex2f( right, bottom+d ); - glEnd(); } } Modified: code/stage/trunk/libstage/region.hh =================================================================== --- code/stage/trunk/libstage/region.hh 2009-08-26 17:30:15 UTC (rev 8232) +++ code/stage/trunk/libstage/region.hh 2009-08-26 19:30:17 UTC (rev 8233) @@ -45,12 +45,10 @@ private: Region* region; std::vector<Block*> blocks; - //std::set<Block*> blocks; - bool boundary; public: - Cell() - : region( NULL), + Cell( Region* reg ) + : region( reg ), blocks() { } @@ -63,26 +61,22 @@ class Region { public: - - Cell* cells; + std::vector<Cell> cells; + SuperRegion* superregion; unsigned long count; // number of blocks rendered into this region - Region(); + Region( SuperRegion* sr ); ~Region(); Cell* GetCell( int32_t x, int32_t y ) { - if( ! cells ) - { - cells = new Cell[REGIONSIZE]; - - for( int i=0; i<REGIONSIZE; ++i ) - cells[i].region = this; - } - + if( cells.empty() ) // lazy population of cells + cells.insert( cells.begin(), REGIONSIZE, Cell( this ) ); + return( (Cell*)&cells[ x + y * REGIONWIDTH ] ); } + }; // end class Region class SuperRegion @@ -92,7 +86,7 @@ private: - Region* regions; + std::vector<Region> regions; stg_point_int_t origin; World* world; @@ -110,24 +104,4 @@ unsigned long count; // number of blocks rendered into this superregion }; // class SuperRegion; - void Cell::RemoveBlock( Block* b ) - { - // linear time removal, but these vectors are very short, usually 1 - // or 2 elements. - blocks.erase( std::remove( blocks.begin(), blocks.end(), b ), blocks.end() ); - - --region->count; - --region->superregion->count; - } - - void Cell::AddBlock( Block* b ) - { - blocks.push_back( b ); - b->RecordRendering( this ); - - ++region->count; - ++region->superregion->count; - } - - }; // namespace Stg Modified: code/stage/trunk/libstage/stage.hh =================================================================== --- code/stage/trunk/libstage/stage.hh 2009-08-26 17:30:15 UTC (rev 8232) +++ code/stage/trunk/libstage/stage.hh 2009-08-26 19:30:17 UTC (rev 8233) @@ -89,6 +89,9 @@ /** Set of pointers to Models. */ typedef std::set<Model*> ModelPtrSet; + /** Set of pointers to Models. */ + typedef std::vector<Model*> ModelPtrVec; + /** Set of pointers to Blocks. */ typedef std::set<Block*> BlockPtrSet; @@ -615,6 +618,11 @@ #define VAR(V,init) __typeof(init) V=(init) #define FOR_EACH(I,C) for(VAR(I,(C).begin());I!=(C).end();I++) +/** wrapper for Erase-Remove method of removing all instances of thing from container */ + template <class T, class C> + void EraseAll( T thing, C& cont ) + { cont.erase( std::remove( cont.begin(), cont.end(), thing ), cont.end() ); } + // Error macros - output goes to stderr #define PRINT_ERR(m) fprintf( stderr, "\033[41merr\033[0m: "m" (%s %s)\n", __FILE__, __FUNCTION__) #define PRINT_ERR1(m,a) fprintf( stderr, "\033[41merr\033[0m: "m" (%s %s)\n", a, __FILE__, __FUNCTION__) @@ -679,7 +687,7 @@ friend class Canvas; // allow Canvas access to our private members protected: - ModelPtrSet children; + ModelPtrVec children; bool debug; char* token; pthread_mutex_t access_mutex; ///< Used by Lock() and Unlock() to prevent parallel access to this model @@ -690,7 +698,7 @@ public: /** get the children of the this element */ - ModelPtrSet& GetChildren(){ return children;} + ModelPtrVec& GetChildren(){ return children;} /** recursively call func( model, arg ) for each descendant */ void ForEachDescendant( stg_model_callback_t func, void* arg ); @@ -812,8 +820,22 @@ /** Keep a list of all models with detectable fiducials. This avoids searching the whole world for fiducials. */ - ModelPtrSet models_with_fiducials; - + ModelPtrVec models_with_fiducials; + + /** Add a model to the set of models with non-zero fiducials, if not already there. */ + void FiducialInsert( Model* mod ) + { + FiducialErase( mod ); // make sure it's not there already + models_with_fiducials.push_back( mod ); + } + + /** Remove a model from the set of models with non-zero fiducials, if it exists. */ + void FiducialErase( Model* mod ) + { + //EraseAll<Model*,ModelPtrVec&>( mod, models_with_fiducials ); + EraseAll( mod, models_with_fiducials ); + } + double ppm; ///< the resolution of the world model in pixels per meter bool quit; ///< quit this world ASAP @@ -842,7 +864,7 @@ std::map<stg_point_int_t,SuperRegion*> superregions; SuperRegion* sr_cached; ///< The last superregion looked up by this world - std::vector<ModelPtrSet> update_lists; + std::vector<ModelPtrVec> update_lists; uint64_t updates; ///< the number of simulated time steps executed so far Worldfile* wf; ///< If set, points to the worldfile used to create this world @@ -1111,9 +1133,6 @@ /** Set the extent in Z of the block */ void SetZ( double min, double max ); - void RecordRendering( Cell* cell ) - { rendered_cells->push_back( cell ); } - stg_point_t* Points( unsigned int *count ) { if( count ) *count = pt_count; return &pts[0]; }; Modified: code/stage/trunk/libstage/world.cc =================================================================== --- code/stage/trunk/libstage/world.cc 2009-08-26 17:30:15 UTC (rev 8232) +++ code/stage/trunk/libstage/world.cc 2009-08-26 19:30:17 UTC (rev 8233) @@ -1024,7 +1024,7 @@ void World::RemovePowerPack( PowerPack* pp ) { - powerpack_list.erase( remove( powerpack_list.begin(), powerpack_list.end(), pp )); + EraseAll( pp, powerpack_list ); } /// Register an Option for pickup by the GUI Modified: code/stage/trunk/webstage/webstage.cc =================================================================== --- code/stage/trunk/webstage/webstage.cc 2009-08-26 17:30:15 UTC (rev 8232) +++ code/stage/trunk/webstage/webstage.cc 2009-08-26 19:30:17 UTC (rev 8233) @@ -137,7 +137,7 @@ virtual bool GetModelChildren(const std::string& model, std::vector<std::string>& children) { - std::set<Model*> c; + std::vector<Model*> c; if(model == "") { @@ -157,9 +157,7 @@ } - for( std::set<Model*>::iterator it = c.begin(); - it != c.end(); - it++ ) + FOR_EACH( it, c ) { children.push_back(std::string((*it)->Token())); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ Playerstage-commit mailing list Playerstage-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/playerstage-commit