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 <exception>
51 #include <sstream>
52 
53 #include <list>
54 
55 #include <error/CallStack.h>
56 #include <thread/ThreadID.h>
57 #include <utils/NoExcept.h>
59 
61 
78 #define MIRA_THROW(ex, msg) \
79 { \
80  std::ostringstream ex_str; \
81  ex_str << msg; \
82  constexpr auto fileInMIRAPath = mira::chopMIRAPath(__FILE__); \
83  throw ex(ex_str.str(), fileInMIRAPath, __LINE__).addStackInfo<ex>(); \
84 }
85 
92 #define MIRA_THROW_NOSTACK(ex, msg) \
93 { \
94  std::ostringstream ex_str; \
95  ex_str << msg; \
96  constexpr auto fileInMIRAPath = mira::chopMIRAPath(__FILE__); \
97  throw ex(ex_str.str(), fileInMIRAPath, __LINE__); \
98 }
99 
107 #define MIRA_THROW_EXTSTACK(ex, msg, stack, thread) \
108 { \
109  std::ostringstream ex_str; \
110  ex_str << msg; \
111  constexpr auto fileInMIRAPath = mira::chopMIRAPath(__FILE__); \
112  throw ex(ex_str.str(), fileInMIRAPath, __LINE__) \
113  .addExternalStackInfo<ex>(stack, thread); \
114 }
115 
144 #define MIRA_RETHROW(ex, msg) \
145 { \
146  std::ostringstream ex_str; \
147  ex_str << msg; \
148  constexpr auto fileInMIRAPath = mira::chopMIRAPath(__FILE__); \
149  ex.addInfo(ex_str.str(),fileInMIRAPath, __LINE__); \
150  throw; /* rethrow */ \
151 }
152 
166 #define MIRA_DEFINE_EXCEPTION(Ex, Base) \
167  class Ex : public Base \
168  { \
169  public: \
170  Ex(std::string msg, const char* file=NULL, int line=0) MIRA_NOEXCEPT_OR_NOTHROW : \
171  Base(std::move(msg), file, line) {} \
172  \
173  virtual ~Ex() MIRA_NOEXCEPT_OR_NOTHROW {} \
174  };
175 
177 
178 namespace mira {
179 
181 
195 class Exception : public std::exception
196 {
197 protected:
199 
200 public:
201 
209  Exception(std::string message, const char* file=NULL, int line=0) MIRA_NOEXCEPT_OR_NOTHROW
210  {
211  addInfo(std::move(message), file, line);
212  }
213 
215  ~Exception() MIRA_NOEXCEPT_OR_NOTHROW override = default;
216 
226  void addInfo(std::string message, const char* file=NULL, int line=0)
227  {
228  mInfos.emplace_back(std::move(message), file ? std::string(file) : "", line);
229  }
230 
238  virtual const char* what() const MIRA_NOEXCEPT_OR_NOTHROW;
239 
240 
248  std::string message() const MIRA_NOEXCEPT_OR_NOTHROW;
249 
253  const CallStack& callStack() const { return mStack; }
254 
255 
257  ThreadID getThreadID() const { return mThreadID; }
258 
259 public:
260 
267  template <typename DerivedException>
268  DerivedException& addStackInfo()
269  {
270  // save the call stack:
271  mStack = createCallStack();
273  return (DerivedException&)*this;
274  }
275 
281  template <typename DerivedException>
282  DerivedException& addExternalStackInfo(CallStack stack, ThreadID thread)
283  {
284  mStack = std::move(stack);
285  mThreadID = thread;
286  return (DerivedException&)*this;
287  }
288 
289 private:
290 
291  CallStack createCallStack();
292 
293 public:
294 
298  struct Info {
299  std::string message;
300  std::string file;
301  int line;
302 
303  public:
304  Info(std::string iMessage, std::string iFile, int iLine) :
305  message(std::move(iMessage)), file(std::move(iFile)), line(iLine) {}
306 
307  std::string what(std::size_t messageWidth) const;
308  };
309 
310 
312  const Info& getInfo() const { assert(!mInfos.empty()); return mInfos.front(); }
313 
314 protected:
315  std::list<Info> mInfos;
318  mutable std::string mMessage;
319 };
320 
322 
323 }
324 
325 #endif
CallStack mStack
Definition: Exception.h:316
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:312
Exception(std::string message, const char *file=NULL, int line=0) MIRA_NOEXCEPT_OR_NOTHROW
The constructor.
Definition: Exception.h:209
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:230
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Compile-time path handling.
~Exception() MIRA_NOEXCEPT_OR_NOTHROW override=default
Destructor.
std::string file
The file the exception occurred.
Definition: Exception.h:300
STL namespace.
void addInfo(std::string message, const char *file=NULL, int line=0)
Adds additional information to the exception.
Definition: Exception.h:226
DerivedException & addStackInfo()
FOR INTERNAL USE ONLY.
Definition: Exception.h:268
uint32 ThreadID
Platform independent thread ID.
Definition: ThreadID.h:64
std::string mMessage
as cache for what()
Definition: Exception.h:318
ThreadID mThreadID
Definition: Exception.h:317
OS independent thread id.
int line
The line the exception occurred.
Definition: Exception.h:301
ThreadID getCurrentThreadID()
Get the ID of the current thread.
Definition: ThreadID.h:72
std::string message() const MIRA_NOEXCEPT_OR_NOTHROW
Similar to what().
const CallStack & callStack() const
Returns the state of the callstack at the moment when the exception was thrown.
Definition: Exception.h:253
#define MIRA_NOEXCEPT_OR_NOTHROW
Definition: NoExcept.h:99
std::list< Info > mInfos
Definition: Exception.h:315
Base class for exceptions.
Definition: Exception.h:195
The info packet that is added in MIRA_THROW and MIRA_RETHROW.
Definition: Exception.h:298
Encapsulates unix call stack functionality.
Definition: CallStack.h:75
DerivedException & addExternalStackInfo(CallStack stack, ThreadID thread)
Stores the provided callstack and thread id within the exception.
Definition: Exception.h:282
virtual const char * what() const MIRA_NOEXCEPT_OR_NOTHROW
Returns the text of exception containing the information given in MIRA_THROW and MIRA_RETHROW as well...
Exception() MIRA_NOEXCEPT_OR_NOTHROW
Definition: Exception.h:198
Info(std::string iMessage, std::string iFile, int iLine)
Definition: Exception.h:304
Compatible no-exception throwing specification.
std::string message
The exception message.
Definition: Exception.h:299
ThreadID getThreadID() const
Returns the id of the thread where the exception was thrown.
Definition: Exception.h:257
std::string what(std::size_t messageWidth) const