OpenWalnut  1.4.0
WPreconditionNotMet.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
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 
00034 
00035 /**
00036  * An exception that gets thrown when preconditions of a function are not met.
00037  */
00038 class WPreconditionNotMet : public WException
00039 {
00040 public:
00041     /**
00042      * Constructor.
00043      *
00044      * \param msg The message.
00045      */
00046     explicit WPreconditionNotMet( std::string const& msg );
00047 
00048     /**
00049      * Destructor.
00050      */
00051     virtual ~WPreconditionNotMet() throw();
00052 
00053 protected:
00054 private:
00055 };
00056 
00057 namespace utility
00058 {
00059 /**
00060  * Throws a WPreconditionNotMet.
00061  *
00062  * \param expr The expression that failed.
00063  * \param msg A message.
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  * Check if two values differ.
00078  *
00079  * \tparam T1 The type of the first value.
00080  * \tparam T2 The type of the second value.
00081  * \param expr1 The first expression.
00082  * \param expr2 The second expression.
00083  * \param value1 The first value.
00084  * \param value2 The second value.
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  * Check if two values are equal.
00101  *
00102  * \tparam T1 The type of the first value.
00103  * \tparam T2 The type of the second value.
00104  * \param expr1 The first expression.
00105  * \param expr2 The second expression.
00106  * \param value1 The first value.
00107  * \param value2 The second value.
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  * Check if a value is smaller than another value.
00124  *
00125  * \tparam T1 The type of the first value.
00126  * \tparam T2 The type of the second value.
00127  * \param expr1 The first expression.
00128  * \param expr2 The second expression.
00129  * \param value1 The first value.
00130  * \param value2 The second value.
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 }  // namespace utility
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