blitzdg
an open-source project aiming to implement parallel discontinuous Galerkin (dg) solvers for common partial differential equations systems using blitz++ for array and tensor manipulations and MPI for distributed parallelism.
exceptions.h
1 
2 // Copyright Joakim Karlsson & Kim Gräsman 2010-2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #ifndef IGLOO_EXCEPTIONS_H
8 #define IGLOO_EXCEPTIONS_H
9 
10 #include "assert.h"
11 
12 namespace snowhouse {
13 
14  template <typename ExceptionType>
16  {
17  public:
18  static std::auto_ptr<ExceptionType>& last_exception()
19  {
20  static std::auto_ptr<ExceptionType> last;
21  return last;
22  }
23 
24  void compiler_thinks_i_am_unused() {}
25 
27  {
28  last_exception().reset(NULL);
29  }
30  };
31 
32  template <typename ExceptionType>
33  inline ExceptionType& LastException()
34  {
36  {
37  Assert::Failure("No exception was stored");
38  }
39 
41  }
42 }
43 
44 #define IGLOO_CONCAT2(a, b) a##b
45 #define IGLOO_CONCAT(a, b) IGLOO_CONCAT2(a, b)
46 
47 #define AssertThrows(EXCEPTION_TYPE, METHOD) \
48 ExceptionStorage<EXCEPTION_TYPE> IGLOO_CONCAT(IGLOO_storage_, __LINE__); IGLOO_CONCAT(IGLOO_storage_, __LINE__).compiler_thinks_i_am_unused(); \
49 { \
50  bool wrong_exception = false; \
51  bool no_exception = false; \
52  try \
53  { \
54  METHOD; \
55  no_exception = true; \
56  } \
57  catch (const EXCEPTION_TYPE& e) \
58  { \
59  ExceptionStorage<EXCEPTION_TYPE>::last_exception() = std::auto_ptr<EXCEPTION_TYPE>(new EXCEPTION_TYPE(e)); \
60  } \
61  catch(...) \
62  { \
63  wrong_exception = true; \
64  } \
65  if(no_exception) \
66  { \
67  std::ostringstream stm; \
68  stm << "Expected " << #EXCEPTION_TYPE << ". No exception was thrown."; \
69  Assert::Failure(stm.str()); \
70  } \
71  if(wrong_exception) \
72  { \
73  std::ostringstream stm; \
74  stm << "Expected " << #EXCEPTION_TYPE << ". Wrong exception was thrown."; \
75  Assert::Failure(stm.str()); \
76  } \
77 }
78 
79 #endif
80 
81 
Definition: assert.h:13
Definition: exceptions.h:15