MIRA
Exception.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_EXCEPTION_H_
48 #define _MIRA_EXCEPTION_H_
49 
50 #include <assert.h>
51 
52 #include <string.h>
53 
54 #include <exception>
55 #include <sstream>
56 
57 #include <list>
58 
59 #include <error/CallStack.h>
60 #include <thread/ThreadID.h>
61 #include <utils/NoExcept.h>
63 
65 
82 #define MIRA_THROW(ex, msg) \
83 { \
84  std::ostringstream ex_str; \
85  ex_str << msg; \
86  constexpr auto fileInMIRAPath = mira::chopMIRAPath(__FILE__); \
87  throw ex(ex_str.str(), fileInMIRAPath, __LINE__).addStackInfo<ex>(); \
88 }
89 
96 #define MIRA_THROW_NOSTACK(ex, msg) \
97 { \
98  std::ostringstream ex_str; \
99  ex_str << msg; \
100  constexpr auto fileInMIRAPath = mira::chopMIRAPath(__FILE__); \
101  throw ex(ex_str.str(), fileInMIRAPath, __LINE__); \
102 }
103 
111 #define MIRA_THROW_EXTSTACK(ex, msg, stack, thread) \
112 { \
113  std::ostringstream ex_str; \
114  ex_str << msg; \
115  constexpr auto fileInMIRAPath = mira::chopMIRAPath(__FILE__); \
116  throw ex(ex_str.str(), fileInMIRAPath, __LINE__) \
117  .addExternalStackInfo<ex>(stack, thread); \
118 }
119 
148 #define MIRA_RETHROW(ex, msg) \
149 { \
150  std::ostringstream ex_str; \
151  ex_str << msg; \
152  constexpr auto fileInMIRAPath = mira::chopMIRAPath(__FILE__); \
153  ex.addInfo(ex_str.str(),fileInMIRAPath, __LINE__); \
154  throw; /* rethrow */ \
155 }
156 
170 #define MIRA_DEFINE_EXCEPTION(Ex, Base) \
171  class Ex : public Base \
172  { \
173  public: \
174  Ex(std::string msg, const char* file=NULL, int line=0) MIRA_NOEXCEPT_OR_NOTHROW : \
175  Base(std::move(msg), file, line) {} \
176  \
177  virtual ~Ex() MIRA_NOEXCEPT_OR_NOTHROW {} \
178  };
179 
181 
182 namespace mira {
183 
185 
199 class MIRA_BASE_EXPORT Exception : public std::exception
200 {
201 protected:
203 
204 public:
205 
213  Exception(std::string message, const char* file=NULL, int line=0) MIRA_NOEXCEPT_OR_NOTHROW
214  {
215  addInfo(std::move(message), file, line);
216  }
217 
220 
230  void addInfo(std::string message, const char* file=NULL, int line=0)
231  {
232  mInfos.emplace_back(std::move(message), file ? std::string(file) : "", line);
233  }
234 
242  virtual const char* what() const MIRA_NOEXCEPT_OR_NOTHROW;
243 
244 
252  std::string message() const MIRA_NOEXCEPT_OR_NOTHROW;
253 
257  const CallStack& callStack() const { return mStack; }
258 
259 
261  ThreadID getThreadID() const { return mThreadID; }
262 
263 public:
264 
271  template <typename DerivedException>
272  DerivedException& addStackInfo()
273  {
274  // save the call stack:
275  mStack = createCallStack();
276  mThreadID = getCurrentThreadID();
277  return (DerivedException&)*this;
278  }
279 
285  template <typename DerivedException>
286  DerivedException& addExternalStackInfo(CallStack stack, ThreadID thread)
287  {
288  mStack = std::move(stack);
289  mThreadID = thread;
290  return (DerivedException&)*this;
291  }
292 
293 private:
294 
295  CallStack createCallStack();
296 
297 public:
298 
302  struct Info {
303  std::string message;
304  std::string file;
305  int line;
306 
307  public:
308  Info(std::string iMessage, std::string iFile, int iLine) :
309  message(std::move(iMessage)), file(std::move(iFile)), line(iLine) {}
310 
311  std::string what(std::size_t messageWidth) const;
312  };
313 
314 
316  const Info& getInfo() const { assert(!mInfos.empty()); return mInfos.front(); }
317 
318 protected:
319  std::list<Info> mInfos;
322  mutable std::string mMessage;
323 };
324 
326 
327 }
328 
329 #endif
CallStack mStack
Definition: Exception.h:320
Encapsulates call stack functionality.
const Info & getInfo() const
Returns the first info packet that describes the location where the exception has occured...
Definition: Exception.h:316
Exception(std::string message, const char *file=NULL, int line=0) MIRA_NOEXCEPT_OR_NOTHROW
The constructor.
Definition: Exception.h:213
PropertyHint file(const std::string &filters=std::string(), bool save=false)
Tells the property editor that the path is for a file, and that it should show a "File Open"/"File Sa...
Definition: Path.h:247
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Compile-time path handling.
std::string file
The file the exception occurred.
Definition: Exception.h:304
STL namespace.
void addInfo(std::string message, const char *file=NULL, int line=0)
Adds additional information to the exception.
Definition: Exception.h:230
DerivedException & addStackInfo()
FOR INTERNAL USE ONLY.
Definition: Exception.h:272
uint32 ThreadID
Platform independent thread ID.
Definition: ThreadID.h:68
std::string mMessage
as cache for what()
Definition: Exception.h:322
ThreadID mThreadID
Definition: Exception.h:321
OS independent thread id.
int line
The line the exception occurred.
Definition: Exception.h:305
#define MIRA_NOEXCEPT_OR_NOTHROW
Definition: NoExcept.h:99
std::list< Info > mInfos
Definition: Exception.h:319
Base class for exceptions.
Definition: Exception.h:199
The info packet that is added in MIRA_THROW and MIRA_RETHROW.
Definition: Exception.h:302
Encapsulates unix call stack functionality.
Definition: CallStack.h:86
DerivedException & addExternalStackInfo(CallStack stack, ThreadID thread)
Stores the provided callstack and thread id within the exception.
Definition: Exception.h:286
Exception() MIRA_NOEXCEPT_OR_NOTHROW
Definition: Exception.h:202
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
Info(std::string iMessage, std::string iFile, int iLine)
Definition: Exception.h:308
virtual ~Exception() MIRA_NOEXCEPT_OR_NOTHROW
Destructor.
Definition: Exception.h:219
Compatible no-exception throwing specification.
std::string message
The exception message.
Definition: Exception.h:303
ThreadID getThreadID() const
Returns the id of the thread where the exception was thrown.
Definition: Exception.h:261