owlps/owlps-positioning/src/building.cc

118 lines
2.4 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS).
* OwlPS is a project of the University of Franche-Comté
* (Université de Franche-Comté), France.
*/
#include "building.hh"
#include "area.hh"
#include "waypoint.hh"
#include "stock.hh"
using namespace std ;
using std::tr1::unordered_set ;
using std::tr1::unordered_map ;
/* *** Constructors *** */
/**
* Note: deletes (unallocates) all elements in #areas; deletes elements
* in #waypoints that belong only to this Building (waypoints that link
* several buildings are preserved).
*/
Building::~Building()
{
// Empty Area list
for (unordered_map<string, Area*>::const_iterator i = areas.begin() ;
i != areas.end() ; ++i)
delete i->second ;
areas.clear() ;
// Empty Waypoint list
for (unordered_set<Waypoint*>::const_iterator i = waypoints.begin() ;
i != waypoints.end() ; ++i)
Stock::waypoint_remove_building(**i, this) ;
waypoints.clear() ;
}
/* *** Write accessors *** */
/**
* @param area A pointer to the Area to add. If it is NULL, nothing
* will be added. If the Area it points to already exist in #areas, it is
* deleted and nullified, unless it is the same pointer (see the code to
* understand!); hmm, maybe we should handle that with exceptions…
*/
void Building::add_area(Area *&area)
{
if (area == NULL)
return ;
string area_name = area->get_name() ;
// Check if the area already exist in areas
unordered_map<string, Area*>::const_iterator i =
areas.find(area_name) ;
if (i != areas.end())
{
// Do not delete if area points to the already stored Area
if (i->second != area)
{
delete area ;
area = NULL ;
}
return ;
}
areas[area_name] = const_cast<Area*>(area) ;
}
/* *** Operators *** */
Building& Building::operator=(const Building &source)
{
if (this == &source)
return *this ;
name = source.name ;
areas = source.areas ;
waypoints = source.waypoints ;
return *this;
}
bool Building::operator==(const Building &source) const
{
if (this == &source)
return true ;
return
name == source.name &&
areas == source.areas &&
waypoints == source.waypoints ;
}
ostream& operator<<(ostream &os, const Building &b)
{
os
<< b.name << " ("
<< b.areas.size() << " areas, "
<< b.waypoints.size() << " waypoints)" ;
return os ;
}