2009-12-07 16:03:53 +01:00
|
|
|
#include "building.hh"
|
|
|
|
#include "area.hh"
|
2010-01-06 01:18:14 +01:00
|
|
|
#include "waypoint.hh"
|
2010-04-13 10:47:10 +02:00
|
|
|
#include "stock.hh"
|
2009-12-07 16:03:53 +01:00
|
|
|
|
|
|
|
|
2010-01-04 17:10:42 +01:00
|
|
|
using namespace std ;
|
2010-04-02 10:56:52 +02:00
|
|
|
using std::tr1::unordered_set ;
|
|
|
|
using std::tr1::unordered_map ;
|
2010-01-04 17:10:42 +01:00
|
|
|
|
|
|
|
|
2009-12-07 16:03:53 +01:00
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/* *** Constructors *** */
|
2009-12-07 16:03:53 +01:00
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/**
|
|
|
|
* Note: deletes (unallocates) all elements in #areas; deletes elements
|
|
|
|
* in #waypoints that belong only to this Building (waypoints that link
|
|
|
|
* several buildings are preserved).
|
2009-12-07 16:03:53 +01:00
|
|
|
*/
|
|
|
|
Building::~Building()
|
|
|
|
{
|
|
|
|
// Empty Area list
|
2010-04-02 10:56:52 +02:00
|
|
|
for (unordered_map<string, Area*>::const_iterator i = areas.begin() ;
|
2010-02-02 20:02:41 +01:00
|
|
|
i != areas.end() ; ++i)
|
2010-04-02 10:56:52 +02:00
|
|
|
delete i->second ;
|
2009-12-07 16:03:53 +01:00
|
|
|
areas.clear() ;
|
|
|
|
|
|
|
|
// Empty Waypoint list
|
2010-04-02 10:56:52 +02:00
|
|
|
for (unordered_set<Waypoint*>::const_iterator i = waypoints.begin() ;
|
2010-01-13 15:29:44 +01:00
|
|
|
i != waypoints.end() ; ++i)
|
2010-04-13 10:47:10 +02:00
|
|
|
Stock::waypoint_remove_building(**i, this) ;
|
2009-12-07 16:03:53 +01:00
|
|
|
waypoints.clear() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-02 10:56:52 +02:00
|
|
|
/* *** Write accessors *** */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-07-24 13:53:47 +02:00
|
|
|
* @param area A pointer to the Area to add. If it is NULL, nothing
|
2010-06-22 09:19:29 +02:00
|
|
|
* will be added. If the Area it points to already exist in #areas, it is
|
2010-04-02 10:56:52 +02:00
|
|
|
* 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) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/* *** Operators *** */
|
2009-12-07 16:03:53 +01:00
|
|
|
|
|
|
|
|
2011-07-30 20:38:42 +02:00
|
|
|
Building& Building::operator=(const Building &source)
|
2009-12-07 16:03:53 +01:00
|
|
|
{
|
2010-03-05 17:24:18 +01:00
|
|
|
if (this == &source)
|
2009-12-07 16:03:53 +01:00
|
|
|
return *this ;
|
|
|
|
|
2010-03-05 17:24:18 +01:00
|
|
|
name = source.name ;
|
|
|
|
areas = source.areas ;
|
|
|
|
waypoints = source.waypoints ;
|
2009-12-07 16:03:53 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-05 17:24:18 +01:00
|
|
|
bool Building::operator==(const Building &source) const
|
2009-12-07 16:03:53 +01:00
|
|
|
{
|
2010-03-05 17:24:18 +01:00
|
|
|
if (this == &source)
|
2009-12-07 16:03:53 +01:00
|
|
|
return true ;
|
|
|
|
|
|
|
|
return
|
2010-03-05 17:24:18 +01:00
|
|
|
name == source.name &&
|
|
|
|
areas == source.areas &&
|
|
|
|
waypoints == source.waypoints ;
|
2009-12-07 16:03:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-04 16:37:18 +01:00
|
|
|
ostream& operator<<(ostream &os, const Building &b)
|
2009-12-07 16:03:53 +01:00
|
|
|
{
|
2010-04-02 10:56:52 +02:00
|
|
|
os
|
|
|
|
<< b.name << " ("
|
|
|
|
<< b.areas.size() << " areas, "
|
|
|
|
<< b.waypoints.size() << " waypoints)" ;
|
2009-12-07 16:03:53 +01:00
|
|
|
|
|
|
|
return os ;
|
|
|
|
}
|