User Manual, Developers Guide and API Documentation

Assure.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #ifndef WNS_ASSURE_HPP
00029 #define WNS_ASSURE_HPP
00030 
00031 #include <WNS/Exception.hpp>
00032 #include <WNS/TypeInfo.hpp>
00033 
00034 #include <cassert>
00035 #include <sstream>
00036 #include <iostream>
00037 #include <string>
00038 #include <csignal>
00039 
00093 // WNS_NDEBUG means: assures are disabled
00094 #if defined WNS_NDEBUG
00095 #define assure(expr, reason) \
00096 ((void)0)
00097 
00098 #define assureWithExceptionType(expr, reason, type) \
00099 ((void)0)
00100 
00101 // WNS_ASSERT means: assure behaves like assert -> abort if expression is not true
00102 #elif defined WNS_ASSERT
00103 #define assure(expr, reason) \
00104 if (true) \
00105 { \
00106     assert(expr); \
00107 } \
00108 else \
00109     void()
00110 
00111 #define assureWithExceptionType(expr, reason, type) \
00112 if (true) \
00113 { \
00114     assert(expr); \
00115 } \
00116 else \
00117     void()
00118 
00119 // if nothing is defined: assure throws an exception if expression is not true
00120 #else
00121 #define assure(expr, reason) \
00122 if (!(expr)) \
00123 { \
00124     std::stringstream s; \
00125     s.precision(10); \
00126     s<<reason; \
00127     wns::Assure::expression<wns::Assure::Exception>(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__, (s.str())); \
00128 } \
00129 else \
00130     void()
00131 
00132 #define assureWithExceptionType(expr, reason, type) \
00133 if (!(expr)) \
00134 { \
00135     std::stringstream s; \
00136     s.precision(10); \
00137     s<<reason; wns::Assure::expression<type>(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__, (s.str())); \
00138 } else \
00139     void()
00140 
00141 #define WNS_ASSURE_THROWS_EXCEPTION 1
00142 
00143 #endif // WNS_NDEBUG
00144 
00145 
00159 #define assureType(pointer, T) \
00160 assure(dynamic_cast<T>(pointer), \
00161        std::string(#pointer) + " is not of type " + wns::TypeInfo::create<T>().toString() + \
00162                    " but of Type: " + wns::TypeInfo::create(*pointer).toString()+"*")
00163 
00175 #define assureNotNull(pointer) \
00176 assureWithExceptionType(NULL != pointer, \
00177                "Pointer " + std::string(#pointer) + " invalid (NULL)", wns::NullException)
00178 
00179 namespace wns {
00180 
00186     class NullException :
00187         public wns::Exception
00188     {
00189     public:
00194         explicit
00195         NullException(const std::string& s);
00196         ~NullException() throw() {}
00197     };
00198 
00199 
00206     class Assure
00207     {
00208     public:
00215         static bool useSIGTRAP;
00216 
00222         class Exception :
00223             public wns::Exception
00224         {
00225         public:
00230             explicit
00231             Exception(const std::string& s);
00232 
00233       ~Exception() throw() {};
00234         };
00235 
00246         template<typename TYPE>
00247         static void
00248         expression(
00249             const std::string& expression,
00250             const std::string& fileName,
00251             int line,
00252             const std::string& functionName,
00253             const std::string& reason)
00254         {
00255             std::stringstream ss;
00256             ss << "In '"
00257                << functionName
00258                << "' ("
00259                << fileName
00260                << ":"
00261                << line
00262                << "):\nfailed assertion (" << expression << ")." << std::endl
00263                << "Reason: " << reason << std::endl;
00264 
00265             if (useSIGTRAP)
00266             {
00267                 std::cerr << ss.str();
00268                 raise(SIGTRAP);
00269             }
00270             else
00271             {
00272                 TYPE e(ss.str());
00273                 throw(e);
00274             }
00275         }
00276 
00277     };
00278 }
00279 
00280 #endif // WNS_ASSURE_HPP

Generated on Wed Feb 8 03:31:38 2012 for openWNS by  doxygen 1.5.5