casadi_exception.hpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of CasADi.
00003  *
00004  *    CasADi -- A symbolic framework for dynamic optimization.
00005  *    Copyright (C) 2010 by Joel Andersson, Moritz Diehl, K.U.Leuven. All rights reserved.
00006  *
00007  *    CasADi is free software; you can redistribute it and/or
00008  *    modify it under the terms of the GNU Lesser General Public
00009  *    License as published by the Free Software Foundation; either
00010  *    version 3 of the License, or (at your option) any later version.
00011  *
00012  *    CasADi is distributed in the hope that it will be useful,
00013  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *    Lesser General Public License for more details.
00016  *
00017  *    You should have received a copy of the GNU Lesser General Public
00018  *    License along with CasADi; if not, write to the Free Software
00019  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020  *
00021  */
00022 
00023 #ifndef CASADI_EXCEPTION_HPP
00024 #define CASADI_EXCEPTION_HPP
00025 
00026 #include <exception>
00027 #include <string>
00028 #include <sstream>
00029 #include <iostream>
00030 #include <stdexcept>
00031 
00032 namespace CasADi{
00033 
00050 class CasadiException : public std::exception{
00051   public:
00053   CasadiException(){
00054   }
00055     
00057   explicit CasadiException(const std::string& msg) : msg_(msg){}
00058 
00060   ~CasadiException() throw(){}
00061     
00063   virtual const char* what() const throw(){
00064     return msg_.c_str();
00065   }
00066   
00068   CasadiException& operator<<(const std::string& msg){
00069     msg_ += msg;
00070     return *this;
00071   }
00072 
00074   CasadiException& operator<<(const std::exception& ex){
00075     msg_ += " => ";
00076     msg_ += ex.what();
00077     return *this;
00078   }
00079 
00080   protected:
00081   std::string msg_;
00082 };
00083 
00084 
00085 // Assertion similar to the standard C assert statement, with the difference that it throws an exception with the same information
00086 #ifdef CASADI_NDEBUG
00087 // Release mode
00088 #define casadi_assert(x)
00089 #define casadi_assert_message(x,msg)
00090 #define casadi_assert_warning(x,msg)
00091 #define casadi_warning(msg)
00092 #define casadi_error(msg)
00093  
00094 #else // CASADI_NDEBUG
00095 // Debug mode
00096 // Convert to string
00097 #define CASADI_ASSERT_STR1(x) #x
00098 #define CASADI_ASSERT_STR(x) CASADI_ASSERT_STR1(x)
00099 
00100 // String denoting where the assertation is situated
00101 #define CASADI_ASSERT_WHERE " on line " CASADI_ASSERT_STR(__LINE__) " of file " CASADI_ASSERT_STR(__FILE__)
00102 
00103 #define casadi_log(msg) \
00104   if(verbose()){ \
00105     std::cout << "CasADi log message: " << msg << std::endl; \
00106   }
00107   
00108 #define casadi_error(msg) \
00109  {\
00110   std::stringstream ss_internal_; \
00111   ss_internal_ << CASADI_ASSERT_WHERE << std::endl << msg  <<  std::endl; \
00112   throw CasADi::CasadiException(ss_internal_.str()); \
00113  }
00114 
00115 // This assertion checks for illegal user inputs. It will not be checked if CASADI_NDEBUG is defined
00116 #define casadi_assert_message(x,msg) \
00117 { \
00118   bool is_ok; \
00119   try{ \
00120     is_ok = x; \
00121   } catch(std::exception& ex){ \
00122       throw CasADi::CasadiException(std::string("When trying to check the assertion \"" CASADI_ASSERT_STR(x) "\"" CASADI_ASSERT_WHERE ", caught: \n")+ex.what());\
00123   } \
00124  if(!is_ok) { \
00125   std::stringstream ss_internal_; \
00126   ss_internal_ << "The assertion \"" CASADI_ASSERT_STR(x) "\"" CASADI_ASSERT_WHERE " failed. " << std::endl << msg  <<  std::endl; \
00127   throw CasADi::CasadiException(ss_internal_.str()); \
00128  }\
00129 } \
00130 
00131 // This assersion if for errors caused by bugs in CasADi, use it instead of C:s assert(), but never in destructors
00132 #define casadi_assert(x) casadi_assert_message(x,"(Hint for developers: CasadiOptions.setCatchErrorsPython(False) to obtain gdb stacktrace in python.)" << std::endl << "Please notify the CasADi developers.")
00133 
00134 // This is for warnings to be issued when casadi is not in release mode and an assertion fails
00135 #define casadi_assert_warning(x,msg) \
00136 if((x)==false){ \
00137   std::cerr << "CasADi warning: \"" << msg << "\" (assertion \"" CASADI_ASSERT_STR(x) "\"" CASADI_ASSERT_WHERE " failed.)" << std::endl;\
00138 }
00139 
00140 // This is for warnings to be issued when casadi is not in release mode
00141 #define casadi_warning(msg) \
00142 std::cerr << "CasADi warning: \"" << msg << "\" issued " CASADI_ASSERT_WHERE ". " << std::endl;
00143 
00144 #endif // CASADI_NDEBUG
00145   
00146 } // namespace CasADi
00147 
00148 #endif // CASADI_EXCEPTION_HPP


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Sat Jun 8 2019 19:36:48