TiledArray  0.7.0
error.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef TILEDARRAY_ERROR_H__INCLUDED
21 #define TILEDARRAY_ERROR_H__INCLUDED
22 
23 #include <TiledArray/config.h>
24 
25 
26 // Check for default error checking method, which is determined by TA_DEFAULT_ERROR,
27 // which is defined in TiledArray/config.h.
28 #ifdef TA_DEFAULT_ERROR
29 # if !defined(TA_EXCEPTION_ERROR) && !defined(TA_ASSERT_ERROR) && !defined(TA_NO_ERROR) && !defined(TA_ABORT_ERROR)
30 # if TA_DEFAULT_ERROR == 0
31 # define TA_NO_ERROR
32 # elif TA_DEFAULT_ERROR == 1
33 # define TA_EXCEPTION_ERROR
34 # elif TA_DEFAULT_ERROR == 2
35 # define TA_ASSERT_ERROR
36 # elif TA_DEFAULT_ERROR == 3
37 # define TA_ABORT_ERROR
38 # endif // TA_DEFAULT_ERROR == ?
39 # endif // !defined(TA_EXCEPTION_ERROR) && !defined(TA_ASSERT_ERROR) && !defined(TA_NO_ERROR) && !defined(TA_ABORT_ERROR)
40 #endif // TA_DEFAULT_ERROR
41 
42 #if __cplusplus > 199711L
43 // C++11
44 #define TILEDARRAY_NO_EXCEPTION noexcept
45 #else
46 #define TILEDARRAY_NO_EXCEPTION throw()
47 #endif
48 
49 #include <exception>
50 namespace TiledArray {
51 
52  class Exception : public std::exception {
53  public:
54  Exception(const char* m) : message_(m) { }
55 
56  virtual const char* what() const TILEDARRAY_NO_EXCEPTION { return message_; }
57 
58  private:
59  const char* message_;
60  }; // class Exception
61 
63  inline void exception_break() { }
64 } // namespace TiledArray
65 
66 
67 #define TA_STRINGIZE( s ) #s
68 
69 #define TA_EXCEPTION_MESSAGE( file , line , mess ) \
70  "TiledArray: exception at " file "(" TA_STRINGIZE( line ) "): " mess
71 
72 #define TA_EXCEPTION( m ) \
73  { \
74  TiledArray::exception_break(); \
75  throw TiledArray::Exception ( TA_EXCEPTION_MESSAGE( __FILE__ , __LINE__ , m ) ); \
76  }
77 
78 #ifdef TA_EXCEPTION_ERROR
79 // This section defines the behavior for TiledArray assertion error checking
80 // which will throw exceptions.
81 #ifdef TA_ASSERT_ERROR
82 #undef TA_ASSERT_ERROR
83 #endif
84 #ifdef TA_ABORT_ERROR
85 #undef TA_ABORT_ERROR
86 #endif
87 
88 #define TA_ASSERT( a ) if(! ( a ) ) TA_EXCEPTION( "assertion failure" )
89 #define TA_TEST( a ) TA_ASSERT( a )
90 
91 #elif defined(TA_ASSERT_ERROR)
92 // This sections defines behavior for TiledArray assertion error checking which
93 // uses assertions.
94 #include <cassert>
95 #define TA_ASSERT( a ) assert( a )
96 #define TA_TEST( a ) TA_ASSERT( a )
97 #elif defined(TA_ABORT_ERROR)
98 // This sections defines behavior for TiledArray assertion error checking which
99 // calls std::abort
100 #include <cstdlib>
101 #define TA_ASSERT( a ) if(! ( a ) ) std::abort();
102 #define TA_TEST( a ) TA_ASSERT( a )
103 #else
104 // This section defines behavior for TiledArray assertion error checking which
105 // does no error checking.
106 // WARNING: TiledArray will perform no error checking.
107 #define TA_ASSERT( a ) { ; }
108 #define TA_TEST( a ) a
109 
110 #endif //TA_EXCEPTION_ERROR
111 
112 #define TA_CHECK( a ) if(! ( a ) ) TA_EXCEPTION( "check failure" )
113 
114 #ifdef TILEDARRAY_NO_USER_ERROR_MESSAGES
115 #define TA_USER_ERROR_MESSAGE( m )
116 #else
117 #include <iostream>
118 #define TA_USER_ERROR_MESSAGE( m ) std::cerr << "!! ERROR TiledArray: " << m << "\n";
119 #endif // TILEDARRAY_NO_USER_ERROR_MESSAGES
120 
121 #ifndef NDEBUG
122 // User interface assertion
123 #define TA_USER_ASSERT( a , m ) \
124  if(! ( a ) ) \
125  { \
126  TA_USER_ERROR_MESSAGE( m ) \
127  TiledArray::exception_break(); \
128  throw TiledArray::Exception( m ); \
129  }
130 
131 #else
132 
133 // Disable user interface assertion when NDEBUG is defined
134 #define TA_USER_ASSERT( a , m )
135 #define TA_USER_ASSERT_DISABLED 1
136 
137 #endif
138 
139 // mark functions as deprecated using this macro
140 // will result in a warning
141 #ifndef DEPRECATED // avoid clashing with previous definitions
142 #if __cplusplus >= 201402L
143 #define DEPRECATED [[deprecated]]
144 #elif defined(__GNUC__)
145 #define DEPRECATED __attribute__((deprecated))
146 #else
147 #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
148 #define DEPRECATED
149 #endif
150 #endif // not defined(DEPRECATED)
151 
152 // same as DEPRECATED, but annotated with a message
153 // will result in a warning
154 #ifndef DEPRECATEDMSG // avoid clashing with previous definitions
155 #if __cplusplus >= 201402L
156 #define DEPRECATEDMSG(msg) [[deprecated(msg)]]
157 #elif defined(__GNUC__)
158 #define DEPRECATEDMSG(msg) __attribute__((deprecated(msg)))
159 #else
160 #pragma message("WARNING: You need to implement DEPRECATEDMSG for this compiler")
161 #define DEPRECATEDMSG(msg)
162 #endif
163 #endif // not defined(DEPRECATEDMSG)
164 
165 #endif // TILEDARRAY_ERROR_H__INCLUDED
virtual const char * what() const TILEDARRAY_NO_EXCEPTION
Definition: error.h:56
Exception(const char *m)
Definition: error.h:54
void exception_break()
Place a break point on this function to stop before TiledArray exceptions are thrown.
Definition: error.h:63
#define TILEDARRAY_NO_EXCEPTION
Definition: error.h:46