00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef WPRECONDITIONNOTMET_H
00026 #define WPRECONDITIONNOTMET_H
00027
00028 #include <string>
00029 #include <sstream>
00030 #include <utility>
00031
00032 #include "../WException.h"
00033 #include "../WExportCommon.h"
00034
00035
00036
00037
00038 class OWCOMMON_EXPORT WPreconditionNotMet : public WException
00039 {
00040 public:
00041
00042
00043
00044
00045
00046 explicit WPreconditionNotMet( std::string const& msg );
00047
00048
00049
00050
00051 virtual ~WPreconditionNotMet() throw();
00052
00053 protected:
00054 private:
00055 };
00056
00057 namespace utility
00058 {
00059
00060
00061
00062
00063
00064
00065 inline void __WPrecondImpl( std::string const& expr, std::string const& msg )
00066 {
00067 std::stringstream s;
00068 s << "Function precondition not met: " << expr;
00069 if( !msg.empty() )
00070 {
00071 s << "\n" << msg;
00072 }
00073 throw WPreconditionNotMet( s.str() );
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 template< typename T1, typename T2 >
00087 void __WPrecondDiffersImpl( std::string const& expr1, std::string const& expr2, T1 const& value1, T2 const& value2 )
00088 {
00089 if( value1 == value2 )
00090 {
00091 std::stringstream s;
00092 s << "Function precondition not met: \n";
00093 s << "Expected " << expr1 << " to differ from " << expr2 << ", yet " << value1
00094 << " == " << value2;
00095 throw WPreconditionNotMet( s.str() );
00096 }
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 template< typename T1, typename T2 >
00110 void __WPrecondEqualsImpl( std::string const& expr1, std::string const& expr2, T1 const& value1, T2 const& value2 )
00111 {
00112 if( !( value1 == value2 ) )
00113 {
00114 std::stringstream s;
00115 s << "Function precondition not met: \n";
00116 s << "Expected " << expr1 << " to be equal to " << expr2 << ", yet " << value1
00117 << " != " << value2;
00118 throw WPreconditionNotMet( s.str() );
00119 }
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 template< typename T1, typename T2 >
00133 void __WPrecondLessImpl( std::string const& expr1, std::string const& expr2, T1 const& value1, T2 const& value2 )
00134 {
00135 if( !( value1 < value2 ) )
00136 {
00137 std::stringstream s;
00138 s << "Function precondition not met: \n";
00139 s << "Expected " << expr1 << " to be smaller than " << expr2 << ", yet " << value1
00140 << " => " << value2;
00141 throw WPreconditionNotMet( s.str() );
00142 }
00143 }
00144
00145 }
00146
00147 #define WPrecond( expr, msg ) ( ( expr ) ? ( ( void )0 ) : ( ::utility::__WPrecondImpl( #expr, msg ) ) )
00148
00149 #define WPrecondDiffers( expr1, expr2 ) ( ::utility::__WPrecondDiffersImpl( #expr1, #expr2, ( expr1 ), ( expr2 ) ) ) // NOLINT
00150
00151 #define WPrecondEquals( expr1, expr2 ) ( ::utility::__WPrecondEqualsImpl( #expr1, #expr2, ( expr1 ), ( expr2 ) ) ) // NOLINT
00152
00153 #define WPrecondLess( expr1, expr2 ) ( ::utility::__WPrecondLessImpl( #expr1, #expr2, ( expr1 ), ( expr2 ) ) ) // NOLINT
00154
00155 #endif // WPRECONDITIONNOTMET_H