[Positioning] Use new exceptions in posexcept

Replace use of standard exceptions (std::out_of_range,
std::runtime_error) by new exceptions in posexcept.
This commit is contained in:
Matteo Cypriani 2010-02-11 18:33:45 +01:00
parent fc1ceb25c7
commit b6ab3c841d
5 changed files with 72 additions and 23 deletions

View File

@ -1,5 +1,6 @@
#include "inputcsv.hh"
#include "posutil.hh"
#include "posexcept.hh"
#include "stock.hh"
#include <stdint.h>
@ -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) ;
}

View File

@ -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() ;
}

View File

@ -5,6 +5,18 @@
#include <string>
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_

View File

@ -1,6 +1,5 @@
#include "stock.hh"
#include <stdexcept>
#include "posexcept.hh"
using namespace std ;
using std::tr1::unordered_map ;
@ -22,7 +21,7 @@ unordered_map<string, AccessPoint> 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<string, Mobile>::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<string, AccessPoint>::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 + " »!") ;
}

View File

@ -1,6 +1,7 @@
#include <cxxtest/TestSuite.h>
#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) ;
}
} ;