diff --git a/owlps-positioning/src/inputcsv.cc b/owlps-positioning/src/inputcsv.cc index e17d5ff..355b993 100644 --- a/owlps-positioning/src/inputcsv.cc +++ b/owlps-positioning/src/inputcsv.cc @@ -90,17 +90,8 @@ const Request& InputCSV::get_next_request() // Read Mobile MAC field if (ti == tok.end()) // Wrong number of fields return *current_request ; - - // If the mobile did not exist, we create it - try - { - Stock::get_mobile(*ti) ; - } - catch (element_not_found e) - { - Stock::getw_mobile(*ti).set_mac_addr(*ti) ; - } - current_request->set_mobile(&Stock::get_mobile(*ti)) ; + const Mobile &mobile = Stock::find_create_mobile(*ti) ; + current_request->set_mobile(&mobile) ; // Read Timestamp field if (++ti == tok.end()) @@ -213,19 +204,8 @@ const Request& InputCSV::get_next_request() current_request->clear() ; // Blank current request return *current_request ; } - - // If the AP did not exist, we create it - try - { - Stock::get_ap(mac_ap) ; - } - catch (element_not_found e) - { - Stock::getw_ap(mac_ap).set_mac_addr(mac_ap) ; - } - measurements[mac_ap].set_ap(&Stock::get_ap(mac_ap)) ; - - // Adding value + const AccessPoint &ap = Stock::find_create_ap(mac_ap) ; + measurements[mac_ap].set_ap(&ap) ; measurements[mac_ap].add_ss(ss) ; } diff --git a/owlps-positioning/src/stock.cc b/owlps-positioning/src/stock.cc index 45566f3..3f14878 100644 --- a/owlps-positioning/src/stock.cc +++ b/owlps-positioning/src/stock.cc @@ -44,6 +44,21 @@ const Mobile& Stock::get_mobile(const string &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. @@ -61,6 +76,21 @@ const AccessPoint& Stock::get_ap(const string &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) { diff --git a/owlps-positioning/src/stock.hh b/owlps-positioning/src/stock.hh index 3c14690..7d0e6fc 100644 --- a/owlps-positioning/src/stock.hh +++ b/owlps-positioning/src/stock.hh @@ -28,11 +28,15 @@ private: public: /// Read the Mobile corresponding to a given MAC address static const Mobile& get_mobile(const std::string &mac) ; + /// Look for a Mobile and create it if it does not exist + static const Mobile& find_create_mobile(const std::string &mac) ; /// Get a reference to the Mobile corresponding to a given MAC address static Mobile& getw_mobile(const std::string &mac) ; /// Read the AccessPoint corresponding to a given MAC address static const AccessPoint& get_ap(const std::string &mac) ; + /// Look for an AccessPoint and create it if it does not exist + static const AccessPoint& find_create_ap(const std::string &mac) ; /// Get a reference to the AccessPoint corresponding to a given MAC address static AccessPoint& getw_ap(const std::string &mac) ;