Exception.h

Go to the documentation of this file.
00001 // ---------------------------------------------------------------------
00002 // $Id: Exception.h 227 2008-08-13 20:51:32Z daaugusto $
00003 //
00004 //   Exception.h (created on Tue Aug 23 01:08:35 BRT 2005)
00005 // 
00006 //   Genetic Algorithm File Fitter (gaffitter)
00007 //
00008 //   Copyright (C) 2005-2008 Douglas A. Augusto
00009 // 
00010 // This file is part of gaffitter.
00011 // 
00012 // gaffitter is free software; you can redistribute it and/or modify it
00013 // under the terms of the GNU General Public License as published by the
00014 // Free Software Foundation; either version 3 of the License, or (at
00015 // your option) any later version.
00016 // 
00017 // gaffitter is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // General Public License for more details.
00021 // 
00022 // You should have received a copy of the GNU General Public License
00023 // along with gaffitter; if not, see <http://www.gnu.org/licenses/>.
00024 //
00025 // ---------------------------------------------------------------------
00026 
00027 #ifndef exception_hh
00028 #define exception_hh 
00029 
00030 #include <string>
00031 #include <sstream>
00032 #include <exception>
00033 
00034 //----------------------------------------------------------------------
00035 #ifdef NDEBUG
00036 const bool debug = false;
00037 #else 
00038 const bool debug = true;
00039 #endif
00040 
00041 class E_Exception;
00042 
00043 //----------------------------------------------------------------------
00047 class E_Exception: std::exception { 
00048 public:
00053   E_Exception(const std::string& L = "", const std::string& M = "")
00054             : m_msg( M ), m_loc( L ) {}
00055 
00056   virtual ~E_Exception() throw() {};
00060   virtual const char * local() { return m_loc.c_str(); }
00064   virtual const char * what() const throw() { return m_msg.c_str(); }
00065 
00066 protected:
00067   std::string m_msg; 
00068   std::string m_loc; 
00070 };
00071 
00072 //----------------------------------------------------------------------
00073 inline std::ostream&
00074 operator<<( std::ostream& o, const E_Exception& e )
00075 {
00076    o << '\n' << "> Error: " << e.what() << std::endl;
00077    return o;
00078 }
00079 
00080 //-----------------------------------------------------------------------------
00081 class E_NoInputFiles : public E_Exception {
00082 public:  
00083    E_NoInputFiles ( const std::string& loc = "" )
00084       : E_Exception (loc, "No input to process") {};
00085 };
00086 
00087 //-----------------------------------------------------------------------------
00088 class E_BigInput : public E_Exception {
00089 public:  
00090    E_BigInput ( const std::string& loc = "" )
00091       : E_Exception (loc, "Too many input files/items") {};
00092 };
00093 
00094 //-----------------------------------------------------------------------------
00095 class E_NoTarget : public E_Exception {
00096 public:  
00097    E_NoTarget (const std::string& app = "gaffitter"): E_Exception ("", 
00098          "Missing '--target' (Try '"+app+" -h' for more information)") {};
00099 };
00100 
00101 //-----------------------------------------------------------------------------
00102 template<class X, class A> inline void Assert(A expression)
00103 {
00104    if (debug && !expression) throw X();
00105 } 
00106 
00107 inline void Assert(bool expression)
00108 {
00109    Assert<E_Exception>(expression);
00110 }
00111 
00112 //-----------------------------------------------------------------------------
00113 #endif