CmdLineException.h

Go to the documentation of this file.
00001 // ---------------------------------------------------------------------
00002 // $Id: CmdLineException.h 79 2008-08-10 15:58:07Z daaugusto $
00003 //
00004 //   CmdLineException.h (created on Tue Aug 23 01:08:35 BRT 2005)
00005 // 
00006 //   Command-line Parser
00007 //
00008 //   Copyright (C) 2006-2008 Douglas Adriano Augusto (daaugusto)
00009 // 
00010 // This file is part of Command-line Parser.
00011 //
00012 // Command-line Parser is free software; you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as published
00014 // by the Free Software Foundation; either version 3 of the License, or (at
00015 // your option) any later version.
00016 // 
00017 // Command-line Parser is distributed in the hope that it will be useful,
00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00020 // Public License for more details.
00021 // 
00022 // You should have received a copy of the GNU General Public License along
00023 // with Command-line Parser; if not, see <http://www.gnu.org/licenses/>.
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 } // end namespace scope
00194 //-----------------------------------------------------------------------------
00195 #endif