[Positioner] Rework Building::add_area()

Don't take a pointer reference as argument, throw an exception instead
of deleting the area, clarify comments.
This commit is contained in:
Matteo Cypriani 2013-10-02 17:33:53 -04:00
parent 939ef17d6a
commit 0c79e230b9
3 changed files with 26 additions and 15 deletions

View File

@ -16,6 +16,7 @@
#include "area.hh" #include "area.hh"
#include "waypoint.hh" #include "waypoint.hh"
#include "stock.hh" #include "stock.hh"
#include "posexcept.hh"
using namespace std ; using namespace std ;
@ -49,13 +50,16 @@ Building::~Building()
/** /**
* @param[in,out] area A pointer to the Area to add. If it is NULL, * If `area` is NULL, nothing is done.
* nothing will be added. If the Area it points to already exists in * If `area` is a pointer to an Area that is already stored in #areas,
* #areas, it is deleted and set to NULL, unless it is the same pointer * nothing is done.
* (see the code to understand!); hmm, maybe we should handle that with * If `area` has the same name as (but is not identical to) an Area that
* exceptions * is already stored in #areas, an exception is thrown.
*
* @param area A pointer to the Area to add.
* @throws element_already_exists
*/ */
void Building::add_area(Area *&area) void Building::add_area(const Area *const area)
{ {
if (area == NULL) if (area == NULL)
return ; return ;
@ -66,15 +70,15 @@ void Building::add_area(Area *&area)
auto i = areas.find(area_name) ; auto i = areas.find(area_name) ;
if (i != areas.end()) if (i != areas.end())
{ {
// Do not delete if area points to the already stored Area // Just return if area points to the already stored Area
if (i->second != area) if (i->second == area)
{ return ;
delete area ; // Otherwise throw an exception
area = NULL ; throw element_already_exists(
} "an Area named \""+ area_name +"\" is already stored") ;
return ;
} }
// Store the area
areas[area_name] = area ; areas[area_name] = area ;
} }

View File

@ -55,7 +55,7 @@ public:
//@{ //@{
void set_name(const std::string &_name) ; void set_name(const std::string &_name) ;
/// Adds an Area to the [list of areas](@ref #areas) /// Adds an Area to the [list of areas](@ref #areas)
void add_area(Area *&area) ; void add_area(const Area *const area) ;
/// Adds a Waypoint to the [list of waypoints](@ref #waypoints) /// Adds a Waypoint to the [list of waypoints](@ref #waypoints)
void add_waypoint(Waypoint *const wp) ; void add_waypoint(Waypoint *const wp) ;
//@} //@}

View File

@ -68,7 +68,14 @@ void TopologyReaderCSV::process_area_line()
throw malformed_input_data("Cannot read area max coordinates!") ; throw malformed_input_data("Cannot read area max coordinates!") ;
Area *area = new Area(&building, name, p_min, p_max) ; Area *area = new Area(&building, name, p_min, p_max) ;
building.add_area(area) ; try
{
building.add_area(area) ;
}
catch (element_already_exists e)
{
delete area ;
}
} }