diff --git a/owlps-positioning/inputcsv.cc b/owlps-positioning/inputcsv.cc index 94834b4..c9bedb2 100644 --- a/owlps-positioning/inputcsv.cc +++ b/owlps-positioning/inputcsv.cc @@ -1,5 +1,6 @@ #include "inputcsv.hh" #include "posutil.hh" +#include "posexcept.hh" #include "stock.hh" #include @@ -23,7 +24,7 @@ using namespace boost ; /** * Prepares the InputCSV to read a CSV file. * @param filename The name of the file to open. - * @throw runtime_error if the file cannot be opened. + * @throw error_opening_input_file if the file cannot be opened. */ InputCSV::InputCSV(const string &filename) { @@ -31,8 +32,7 @@ InputCSV::InputCSV(const string &filename) input_file.open(input_file_name.c_str()) ; if (! input_file) - throw runtime_error("Error opening input file « " + - input_file_name + " »!") ; + throw error_opening_input_file(input_file_name) ; read_next_line() ; } @@ -136,7 +136,7 @@ const Request& InputCSV::get_next_request() { Stock::get_mobile(*ti) ; } - catch (out_of_range e) + catch (element_not_found e) { Stock::getw_mobile(*ti).set_mac_addr(*ti) ; } @@ -228,7 +228,7 @@ const Request& InputCSV::get_next_request() { Stock::get_ap(mac_ap) ; } - catch (out_of_range e) + catch (element_not_found e) { Stock::getw_ap(mac_ap).set_mac_addr(mac_ap) ; } diff --git a/owlps-positioning/posexcept.cc b/owlps-positioning/posexcept.cc index 5f59240..b06a4e9 100644 --- a/owlps-positioning/posexcept.cc +++ b/owlps-positioning/posexcept.cc @@ -3,14 +3,24 @@ using namespace std ; +element_not_found:: +element_not_found(const string &_explanation) throw(): + explanation(_explanation) {} + + +const char* element_not_found::what() const throw() +{ + string message = "Element not found in collection: " + explanation ; + return message.c_str() ; +} + + /** * @param _medium_name The medium that is unknown */ input_medium_type_unknown:: -input_medium_type_unknown(const string &_medium_name) throw() -{ - medium_name = _medium_name ; -} +input_medium_type_unknown(const string &_medium_name) throw(): + medium_name(_medium_name) {} const char* input_medium_type_unknown::what() const throw() @@ -37,3 +47,16 @@ const char* no_input_csv_file::what() const throw() { return "No input CSV file specified in the configuration!" ; } + + +error_opening_input_file:: +error_opening_input_file(const string &_file_name) throw(): + file_name(_file_name) {} + + +const char* error_opening_input_file::what() const throw() +{ + string message = "Error opening input file « " + + file_name + " »!" ; + return message.c_str() ; +} diff --git a/owlps-positioning/posexcept.hh b/owlps-positioning/posexcept.hh index 3cd5dce..c1f85c4 100644 --- a/owlps-positioning/posexcept.hh +++ b/owlps-positioning/posexcept.hh @@ -5,6 +5,18 @@ #include +class element_not_found: public std::exception +{ +private: + std::string explanation ; + +public: + element_not_found(const std::string &_explanation) throw() ; + ~element_not_found(void) throw() {} + const char* what(void) const throw() ; +} ; + + class input_medium_type_unknown: public std::exception { private: @@ -37,4 +49,17 @@ public: const char* what() const throw() ; } ; + +class error_opening_input_file: public std::exception +{ +private: + std::string file_name ; + +public: + error_opening_input_file(const std::string &_file_name) throw() ; + ~error_opening_input_file(void) throw() {} + const char* what(void) const throw() ; +} ; + + #endif // _OWLPS_POSITIONING_POSEXCEPT_HH_ diff --git a/owlps-positioning/stock.cc b/owlps-positioning/stock.cc index 686a85d..10c48b2 100644 --- a/owlps-positioning/stock.cc +++ b/owlps-positioning/stock.cc @@ -1,6 +1,5 @@ #include "stock.hh" - -#include +#include "posexcept.hh" using namespace std ; using std::tr1::unordered_map ; @@ -22,7 +21,7 @@ unordered_map Stock::aps ; * @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 std::out_of_range is thrown if the Mobile corresponding + * @throw element_not_found is thrown if the Mobile corresponding * to \em mac does not exist. */ const Mobile& Stock::get_mobile(const string &mac) @@ -30,7 +29,7 @@ const Mobile& Stock::get_mobile(const string &mac) unordered_map::const_iterator i = mobiles.find(mac) ; if (i != mobiles.end()) return i->second ; - throw out_of_range("No Mobile with MAC address « " + mac + " »!") ; + throw element_not_found("No Mobile with MAC address « " + mac + " »!") ; } @@ -38,7 +37,7 @@ const Mobile& Stock::get_mobile(const string &mac) * @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 std::out_of_range is thrown if the AccessPoint corresponding + * @throw element_not_found is thrown if the AccessPoint corresponding * to \em mac does not exist. */ const AccessPoint& Stock::get_ap(const string &mac) @@ -46,7 +45,8 @@ const AccessPoint& Stock::get_ap(const string &mac) unordered_map::const_iterator i = aps.find(mac) ; if (i != aps.end()) return i->second ; - throw out_of_range("No AccessPoint with MAC address « " + mac + " »!") ; + throw element_not_found("No AccessPoint with MAC address « " + + mac + " »!") ; } diff --git a/owlps-positioning/tests/stock_test.hh b/owlps-positioning/tests/stock_test.hh index 4e8ebe5..cc7a2b2 100644 --- a/owlps-positioning/tests/stock_test.hh +++ b/owlps-positioning/tests/stock_test.hh @@ -1,6 +1,7 @@ #include #include "stock.hh" +#include "posexcept.hh" class Stock_test: public CxxTest::TestSuite { @@ -13,9 +14,9 @@ public: // Non-existing elements TS_ASSERT_THROWS(Stock::get_mobile("aa:bb:cc:dd:ee:ff"), - std::out_of_range) ; + element_not_found) ; TS_ASSERT_THROWS(Stock::get_ap("aa:bb:cc:dd:ee:ff"), - std::out_of_range) ; + element_not_found) ; // Creation of empty elements Mobile m1 ; @@ -38,17 +39,17 @@ public: // clear() Stock::clear() ; TS_ASSERT_THROWS(Stock::get_mobile("aa:bb:cc:dd:ee:ff"), - std::out_of_range) ; + element_not_found) ; TS_ASSERT_THROWS(Stock::get_mobile("00:00:00:00:01:01"), - std::out_of_range) ; + element_not_found) ; TS_ASSERT_THROWS(Stock::get_mobile("00:00:00:00:01:02"), - std::out_of_range) ; + element_not_found) ; TS_ASSERT_THROWS(Stock::get_ap("aa:bb:cc:dd:ee:ff"), - std::out_of_range) ; + element_not_found) ; TS_ASSERT_THROWS(Stock::get_ap("00:00:00:00:02:01"), - std::out_of_range) ; + element_not_found) ; TS_ASSERT_THROWS(Stock::get_ap("00:00:00:00:02:02"), - std::out_of_range) ; + element_not_found) ; } } ;