From 675d2d7213fdb69b197bffa62f620ce66daa1e91 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Thu, 1 Apr 2010 17:51:59 +0200 Subject: [PATCH] [Positioning] Simplify exceptions in posexcept --- owlps-positioning/src/posexcept.cc | 124 ++++++++--------------------- owlps-positioning/src/posexcept.hh | 75 +++++++---------- 2 files changed, 59 insertions(+), 140 deletions(-) diff --git a/owlps-positioning/src/posexcept.cc b/owlps-positioning/src/posexcept.cc index 1a4c622..725bfc4 100644 --- a/owlps-positioning/src/posexcept.cc +++ b/owlps-positioning/src/posexcept.cc @@ -9,31 +9,18 @@ using namespace std ; /* *** Other exceptions *** */ -bad_direction::bad_direction(const char _direction) throw() -{ - direction = _direction ; -} - - -const char* bad_direction::what() const throw() +bad_direction::bad_direction(const char direction) throw() { ostringstream message ; message << "`" << static_cast(direction) << "` is not a valid direction value!" ; - return message.str().c_str() ; + explanation = message.str() ; } 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() ; -} + posexcept("Element not found in collection:" + _explanation) {} @@ -41,56 +28,33 @@ const char* element_not_found::what() const throw() /** - * @param _medium_name The medium that is unknown + * @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(): + posexcept("The specified input medium « "+ medium_name + + " » is unknown!") {} -const char* input_medium_type_unknown::what() const throw() -{ - string message = "The specified input medium « "+ medium_name + - " » is unknown!" ; - return message.c_str() ; -} +no_input_medium::no_input_medium() throw(): + posexcept("No input medium specified in configuration!") {} -const char* no_input_medium::what() const throw() -{ - return "No input medium specified in configuration!" ; -} +null_input_medium::null_input_medium() throw(): + posexcept("The input medium is not initialised!") {} -const char* null_input_medium::what() const throw() -{ - return "The input medium is not initialised!" ; -} +no_input_csv_file::no_input_csv_file() throw(): + posexcept("No input CSV file specified in the configuration!") {} -const char* no_input_csv_file::what() const throw() -{ - return "No input CSV file specified in the configuration!" ; -} - - -const char* no_log_csv_file::what() const throw() -{ - return "No log CSV file specified in the configuration!" ; -} +no_log_csv_file::no_log_csv_file() throw(): + posexcept("No log 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() ; -} +error_opening_input_file(const string &file_name) throw(): + posexcept("Error opening input file « " + file_name + " »!") {} @@ -98,61 +62,35 @@ const char* error_opening_input_file::what() const throw() /** - * @param _medium_name The medium that is unknown + * @param medium_name The medium that is unknown */ output_medium_type_unknown:: -output_medium_type_unknown(const string &_medium_name) throw(): - medium_name(_medium_name) {} +output_medium_type_unknown(const string &medium_name) throw(): + posexcept("The specified output medium « "+ medium_name + + " » is unknown!") {} -const char* output_medium_type_unknown::what() const throw() -{ - string message = "The specified output medium « "+ medium_name + - " » is unknown!" ; - return message.c_str() ; -} - - -const char* no_output_csv_file::what() const throw() -{ - return "No input CSV file specified in the configuration!" ; -} +no_output_csv_file::no_output_csv_file() throw(): + posexcept("No output CSV file specified in the configuration!") {} error_opening_output_file:: -error_opening_output_file(const string &_file_name) throw(): - file_name(_file_name) {} - - -const char* error_opening_output_file::what() const throw() -{ - string message = "Error opening output file « " + - file_name + " »!" ; - return message.c_str() ; -} +error_opening_output_file(const string &file_name) throw(): + posexcept("Error opening output file « " + file_name + " »!") {} /* *** Algorithms *** */ -const char* no_positioning_algorithm::what() const throw() -{ - return "No positioning algorithm specified in configuration!" ; -} +no_positioning_algorithm::no_positioning_algorithm() throw(): + posexcept("No positioning algorithm specified in configuration!") {} /** - * @param _algo_name The algo that is unknown + * @param algo_name The algo that is unknown */ positioning_algorithm_unknown:: -positioning_algorithm_unknown(const string &_algo_name) throw(): - algo_name(_algo_name) {} - - -const char* positioning_algorithm_unknown::what() const throw() -{ - string message = "The specified positioning_algorithm « "+ algo_name + - " » is unknown!" ; - return message.c_str() ; -} +positioning_algorithm_unknown(const string &algo_name) throw(): + posexcept("The specified positioning_algorithm « "+ algo_name + + " » is unknown!") {} diff --git a/owlps-positioning/src/posexcept.hh b/owlps-positioning/src/posexcept.hh index d2426d1..d69bca0 100644 --- a/owlps-positioning/src/posexcept.hh +++ b/owlps-positioning/src/posexcept.hh @@ -7,7 +7,22 @@ /// Super-class of all exceptions defined in OWLPS-Positioning -class posexcept: public std::exception {} ; +class posexcept: public std::exception +{ +protected: + std::string explanation ; + +public: + posexcept(const std::string &_explanation = "") throw(): + explanation(_explanation) {} + + ~posexcept(void) throw() {} + + const char* what(void) const throw() + { + return explanation.c_str() ; + } +} ; @@ -16,24 +31,15 @@ class posexcept: public std::exception {} ; class bad_direction: public posexcept { -private: - char direction ; - public: - bad_direction(const char _direction) throw() ; - const char* what() const throw() ; + bad_direction(const char direction) throw() ; } ; class element_not_found: public posexcept { -private: - std::string explanation ; - public: element_not_found(const std::string &_explanation) throw() ; - ~element_not_found(void) throw() {} - const char* what(void) const throw() ; } ; @@ -43,53 +49,43 @@ public: class input_medium_type_unknown: public posexcept { -private: - std::string medium_name ; - public: - input_medium_type_unknown(const std::string &_medium_name) throw() ; - ~input_medium_type_unknown(void) throw() {} - const char* what(void) const throw() ; + input_medium_type_unknown(const std::string &medium_name) throw() ; } ; class no_input_medium: public posexcept { public: - const char* what() const throw() ; + no_input_medium(void) throw() ; } ; class null_input_medium: public posexcept { public: - const char* what() const throw() ; + null_input_medium(void) throw() ; } ; class no_input_csv_file: public posexcept { public: - const char* what() const throw() ; + no_input_csv_file(void) throw() ; } ; class no_log_csv_file: public posexcept { public: - const char* what() const throw() ; + no_log_csv_file(void) throw() ; } ; class error_opening_input_file: public posexcept { -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() ; + error_opening_input_file(const std::string &file_name) throw() ; } ; @@ -99,32 +95,22 @@ public: class output_medium_type_unknown: public posexcept { -private: - std::string medium_name ; - public: - output_medium_type_unknown(const std::string &_medium_name) throw() ; - ~output_medium_type_unknown(void) throw() {} - const char* what(void) const throw() ; + output_medium_type_unknown(const std::string &medium_name) throw() ; } ; class no_output_csv_file: public posexcept { public: - const char* what() const throw() ; + no_output_csv_file(void) throw() ; } ; class error_opening_output_file: public posexcept { -private: - std::string file_name ; - public: - error_opening_output_file(const std::string &_file_name) throw() ; - ~error_opening_output_file(void) throw() {} - const char* what(void) const throw() ; + error_opening_output_file(const std::string &file_name) throw() ; } ; @@ -135,19 +121,14 @@ public: class no_positioning_algorithm: public posexcept { public: - const char* what() const throw() ; + no_positioning_algorithm(void) throw() ; } ; class positioning_algorithm_unknown: public posexcept { -private: - std::string algo_name ; - public: - positioning_algorithm_unknown(const std::string &_algo_name) throw() ; - ~positioning_algorithm_unknown(void) throw() {} - const char* what(void) const throw() ; + positioning_algorithm_unknown(const std::string &algo_name) throw() ; } ;