libDAI
include/dai/exceptions.h
Go to the documentation of this file.
00001 /*  This file is part of libDAI - http://www.libdai.org/
00002  *
00003  *  Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
00004  *
00005  *  Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
00006  */
00007 
00008 
00011 
00012 
00013 #ifndef __defined_libdai_exceptions_h
00014 #define __defined_libdai_exceptions_h
00015 
00016 
00017 #include <exception>
00018 #include <stdexcept>
00019 #include <string>
00020 #include <iostream>
00021 
00022 
00024 #define DAI_QUOTE(x) #x
00025 
00027 #define DAI_TOSTRING(x) DAI_QUOTE(x)
00028 
00030 
00039 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, __FILE__, __PRETTY_FUNCTION__, DAI_TOSTRING(__LINE__), "")
00040 
00042 
00050 #define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, __FILE__, __PRETTY_FUNCTION__, DAI_TOSTRING(__LINE__), msg)
00051 
00053 #define DAI_ASSERT(condition) ((condition) ? ((void)0) : DAI_THROWE(ASSERTION_FAILED, std::string("Assertion \"" #condition "\" failed")))
00054 
00055 // Assertion only if DAI_DEBUG is defined
00056 #ifdef DAI_DEBUG
00057 
00058 #define DAI_DEBASSERT(x) do {DAI_ASSERT(x);} while(0)
00059 #else
00060 #define DAI_DEBASSERT(x) do {} while(0)
00061 #endif
00062 
00063 
00064 namespace dai {
00065 
00066 
00068 
00072 class Exception : public std::runtime_error {
00073     public:
00075         enum Code {NOT_IMPLEMENTED,
00076                    ASSERTION_FAILED,
00077                    IMPOSSIBLE_TYPECAST,
00078                    OBJECT_NOT_FOUND,
00079                    BELIEF_NOT_AVAILABLE,
00080                    UNKNOWN_ENUM_VALUE,
00081                    UNKNOWN_DAI_ALGORITHM,
00082                    UNKNOWN_PARAMETER_ESTIMATION_METHOD,
00083                    UNKNOWN_PROPERTY_TYPE,
00084                    UNKNOWN_PROPERTY,
00085                    MALFORMED_PROPERTY,
00086                    NOT_ALL_PROPERTIES_SPECIFIED,
00087                    INVALID_ALIAS,
00088                    CANNOT_READ_FILE,
00089                    CANNOT_WRITE_FILE,
00090                    INVALID_FACTORGRAPH_FILE,
00091                    INVALID_EVIDENCE_FILE,
00092                    INVALID_EMALG_FILE,
00093                    NOT_NORMALIZABLE,
00094                    MULTIPLE_UNDO,
00095                    FACTORGRAPH_NOT_CONNECTED,
00096                    INTERNAL_ERROR,
00097                    RUNTIME_ERROR,
00098                    OUT_OF_MEMORY,
00099                    NUM_ERRORS};  // NUM_ERRORS should be the last entry
00100 
00102         Exception( Code code, const char *filename, const char *function, const char *line, const std::string& detailedMsg ) :
00103             std::runtime_error(ErrorStrings[code] + (detailedMsg.empty() ? "" : (": " + detailedMsg)) + " [File " + filename + ", line " + line + ", function: " + function + "]"), 
00104             _errorcode(code), _detailedMsg(detailedMsg), _filename(filename), _function(function), _line(line) {}
00105 
00107         ~Exception() throw () {}
00108 
00110         Code getCode() const { return _errorcode; }
00111 
00113 
00115         Code code() const { return getCode(); }
00116 
00118         const std::string& getMsg() const { return ErrorStrings[_errorcode]; }
00119 
00121         const std::string& getDetailedMsg() const { return _detailedMsg; }
00122 
00124         const std::string& getFilename() const { return _filename; }
00125 
00127         const std::string& getFunction() const { return _function; }
00128 
00130         const std::string& getLine() const { return _line; }
00131 
00133         const std::string& message( const Code c ) const { return ErrorStrings[c]; }
00134 
00135     private:
00137         Code _errorcode;
00138 
00140         std::string _detailedMsg;
00141         
00143         std::string _filename;
00144 
00146         std::string _function;
00147 
00149         std::string _line;
00150 
00152         static std::string ErrorStrings[NUM_ERRORS];
00153 };
00154 
00155 
00156 }
00157 
00158 
00159 #endif