owlps/owlps-positioner/building.cc

124 lines
2.8 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS) project.
* It is subject to the copyright notice and license terms in the
* COPYRIGHT.t2t file found in the top-level directory of this
* distribution and at
* http://code.lm7.fr/p/owlps/source/tree/master/COPYRIGHT.t2t
* No part of the OwlPS Project, including this file, may be copied,
* modified, propagated, or distributed except according to the terms
* contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
* distributed along with this file, either separately or by replacing
* this notice by the COPYRIGHT.t2t file's contents.
*/
#include "building.hh"
#include "area.hh"
#include "waypoint.hh"
#include "stock.hh"
using namespace std ;
/* *** 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[in,out] area A pointer to the Area to add. If it is NULL,
* nothing will be added. If the Area it points to already exists in
* #areas, it is deleted and set to NULL, 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 ;
}