#include "stock.hh" #include "posexcept.hh" using namespace std ; using std::tr1::unordered_map ; using std::tr1::unordered_set ; /// Hash function, required to put ReferencePoint in a set size_t hash_value(const ReferencePoint &object) { return boost::hash_value(static_cast(object)) ; } /* *** Attribute definitions *** */ unordered_map Stock::buildings ; unordered_map Stock::mobiles ; unordered_map Stock::aps ; unordered_set Stock::reference_points ; unordered_set Stock::calibration_requests ; /* *** Accessors *** */ /** * The name of the Building is initialised. */ const Building& Stock::find_create_building(const string &name) { unordered_map::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::const_iterator i = buildings.find(name) ; if (i != buildings.end()) return i->second ; throw element_not_found("No Building with name « " + name + " »!") ; } /** * @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. * @throw element_not_found is thrown if the Mobile corresponding * to \em mac does not exist. */ const Mobile& Stock::get_mobile(const string &mac) { unordered_map::const_iterator i = mobiles.find(mac) ; if (i != mobiles.end()) return i->second ; throw element_not_found("No Mobile with MAC address « " + mac + " »!") ; } /** * The MAC address of the Mobile is initialised. */ const Mobile& Stock::find_create_mobile(const string &mac) { unordered_map::const_iterator i = mobiles.find(mac) ; if (i != mobiles.end()) return i->second ; Mobile &mobile = mobiles[mac] ; mobile.set_mac_addr(mac) ; return mobile ; } /** * @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. * @throw element_not_found is thrown if the AccessPoint corresponding * to \em mac does not exist. */ const AccessPoint& Stock::get_ap(const string &mac) { unordered_map::const_iterator i = aps.find(mac) ; if (i != aps.end()) return i->second ; throw element_not_found("No AccessPoint with MAC address « " + mac + " »!") ; } /** * The MAC address of the AccessPoint is initialised. */ const AccessPoint& Stock::find_create_ap(const string &mac) { unordered_map::const_iterator i = aps.find(mac) ; if (i != aps.end()) return i->second ; AccessPoint &ap = aps[mac] ; ap.set_mac_addr(mac) ; return ap ; } const ReferencePoint& Stock:: find_create_reference_point(const ReferencePoint &point) { // unordered_set::insert() do all the job: see the documentation at // http://www.boost.org/doc/libs/1_41_0/doc/html/boost/unordered_set.html pair::iterator, bool> ret = reference_points.insert(point) ; return *ret.first ; } const CalibrationRequest& Stock:: find_create_calibration_request(const CalibrationRequest &request) { pair::iterator, bool> ret = calibration_requests.insert(request) ; return *ret.first ; } void Stock::clear() { buildings.clear() ; mobiles.clear() ; aps.clear() ; reference_points.clear() ; calibration_requests.clear() ; }