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>
62 
64 
81 #define MIRA_THROW(ex, msg) \
82 { \
83  std::ostringstream ex_str; \
84  ex_str << msg; \
85  throw ex(ex_str.str(), __FILE__, __LINE__).addStackInfo<ex>(); \
86 }
87 
94 #define MIRA_THROW_NOSTACK(ex, msg) \
95 { \
96  std::ostringstream ex_str; \
97  ex_str << msg; \
98  throw ex(ex_str.str(), __FILE__, __LINE__) \
99 }
100 
108 #define MIRA_THROW_EXTSTACK(ex, msg, stack, thread) \
109 { \
110  std::ostringstream ex_str; \
111  ex_str << msg; \
112  throw ex(ex_str.str(), __FILE__, __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  ex.addInfo(ex_str.str(),__FILE__, __LINE__); \
149  throw; /* rethrow */ \
150 }
151 
165 #define MIRA_DEFINE_EXCEPTION(Ex, Base) \
166  class Ex : public Base \
167  { \
168  public: \
169  Ex(const std::string& msg, const char* file=NULL, int line=0) MIRA_NOEXCEPT_OR_NOTHROW : \
170  Base(msg, file, line) {} \
171  \
172  virtual ~Ex() MIRA_NOEXCEPT_OR_NOTHROW {} \
173  };
174 
176 
177 namespace mira {
178 
180 
194 class MIRA_BASE_EXPORT Exception : public std::exception
195 {
196 protected:
198 
199 public:
200 
208  Exception(const std::string& message, const char* file=NULL, int line=0) MIRA_NOEXCEPT_OR_NOTHROW
209  {
210  addInfo(message, file, line);
211  }
212 
215 
225  void addInfo(const std::string& message, const char* file=NULL, int line=0)
226  {
227  mInfos.push_back(Info(message, file ? std::string(file) : "", line));
228  }
229 
237  virtual const char* what() const MIRA_NOEXCEPT_OR_NOTHROW;
238 
239 
247  std::string message() const MIRA_NOEXCEPT_OR_NOTHROW;
248 
252  const CallStack& callStack() const { return mStack; }
253 
254 
256  ThreadID getThreadID() const { return mThreadID; }
257 
258 public:
259 
266  template <typename DerivedException>
267  DerivedException& addStackInfo()
268  {
269  // save the call stack:
270  mStack = getCallStack();
271  mThreadID = getCurrentThreadID();
272  return (DerivedException&)*this;
273  }
274 
280  template <typename DerivedException>
281  DerivedException& addExternalStackInfo(const CallStack& stack, ThreadID thread)
282  {
283  mStack = stack;
284  mThreadID = thread;
285  return (DerivedException&)*this;
286  }
287 
288 private:
289 
290  CallStack getCallStack();
291 
292 public:
293 
297  struct Info {
298  std::string message;
299  std::string file;
300  int line;
301 
302  public:
303  Info(const std::string& iMessage, const std::string& iFile, int iLine) :
304  message(iMessage), file(iFile), line(iLine) {}
305 
306  std::string what(std::size_t messageWidth) const;
307  };
308 
309 
311  const Info& getInfo() const { assert(!mInfos.empty()); return mInfos.front(); }
312 
313 protected:
314  std::list<Info> mInfos;
317  mutable std::string mMessage;
318 };
319 
321 
322 }
323 
324 #endif
CallStack mStack
Definition: Exception.h:315
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:311
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
Info(const std::string &iMessage, const std::string &iFile, int iLine)
Definition: Exception.h:303
std::string file
The file the exception occurred.
Definition: Exception.h:299
STL namespace.
DerivedException & addStackInfo()
FOR INTERNAL USE ONLY.
Definition: Exception.h:267
uint32 ThreadID
Platform independent thread ID.
Definition: ThreadID.h:68
std::string mMessage
as cache for what()
Definition: Exception.h:317
ThreadID mThreadID
Definition: Exception.h:316
OS independent thread id.
int line
The line the exception occurred.
Definition: Exception.h:300
#define MIRA_NOEXCEPT_OR_NOTHROW
Definition: NoExcept.h:99
std::list< Info > mInfos
Definition: Exception.h:314
Base class for exceptions.
Definition: Exception.h:194
Exception(const std::string &message, const char *file=NULL, int line=0) MIRA_NOEXCEPT_OR_NOTHROW
The constructor.
Definition: Exception.h:208
The info packet that is added in MIRA_THROW and MIRA_RETHROW.
Definition: Exception.h:297
Encapsulates unix call stack functionality.
Definition: CallStack.h:86
Exception() MIRA_NOEXCEPT_OR_NOTHROW
Definition: Exception.h:197
void addInfo(const std::string &message, const char *file=NULL, int line=0)
Adds additional information to the exception.
Definition: Exception.h:225
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
virtual ~Exception() MIRA_NOEXCEPT_OR_NOTHROW
Destructor.
Definition: Exception.h:214
DerivedException & addExternalStackInfo(const CallStack &stack, ThreadID thread)
Stores the provided callstack and thread id within the exception.
Definition: Exception.h:281
Compatible no-exception throwing specification.
std::string message
The exception message.
Definition: Exception.h:298
ThreadID getThreadID() const
Returns the id of the thread where the exception was thrown.
Definition: Exception.h:256