OpenWalnut  1.5.0dev
WPreconditionNotMet.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WPRECONDITIONNOTMET_H
26 #define WPRECONDITIONNOTMET_H
27 
28 #include <string>
29 #include <sstream>
30 #include <utility>
31 
32 #include "../WException.h"
33 
34 
35 /**
36  * An exception that gets thrown when preconditions of a function are not met.
37  */
39 {
40 public:
41  /**
42  * Constructor.
43  *
44  * \param msg The message.
45  */
46  explicit WPreconditionNotMet( std::string const& msg );
47 
48  /**
49  * Destructor.
50  */
51  virtual ~WPreconditionNotMet() throw();
52 
53 protected:
54 private:
55 };
56 
57 namespace utility
58 {
59 /**
60  * Throws a WPreconditionNotMet.
61  *
62  * \param expr The expression that failed.
63  * \param msg A message.
64  */
65 inline void __WPrecondImpl( std::string const& expr, std::string const& msg )
66 {
67  std::stringstream s;
68  s << "Function precondition not met: " << expr;
69  if( !msg.empty() )
70  {
71  s << "\n" << msg;
72  }
73  throw WPreconditionNotMet( s.str() );
74 }
75 
76 /**
77  * Check if two values differ.
78  *
79  * \tparam T1 The type of the first value.
80  * \tparam T2 The type of the second value.
81  * \param expr1 The first expression.
82  * \param expr2 The second expression.
83  * \param value1 The first value.
84  * \param value2 The second value.
85  */
86 template< typename T1, typename T2 >
87 void __WPrecondDiffersImpl( std::string const& expr1, std::string const& expr2, T1 const& value1, T2 const& value2 )
88 {
89  if( value1 == value2 )
90  {
91  std::stringstream s;
92  s << "Function precondition not met: \n";
93  s << "Expected " << expr1 << " to differ from " << expr2 << ", yet " << value1
94  << " == " << value2;
95  throw WPreconditionNotMet( s.str() );
96  }
97 }
98 
99 /**
100  * Check if two values are equal.
101  *
102  * \tparam T1 The type of the first value.
103  * \tparam T2 The type of the second value.
104  * \param expr1 The first expression.
105  * \param expr2 The second expression.
106  * \param value1 The first value.
107  * \param value2 The second value.
108  */
109 template< typename T1, typename T2 >
110 void __WPrecondEqualsImpl( std::string const& expr1, std::string const& expr2, T1 const& value1, T2 const& value2 )
111 {
112  if( !( value1 == value2 ) )
113  {
114  std::stringstream s;
115  s << "Function precondition not met: \n";
116  s << "Expected " << expr1 << " to be equal to " << expr2 << ", yet " << value1
117  << " != " << value2;
118  throw WPreconditionNotMet( s.str() );
119  }
120 }
121 
122 /**
123  * Check if a value is smaller than another value.
124  *
125  * \tparam T1 The type of the first value.
126  * \tparam T2 The type of the second value.
127  * \param expr1 The first expression.
128  * \param expr2 The second expression.
129  * \param value1 The first value.
130  * \param value2 The second value.
131  */
132 template< typename T1, typename T2 >
133 void __WPrecondLessImpl( std::string const& expr1, std::string const& expr2, T1 const& value1, T2 const& value2 )
134 {
135  if( !( value1 < value2 ) )
136  {
137  std::stringstream s;
138  s << "Function precondition not met: \n";
139  s << "Expected " << expr1 << " to be smaller than " << expr2 << ", yet " << value1
140  << " => " << value2;
141  throw WPreconditionNotMet( s.str() );
142  }
143 }
144 
145 } // namespace utility
146 
147 #define WPrecond( expr, msg ) ( ( expr ) ? ( ( void )0 ) : ( ::utility::__WPrecondImpl( #expr, msg ) ) )
148 
149 #define WPrecondDiffers( expr1, expr2 ) ( ::utility::__WPrecondDiffersImpl( #expr1, #expr2, ( expr1 ), ( expr2 ) ) ) // NOLINT
150 
151 #define WPrecondEquals( expr1, expr2 ) ( ::utility::__WPrecondEqualsImpl( #expr1, #expr2, ( expr1 ), ( expr2 ) ) ) // NOLINT
152 
153 #define WPrecondLess( expr1, expr2 ) ( ::utility::__WPrecondLessImpl( #expr1, #expr2, ( expr1 ), ( expr2 ) ) ) // NOLINT
154 
155 #endif // WPRECONDITIONNOTMET_H
Basic exception handler.
Definition: WException.h:39
An exception that gets thrown when preconditions of a function are not met.
WPreconditionNotMet(std::string const &msg)
Constructor.
virtual ~WPreconditionNotMet()
Destructor.