MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
simple_assert.hpp
Go to the documentation of this file.
1 // moose/assert.hpp - SIMPLE_ASSERT(expr)
2 // SIMPLE_ASSERT_MSG(expr, msg)
3 // SIMPLE_VERIFY(expr)
4 //
5 // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
6 // Copyright (c) 2007 Peter Dimov
7 // Copyright (c) Beman Dawes 2011
8 // Copyright (c) Dilawar Singh 2013
9 //
10 // Distributed under the Boost Software License, Version 1.0. (See
11 // accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
13 //
14 // Note: There are no include guards. This is intentional.
15 //
16 // See http://www.moose.org/libs/utility/assert.html for documentation.
17 //
18 
19 //
20 // Stop inspect complaining about use of 'assert':
21 //
22 // mooseinspect:naassert_macro
23 //
24 // Log:
25 // Thursday 01 May 2014 01:18:02 PM IST
26 //
27 // This files is a modified version of boost/assert.hpp file. The names of
28 // macros have been changed.
29 //
30 
31 //--------------------------------------------------------------------------------------//
32 // SIMPLE_ASSERT //
33 //--------------------------------------------------------------------------------------//
34 
35 #undef SIMPLE_ASSERT
36 
37 #if defined(SIMPLE_DISABLE_ASSERTS)
38 
39 # define SIMPLE_ASSERT(expr) ((void)0)
40 
41 #elif defined(SIMPLE_ENABLE_ASSERT_HANDLER)
42 
43 #include "current_function.hpp"
44 #include <stringstream>
45 
46 namespace moose
47 {
48  void assertion_failed(char const * expr,
49  char const * function, char const * file, long line); // user defined
50 } // namespace moose
51 
52 #define SIMPLE_ASSERT(expr) ((expr) \
53  ? ((void)0) \
54  : ::moose::assertion_failed(#expr, SIMPLE_CURRENT_FUNCTION, __FILE__, __LINE__))
55 
56 #else
57 # include <assert.h> // .h to support old libraries w/o <cassert> - effect is the same
58 # define SIMPLE_ASSERT(expr) assert(expr)
59 #endif
60 
61 //--------------------------------------------------------------------------------------//
62 // SIMPLE_ASSERT_MSG //
63 //--------------------------------------------------------------------------------------//
64 
65 # undef SIMPLE_ASSERT_MSG
66 
67 #if defined(SIMPLE_DISABLE_ASSERTS) || defined(NDEBUG)
68 
69  #define SIMPLE_ASSERT_MSG(expr, msg) ((void)0)
70 
71 #elif defined(SIMPLE_ENABLE_ASSERT_HANDLER)
72 
73  #include "current_function.hpp"
74 
75  namespace moose
76  {
77  void assertion_failed_msg(char const * expr, char const * msg,
78  char const * function, char const * file, long line); // user defined
79  } // namespace moose
80 
81  #define SIMPLE_ASSERT_MSG(expr, msg) ((expr) \
82  ? ((void)0) \
83  : ::moose::assertion_failed_msg(#expr, msg, SIMPLE_CURRENT_FUNCTION, __FILE__, __LINE__))
84 
85 #else
86  #ifndef SIMPLE_ASSERT_HPP
87  #define SIMPLE_ASSERT_HPP
88  #include <cstdlib>
89  #include <iostream>
90  #include "current_function.hpp"
91 
92  // IDE's like Visual Studio perform better if output goes to std::cout or
93  // some other stream, so allow user to configure output stream:
94  #ifndef SIMPLE_ASSERT_MSG_OSTREAM
95  # define SIMPLE_ASSERT_MSG_OSTREAM std::cerr
96  #endif
97 
98  namespace moose
99  {
100  namespace assertion
101  {
102  namespace detail
103  {
104  inline void assertion_failed_msg(char const * expr, char const * msg, char const * function,
105  char const * file, long line)
106  {
108  << "***** Internal Program Error - assertion (" << expr << ") failed in "
109  << function << ":\n"
110  << file << '(' << line << "): " << msg << std::endl;
111  #ifdef UNDER_CE
112  // The Windows CE CRT library does not have abort() so use exit(-1) instead.
113  std::exit(-1);
114  #else
115  std::abort();
116  #endif
117  }
118  } // detail
119  } // assertion
120  } // detail
121  #endif
122 
123  #define SIMPLE_ASSERT_MSG(expr, msg) ((expr) \
124  ? ((void)0) \
125  : ::moose::assertion::detail::assertion_failed_msg(#expr, msg, \
126  SIMPLE_CURRENT_FUNCTION, __FILE__, __LINE__))
127 #endif
128 
129 //--------------------------------------------------------------------------------------//
130 // SIMPLE_VERIFY //
131 //--------------------------------------------------------------------------------------//
132 
133 #undef SIMPLE_VERIFY
134 
135 #if defined(SIMPLE_DISABLE_ASSERTS) || ( !defined(MOOSE_ENABLE_ASSERT_HANDLER) && defined(NDEBUG) )
136 
137 # define SIMPLE_VERIFY(expr) ((void)(expr))
138 
139 #else
140 
141 # define SIMPLE_VERIFY(expr) MOOSE_ASSERT(expr)
142 
143 /*-----------------------------------------------------------------------------
144  * SIMPLE_WARN
145  *
146  * This macro accepts a stringstream like object as its second argument. First
147  * arguement is condition which must return a bool.
148  *-----------------------------------------------------------------------------*/
149 #define SIMPLE_WARN(condition, msg) \
150  if(! (condition) ) { \
151  std::ostringstream ss; \
152  ss << endl << "[WARN] " << msg; \
153  std::cout << ss.str() \
154  << std::endl; \
155  std::cout << "\n\t|- " << "In function: " << SIMPLE_CURRENT_FUNCTION \
156  << "\n\t+ In file: " << __FILE__ << ":" << __LINE__ << std::endl; \
157  }
158 
159 #endif
void assertion_failed_msg(char const *expr, char const *msg, char const *function, char const *file, long line)
#define SIMPLE_ASSERT_MSG_OSTREAM