DataTransferKit - Multiphysics Solution Transfer Services  2.0
DTK_DBC.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 /*
3  Copyright (c) 2012, Stuart R. Slattery
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are
8  met:
9 
10  *: Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12 
13  *: Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in the
15  documentation and/or other materials provided with the distribution.
16 
17  *: Neither the name of the University of Wisconsin - Madison nor the
18  names of its contributors may be used to endorse or promote products
19  derived from this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 //---------------------------------------------------------------------------//
39 //---------------------------------------------------------------------------//
40 
41 #ifndef DTK_DBC_HPP
42 #define DTK_DBC_HPP
43 
44 #include <stdexcept>
45 #include <string>
46 
47 #include "DataTransferKitUtils_config.hpp"
48 
49 namespace DataTransferKit
50 {
51 //---------------------------------------------------------------------------//
58 //---------------------------------------------------------------------------//
59 class DataTransferKitException : public std::logic_error
60 {
61  public:
67  DataTransferKitException( const std::string &msg )
68  : std::logic_error( msg )
69  { /* ... */
70  }
71 
82  DataTransferKitException( const std::string &cond, const std::string &file,
83  const int line )
84  : std::logic_error( generate_output( cond, file, line ) )
85  { /* ... */
86  }
87 
89  virtual ~DataTransferKitException() throw() { /* ... */}
90 
91  private:
92  // Build an assertion output from advanced constructor arguments.
93  std::string generate_output( const std::string &cond,
94  const std::string &file,
95  const int line ) const;
96 };
97 
98 //---------------------------------------------------------------------------//
99 // Throw functions.
100 //---------------------------------------------------------------------------//
101 // Throw a DataTransferKit::DataTransferKitException.
102 void throwDataTransferKitException( const std::string &cond,
103  const std::string &file, const int line );
104 
105 // Throw an assertion based on an error code failure.
106 void errorCodeFailure( const std::string &cond, const std::string &file,
107  const int line, const int error_code );
108 
109 //---------------------------------------------------------------------------//
110 
111 } // end namespace DataTransferKit
112 
113 //---------------------------------------------------------------------------//
114 // Design-by-Contract macros.
115 //---------------------------------------------------------------------------//
142 #if HAVE_DTK_DBC
143 
144 #define DTK_REQUIRE( c ) \
145  if ( !( c ) ) \
146  DataTransferKit::throwDataTransferKitException( #c, __FILE__, __LINE__ )
147 #define DTK_ENSURE( c ) \
148  if ( !( c ) ) \
149  DataTransferKit::throwDataTransferKitException( #c, __FILE__, __LINE__ )
150 #define DTK_CHECK( c ) \
151  if ( !( c ) ) \
152  DataTransferKit::throwDataTransferKitException( #c, __FILE__, __LINE__ )
153 #define DTK_REMEMBER( c ) c
154 #define DTK_CHECK_ERROR_CODE( c ) \
155  do \
156  { \
157  int ec = c; \
158  if ( 0 != ec ) \
159  DataTransferKit::errorCodeFailure( #c, __FILE__, __LINE__, ec ); \
160  } while ( 0 )
161 
162 #else
163 
164 #define DTK_REQUIRE( c )
165 #define DTK_ENSURE( c )
166 #define DTK_CHECK( c )
167 #define DTK_REMEMBER( c )
168 #define DTK_CHECK_ERROR_CODE( c ) c
169 #endif
170 
171 #define DTK_INSIST( c ) \
172  if ( !( c ) ) \
173  DataTransferKit::throwDataTransferKitException( #c, __FILE__, __LINE__ )
174 
175 //---------------------------------------------------------------------------//
176 
177 #endif // end DTK_DBC_HPP
178 
179 //---------------------------------------------------------------------------//
180 // end DTK_DBC.hpp
181 //---------------------------------------------------------------------------//
Base class for DTK assertions. This structure is heavily based on that in Nemesis developed by Tom Ev...
Definition: DTK_DBC.hpp:59
DataTransferKitException(const std::string &msg)
Default constructor.
Definition: DTK_DBC.hpp:67
void throwDataTransferKitException(const std::string &cond, const std::string &file, const int line)
Throw a DataTransferKit::DataTransferKitException.
Definition: DTK_DBC.cpp:87
virtual ~DataTransferKitException()
Destructor.
Definition: DTK_DBC.hpp:89
DataTransferKitException(const std::string &cond, const std::string &file, const int line)
DBC constructor.
Definition: DTK_DBC.hpp:82
DTK_BasicEntitySet.cpp.
void errorCodeFailure(const std::string &cond, const std::string &file, const int line, const int error_code)
Throw a DataTransferKit::DataTransferKitException when an error code fails.
Definition: DTK_DBC.cpp:112