Permalink
Browse files

more STLing and type safety to hopefully reduce bugs

  • Loading branch information...
1 parent 31b9f4b commit 64249e4cc8e14a1effc7dc1e82e5c47deb747bef rtv committed Aug 26, 2009
Showing with 105 additions and 112 deletions.
  1. +12 −5 libstage/ancestor.cc
  2. +19 −2 libstage/block.cc
  3. +2 −2 libstage/canvas.cc
  4. +3 −3 libstage/model.cc
  5. +15 −16 libstage/model_draw.cc
  6. +6 −10 libstage/model_getset.cc
  7. +8 −25 libstage/region.cc
  8. +10 −36 libstage/region.hh
  9. +27 −8 libstage/stage.hh
  10. +1 −1 libstage/world.cc
  11. +2 −4 webstage/webstage.cc
View
@@ -20,8 +20,16 @@ Ancestor::~Ancestor()
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 @@ void Ancestor::AddChild( Model* mod )
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()
View
@@ -206,13 +206,30 @@ void Block::UnMap()
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()
View
@@ -304,7 +304,7 @@ void Canvas::unSelect( Model* mod )
{
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::AddModel( Model* mod )
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()
View
@@ -277,8 +277,8 @@ Model::~Model( void )
// 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 @@ void Model::RemoveFlag( Flag* flag )
{
if( flag )
{
- flag_list.erase( remove( flag_list.begin(), flag_list.end(), flag ));
+ EraseAll( flag, flag_list );
CallCallbacks( &hooks.flag_decr );
}
}
View
@@ -246,28 +246,27 @@ void Model::AddVisualizer( Visualizer* cv, bool on_by_default )
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
}
View
@@ -49,9 +49,9 @@ void Model::SetFiducialReturn( int val )
// 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 @@ void Model::SetGlobalPose( const Pose& gpose )
}
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;
View
@@ -11,26 +11,25 @@ using namespace Stg;
// 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( World* world, stg_point_int_t origin )
SuperRegion::~SuperRegion()
{
- if( regions )
- delete[] regions;
-
//printf( "deleting SR %p at [%d,%d]\n", this, origin.x, origin.y );
}
@@ -64,12 +60,12 @@ void SuperRegion::Draw( bool drawall )
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 @@ void SuperRegion::Draw( bool drawall )
// 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();
}
}
View
@@ -45,12 +45,10 @@ namespace Stg
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 @@ namespace Stg
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 @@ namespace Stg
private:
- Region* regions;
+ std::vector<Region> regions;
stg_point_int_t origin;
World* world;
@@ -110,24 +104,4 @@ namespace Stg
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
Oops, something went wrong.

0 comments on commit 64249e4

Please sign in to comment.