Athena IO Library
Exception.hpp
1 // This file is part of libAthena.
2 //
3 // libAthena is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // libAthena is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with libAthena. If not, see <http://www.gnu.org/licenses/>
15 
16 #ifndef EXCEPTION_HPP
17 #define EXCEPTION_HPP
18 
19 #include <string>
20 #include <stdarg.h>
21 #include "Athena/Utility.hpp"
22 #include "Athena/Global.hpp"
23 
24 #define __STRX(x) #x
25 #define __STR(x) __STRX(x)
26 #define __LINE_STRING__ __STR(__LINE__)
27 
28 namespace Athena
29 {
30 namespace error
31 {
38 class Exception
39 {
40 public:
44  inline Exception(const std::string& message, const std::string& file, const std::string& function, const int line) :
45  m_message(message),
46  m_file(file),
47  m_function(function),
48  m_line(line),
49  m_exceptionName("Exception")
50  {
51  }
52 
56  inline std::string message() const
57  {
58  return m_exceptionName + (m_message.empty() ? "" : ": " + m_message);
59  }
60 
61  inline std::string file() const
62  {
63  return m_file;
64  }
65 
66  inline std::string function() const
67  {
68  return m_function;
69  }
70 
71  inline int line() const
72  {
73  return m_line;
74  }
75 
76  inline std::string formattedMessage() const
77  {
78  return Athena::utility::sprintf("%s : %s (%i) %s", m_file.c_str(), m_function.c_str(), m_line, message().c_str());
79  }
80 protected:
81  std::string m_message;
82  std::string m_file;
83  std::string m_function;
84  int m_line;
85  std::string m_exceptionName;
86 };
87 } // error
88 } // Athena
89 #ifdef _MSC_VER
90 #define THROW_EXCEPTION(args,...) \
91 do { \
92  if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return; \
93  } else { std::string msg = Athena::utility::sprintf(__VA_ARGS__); \
94  throw Athena::error::Exception(std::string("Exception: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
95  } \
96 } while(0)
97 #elif defined(__GNUC__)
98 #define THROW_EXCEPTION(args...) \
99 do { \
100  if (atGetExceptionHandler()) { atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return; \
101  } else { \
102  std::string msg = Athena::utility::sprintf(args); \
103  throw Athena::error::Exception(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
104  } \
105 } while(0)
106 #endif
107 
108 #ifdef _MSC_VER
109 #define THROW_EXCEPTION_RETURN(ret, args,...) \
110 do { \
111  if (atGetExceptionHandler()) \
112  { \
113  atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); \
114  return ret; \
115  } else { \
116  std::string msg = Athena::utility::sprintf(__VA_ARGS__); \
117  throw Athena::error::Exception(std::string("Exception: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
118  } \
119 } while(0)
120 #elif defined(__GNUC__)
121 #define THROW_EXCEPTION_RETURN(ret, args...) \
122 do { \
123  if (atGetExceptionHandler()) { atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return ret; \
124  } else { \
125  std::string msg = Athena::utility::sprintf(args); \
126  throw Athena::error::Exception(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
127  } \
128 } while(0)
129 #endif
130 
131 #endif // EXCEPTION_HPP
Exception(const std::string &message, const std::string &file, const std::string &function, const int line)
The constructor for an Exception.
Definition: Exception.hpp:44
std::string message() const
Returns the Error message of the exception.
Definition: Exception.hpp:56
std::string m_message
The error message string.
Definition: Exception.hpp:81
The baseclass for all Exceptions.
Definition: Exception.hpp:38