CmdLineException.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef cmdline_exception_h
00028 #define cmdline_exception_h
00029
00030 #include <string>
00031 #include <sstream>
00032 #include <limits>
00033 #include <exception>
00034 #include <list>
00035
00036 namespace CmdLine {
00037
00038 class E_Exception;
00039
00040
00044 class E_Exception: std::exception {
00045 public:
00050 E_Exception( const std::string& L = "", const std::string& M = "" )
00051 : m_msg( M ), m_loc( L ) {}
00052
00053 virtual ~E_Exception() throw() {};
00057 virtual const char* local() { return m_loc.c_str(); }
00061 virtual const char* what() const throw() { return m_msg.c_str(); }
00062
00063 protected:
00064 std::string m_msg;
00065 std::string m_loc;
00067 };
00068
00069
00070 inline std::ostream&
00071 operator<<( std::ostream& o, const E_Exception& e )
00072 {
00073 o << '\n' << "> Error: " << e.what() << std::endl;
00074 return o;
00075 }
00076
00077
00083 template<class T> class E_MaxMin : public E_Exception {
00084 public:
00085 E_MaxMin (T min, T max, const std::string& loc = "")
00086 {
00087 std::ostringstream o;
00088
00089 o << loc << "'max' (" << max << ") smaller than 'min' (" << min << ")"
00090 << ", feasible range: ["<< std::numeric_limits<T>::min() << ":"
00091 << std::numeric_limits<T>::max() << "]";
00092
00093 m_msg = o.str();
00094 }
00095 };
00096
00097
00103 class E_NoValue : public E_Exception {
00104 public:
00105 E_NoValue( const std::string& loc = "" )
00106 {
00107 std::ostringstream o;
00108
00109 o << loc << ": a value was not provided or it is incompatible.";
00110
00111 m_msg = o.str();
00112 }
00113 };
00114
00115
00121 template<class T> class E_OutOfRange : public E_Exception {
00122 public:
00123 E_OutOfRange (T value, T min, T max, const std::string& loc = "")
00124 {
00125 std::ostringstream o;
00126
00127 o << loc << ": value " << value << " is out of range"
00128 << ", feasible range: ["<< min << ":"
00129 << max << "]";
00130
00131 m_msg = o.str();
00132 }
00133 };
00134
00135
00141 template< > class E_OutOfRange<std::string> : public E_Exception {
00142 public:
00143 E_OutOfRange<std::string>( const std::string& value, const
00144 std::list<std::string>& range, const std::string& arg = "" )
00145 {
00146 std::ostringstream o;
00147
00148 std::list<std::string>::const_iterator it = range.begin();
00149 o << arg << ": value '" << value << "' is out of range. Allowed values:";
00150 while( it != range.end() ) o << " '" << *it++ << "'";
00151
00152 m_msg = o.str();
00153 }
00154 };
00155
00156
00162 template< > class E_OutOfRange<char> : public E_Exception {
00163 public:
00164 E_OutOfRange<char>( const char* value, const char* range,
00165 const std::string& arg = "" )
00166 {
00167 std::ostringstream o;
00168
00169 o << arg << ": value '" << value << "' is out of range. Allowed values:";
00170 for( int i = 0; range[i] != '\0'; ++i ) { o << " '" << range[i] << "'"; }
00171
00172 m_msg = o.str();
00173 }
00174 };
00175
00176
00182 class E_Duplicate: public E_Exception {
00183 public:
00184 E_Duplicate( const std::string& loc = "" )
00185 {
00186 std::ostringstream o; o << loc << ": duplicate option.";
00187
00188 m_msg = o.str();
00189 }
00190 };
00191
00192
00193 }
00194
00195 #endif