2010-01-12 00:31:22 +01:00
|
|
|
#include "stock.hh"
|
2010-02-11 18:33:45 +01:00
|
|
|
#include "posexcept.hh"
|
2010-01-16 12:23:07 +01:00
|
|
|
|
2010-01-12 00:31:22 +01:00
|
|
|
using namespace std ;
|
|
|
|
using std::tr1::unordered_map ;
|
2010-02-19 11:42:04 +01:00
|
|
|
using std::tr1::unordered_set ;
|
2010-01-12 00:31:22 +01:00
|
|
|
|
|
|
|
|
2010-04-13 10:47:10 +02:00
|
|
|
/* *** Hash functions, required to put points in a set *** */
|
|
|
|
|
|
|
|
inline size_t hash_value(const ReferencePoint &object)
|
|
|
|
{
|
|
|
|
return boost::hash_value(static_cast<string>(object)) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t hash_value(const Point3D &object)
|
2010-02-19 11:42:04 +01:00
|
|
|
{
|
|
|
|
return boost::hash_value(static_cast<string>(object)) ;
|
|
|
|
}
|
|
|
|
|
2010-01-12 00:31:22 +01:00
|
|
|
|
2010-04-13 10:47:10 +02:00
|
|
|
|
2010-02-11 09:46:40 +01:00
|
|
|
/* *** Attribute definitions *** */
|
2010-01-16 12:23:07 +01:00
|
|
|
|
2010-04-02 14:03:55 +02:00
|
|
|
unordered_map<string, Building> Stock::buildings ;
|
|
|
|
|
2010-04-13 10:47:10 +02:00
|
|
|
unordered_map<Point3D, Waypoint> Stock::waypoints ;
|
|
|
|
|
2010-02-11 09:46:40 +01:00
|
|
|
unordered_map<string, Mobile> Stock::mobiles ;
|
2010-01-12 00:31:22 +01:00
|
|
|
|
2010-02-11 09:46:40 +01:00
|
|
|
unordered_map<string, AccessPoint> Stock::aps ;
|
2010-01-16 12:23:07 +01:00
|
|
|
|
2010-02-19 11:42:04 +01:00
|
|
|
unordered_set<ReferencePoint> Stock::reference_points ;
|
|
|
|
|
|
|
|
unordered_set<CalibrationRequest> Stock::calibration_requests ;
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
|
|
|
|
|
2010-02-19 11:42:04 +01:00
|
|
|
/* *** Accessors *** */
|
2010-01-16 12:23:07 +01:00
|
|
|
|
|
|
|
|
2010-04-02 14:03:55 +02:00
|
|
|
/**
|
|
|
|
* The name of the Building is initialised.
|
|
|
|
*/
|
|
|
|
const Building& Stock::find_create_building(const string &name)
|
|
|
|
{
|
|
|
|
unordered_map<string, Building>::const_iterator i =
|
|
|
|
buildings.find(name) ;
|
|
|
|
if (i != buildings.end())
|
|
|
|
return i->second ;
|
|
|
|
|
|
|
|
Building &building = buildings[name] ;
|
|
|
|
building.set_name(name) ;
|
|
|
|
return building ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param name The name of the Building to search for.
|
|
|
|
* It must be a valid name, as no check is performed.
|
|
|
|
* @return A const reference to the Building.
|
|
|
|
* @throw element_not_found is thrown if the Building corresponding
|
|
|
|
* to \em name does not exist.
|
|
|
|
*/
|
|
|
|
const Building& Stock::get_building(const string &name)
|
|
|
|
{
|
|
|
|
unordered_map<string, Building>::const_iterator i =
|
|
|
|
buildings.find(name) ;
|
|
|
|
if (i != buildings.end())
|
|
|
|
return i->second ;
|
|
|
|
throw element_not_found("No Building with name « " + name + " »!") ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-13 10:47:10 +02:00
|
|
|
const Waypoint& Stock::
|
|
|
|
find_create_waypoint(const Point3D &point)
|
|
|
|
{
|
|
|
|
unordered_map<Point3D, Waypoint>::const_iterator i =
|
|
|
|
waypoints.find(point) ;
|
|
|
|
if (i != waypoints.end())
|
|
|
|
return i->second ;
|
|
|
|
|
|
|
|
Waypoint &waypoint = waypoints[point] ;
|
|
|
|
waypoint.set_coordinates(point) ;
|
|
|
|
return waypoint ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Stock::waypoint_remove_building(const Waypoint &point,
|
|
|
|
const Building *building)
|
|
|
|
{
|
|
|
|
unordered_map<Point3D, Waypoint>::iterator i =
|
|
|
|
waypoints.find(point) ;
|
|
|
|
|
|
|
|
Waypoint *waypoint = const_cast<Waypoint*>(&i->second) ;
|
|
|
|
waypoint->remove_building(building) ;
|
|
|
|
|
|
|
|
// If the Waypoint is not linked to any Building any more, we
|
|
|
|
// delete it
|
|
|
|
if (waypoint->get_1st_building() == NULL)
|
|
|
|
waypoints.erase(i) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/**
|
|
|
|
* @param mac The MAC address of the Mobile to search for.
|
|
|
|
* It must be a valid MAC address, as no check is performed.
|
|
|
|
* @return A const reference to the Mobile.
|
2010-02-11 18:33:45 +01:00
|
|
|
* @throw element_not_found is thrown if the Mobile corresponding
|
2010-01-16 12:23:07 +01:00
|
|
|
* to \em mac does not exist.
|
|
|
|
*/
|
|
|
|
const Mobile& Stock::get_mobile(const string &mac)
|
|
|
|
{
|
2010-02-02 20:02:41 +01:00
|
|
|
unordered_map<string, Mobile>::const_iterator i = mobiles.find(mac) ;
|
2010-01-16 12:23:07 +01:00
|
|
|
if (i != mobiles.end())
|
|
|
|
return i->second ;
|
2010-04-02 14:03:55 +02:00
|
|
|
throw element_not_found("No Mobile with MAC address « " +
|
|
|
|
mac + " »!") ;
|
2010-01-16 12:23:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-01 11:03:24 +02:00
|
|
|
/**
|
|
|
|
* The MAC address of the Mobile is initialised.
|
|
|
|
*/
|
|
|
|
const Mobile& Stock::find_create_mobile(const string &mac)
|
|
|
|
{
|
|
|
|
unordered_map<string, Mobile>::const_iterator i = mobiles.find(mac) ;
|
|
|
|
if (i != mobiles.end())
|
|
|
|
return i->second ;
|
|
|
|
|
|
|
|
Mobile &mobile = mobiles[mac] ;
|
|
|
|
mobile.set_mac_addr(mac) ;
|
|
|
|
return mobile ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/**
|
|
|
|
* @param mac The MAC address of the AccessPoint to search for.
|
|
|
|
* It must be a valid MAC address, as no check is performed.
|
|
|
|
* @return A const reference to the AccessPoint.
|
2010-02-11 18:33:45 +01:00
|
|
|
* @throw element_not_found is thrown if the AccessPoint corresponding
|
2010-01-16 12:23:07 +01:00
|
|
|
* to \em mac does not exist.
|
|
|
|
*/
|
|
|
|
const AccessPoint& Stock::get_ap(const string &mac)
|
|
|
|
{
|
2010-02-02 20:02:41 +01:00
|
|
|
unordered_map<string, AccessPoint>::const_iterator i = aps.find(mac) ;
|
2010-01-16 12:23:07 +01:00
|
|
|
if (i != aps.end())
|
|
|
|
return i->second ;
|
2010-02-11 18:33:45 +01:00
|
|
|
throw element_not_found("No AccessPoint with MAC address « " +
|
|
|
|
mac + " »!") ;
|
2010-01-16 12:23:07 +01:00
|
|
|
}
|
2010-02-02 17:34:58 +01:00
|
|
|
|
|
|
|
|
2010-04-01 11:03:24 +02:00
|
|
|
/**
|
|
|
|
* The MAC address of the AccessPoint is initialised.
|
|
|
|
*/
|
|
|
|
const AccessPoint& Stock::find_create_ap(const string &mac)
|
|
|
|
{
|
|
|
|
unordered_map<string, AccessPoint>::const_iterator i = aps.find(mac) ;
|
|
|
|
if (i != aps.end())
|
|
|
|
return i->second ;
|
|
|
|
|
|
|
|
AccessPoint &ap = aps[mac] ;
|
|
|
|
ap.set_mac_addr(mac) ;
|
|
|
|
return ap ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-19 11:42:04 +01:00
|
|
|
const ReferencePoint& Stock::
|
|
|
|
find_create_reference_point(const ReferencePoint &point)
|
|
|
|
{
|
|
|
|
// unordered_set::insert() do all the job: see the documentation at
|
2010-04-13 10:47:10 +02:00
|
|
|
// http://boost.org/doc/libs/1_42_0/doc/html/boost/unordered_set.html
|
2010-02-19 11:42:04 +01:00
|
|
|
pair<unordered_set<ReferencePoint>::iterator, bool> ret =
|
|
|
|
reference_points.insert(point) ;
|
|
|
|
return *ret.first ;
|
|
|
|
}
|
|
|
|
|
2010-02-02 17:34:58 +01:00
|
|
|
|
2010-02-19 11:42:04 +01:00
|
|
|
const CalibrationRequest& Stock::
|
|
|
|
find_create_calibration_request(const CalibrationRequest &request)
|
|
|
|
{
|
|
|
|
pair<unordered_set<CalibrationRequest>::iterator, bool> ret =
|
|
|
|
calibration_requests.insert(request) ;
|
|
|
|
return *ret.first ;
|
|
|
|
}
|
2010-02-02 17:34:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
void Stock::clear()
|
|
|
|
{
|
2010-04-02 14:03:55 +02:00
|
|
|
buildings.clear() ;
|
2010-04-13 10:47:10 +02:00
|
|
|
|
|
|
|
// Waypoints are normally deleted in Building::~Building(), so we must
|
|
|
|
// not clear waypoints before buildings, but we clear it *after*, just
|
|
|
|
// in case.
|
|
|
|
waypoints.clear() ;
|
|
|
|
|
2010-02-02 17:34:58 +01:00
|
|
|
mobiles.clear() ;
|
|
|
|
aps.clear() ;
|
2010-02-19 11:42:04 +01:00
|
|
|
reference_points.clear() ;
|
|
|
|
calibration_requests.clear() ;
|
2010-02-02 17:34:58 +01:00
|
|
|
}
|