Free Electron
Exception.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 
8 #ifndef __platform_Exception_h__
9 #define __platform_Exception_h__
10 
11 // TODO: the use of Statics here is not safe. Try to find a better way.
12 
13 #if FE_CODEGEN>FE_DEBUG
14 #define feX throw ::fe::Exception
15 // funkiness to get __FILE__ and __LINE__ with varargs
16 #elif FE_COMPILER==FE_GNU
17 #define feX ::fe::Exception::stage(__FILE__,__LINE__,__PRETTY_FUNCTION__), throw ::fe::Exception
18 #elif FE_COMPILER==FE_MICROSOFT
19 #define feX ::fe::Exception::stage(__FILE__,__LINE__,__FUNCTION__), throw ::fe::Exception
20 #else
21 #define feX ::fe::Exception::stage(__FILE__,__LINE__,__func__), throw ::fe::Exception
22 #endif
23 
24 #define feAssert(cond,msg) {if(!(cond)){feX(msg);}}
25 
26 namespace fe
27 {
28 
29 /**************************************************************************//**
30  @brief Generic exception carrying a fe::String payload
31 
32  @ingroup platform
33 *//***************************************************************************/
34 class FE_DL_PUBLIC FE_DL_EXPORT Exception: public std::exception
35 {
36  public:
37  Exception(void) throw();
38  Exception(const char *fmt, ...) throw();
39  Exception(const char *location,
40  const char *fmt, ...) throw();
41  Exception(const Result &result,
42  const char *fmt, ...) throw();
43  Exception(const Result &result, const char *location,
44  const char *fmt, ...) throw();
45  Exception(const Result &result, const char *location,
46  String fmt, ...) throw();
47  Exception(const Result &result) throw();
48  Exception(const Exception &other) throw();
49 
50 virtual ~Exception(void) throw();
51 
52  //* as std::exception
53 virtual
54 const char* what(void) const throw()
55  { return m_message.c_str(); }
56 
57  Exception &operator=(const Exception &other) throw();
58 
59  /// @brief Return the fe::String payload
60 const String &getMessage(void) const throw()
61  { return m_message; }
62 
63 const String &getLocation(void) const throw()
64  { return m_location; }
65 
66 const String &getFile(void) const throw()
67  { return m_file; }
68 
69 const String &getFunction(void) const throw()
70  { return m_function; }
71 
72 const String &getStack(void) const throw()
73  { return m_stack; }
74 
75  Result getResult(void) const throw()
76  { return m_result; }
77 
78  int getLine(void) const throw()
79  { return m_line; }
80 
81  /// @brief Send the message to the global log
82  void log(void) throw();
83 
84  /** @brief Assert instead of except,
85  if selected
86 
87  If the env variable "FE_E_ASSERT" is set,
88  exceptions cause an assertion when they create.
89  They don't have to be thrown first.
90 
91  @internal */
92 virtual void checkAssert(void) throw();
93 
94  /** Not thread safe, so exceptions thrown from
95  multiple threads are in a race condition on
96  file and line.
97  */
98 static void stage(const char *file, int line, const char* function)
99  {
100  //ms_file = file;
101  //ms_line = line;
102  //ms_function = function;
103  }
104 
105 
106  protected:
107  String m_message;
108  String m_location;
109  Result m_result;
110  String m_file;
111  int m_line;
112  String m_function;
113  String m_stack;
114 static String ms_file;
115 static int ms_line;
116 static String ms_function;
117 
118 };
119 
120 FE_DL_PUBLIC String print(const Exception& a_rException);
121 
122 } /* namespace */
123 
124 #endif /* __platform_Exception_h__ */
static void stage(const char *file, int line, const char *function)
Not thread safe, so exceptions thrown from multiple threads are in a race condition on file and line...
Definition: Exception.h:98
kernel
Definition: namespace.dox:3
Generic exception carrying a fe::String payload.
Definition: Exception.h:34
Automatically reference-counted string container.
Definition: String.h:128
Result
Generalized return value indicating success or failure, and why.
Definition: Result.h:24
const String & getMessage(void) const
Return the fe::String payload.
Definition: Exception.h:60