contract.hh

Go to the documentation of this file.
00001 // contract.hh: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004, 2005 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_MISC_CONTRACT_HH
00018 # define VCSN_MISC_CONTRACT_HH
00019 
00025 # ifndef VCSN_NDEBUG
00026 #  include <vaucanson/config/system.hh>
00027 #  include <vaucanson/misc/static.hh>
00028 
00029 #  include <iostream>
00030 #  include <string>
00031 
00032 #  ifdef EXCEPTION_TRAPS
00033 #   include <sstream>
00034 #   include <stdexcept>
00035 #  else // ! EXCEPTION_TRAPS
00036 #   include <cstdlib>
00037 #  endif // EXCEPTION_TRAP
00038 
00039 namespace utility {
00040   namespace contract {
00041 
00118 
00119     static inline
00120     void trap(const char *file, int line,
00121               const char *location,
00122               const std::string& message)
00123     {
00124 #  ifdef EXCEPTION_TRAPS
00125       std::ostringstream os;
00126       os << file << ':' << line << ':'
00127          << (location ? location : "")
00128          << (location ? ": " : " ")
00129          << message;
00130       throw std::logic_error(os.str());
00131 #  else // ! EXCEPTION_TRAPS
00132       std::cerr << file << ':' << line << ':'
00133                 << (location ? location : "")
00134                 << std::endl
00135                 << '\t' << message
00136                 << std::endl;
00137       abort();
00138 #  endif // EXCEPTION_TRAPS
00139     }
00140 
00141     template<typename T>
00142     struct fail;
00143   }
00144 }
00145 
00146 #  define vcsn_trap_(Message, Cond) \
00147    utility::contract::trap(__FILE__, __LINE__, PRETTY_FUNCTION(), \
00148                            std::string(Message) + ": " #Cond)
00149 #  define vcsn_trap__(Message, Cond, Explanation)                                 \
00150    utility::contract::trap(__FILE__, __LINE__, PRETTY_FUNCTION(), \
00151                            std::string(Message) + ": " #Cond " // " + Explanation)
00152 #  define vcsn_trap_2(Message1, Message2) \
00153    utility::contract::trap(__FILE__, __LINE__, PRETTY_FUNCTION(), \
00154                            std::string(Message1) + ": " + Message2)
00155 
00156 #  define assertion(Cond) static_cast<void>((Cond) ? static_cast<void>(0) : vcsn_trap_("Assertion failed", Cond))
00157 #  define precondition(Cond) static_cast<void>((Cond) ? static_cast<void>(0) : vcsn_trap_("Precondition failed", Cond))
00158 #  define postcondition(Cond) static_cast<void>((Cond) ? static_cast<void>(0) : vcsn_trap_("Postcondition failed", Cond))
00159 
00160 #  define unreachable(Explanation) vcsn_trap_2("Unreachable code reached", Explanation)
00161 
00162 #  define assertion_(Cond, Explanation_if_false) static_cast<void>((Cond) ? static_cast<void>(0) : vcsn_trap__("Assertion failed", Cond, Explanation_if_false))
00163 #  define precondition_(Cond, Explanation_if_false) static_cast<void>((Cond) ? static_cast<void>(0) : vcsn_trap__("Precondition failed", Cond, Explanation_if_false))
00164 #  define postcondition_(Cond, Explanation_if_false) static_cast<void>((Cond) ? static_cast<void>(0) : vcsn_trap__("Postcondition failed", Cond, Explanation_if_false))
00165 
00166 #  define result_not_computable_if(Cond) static_cast<void>((Cond) ? vcsn_trap_("Result is not computable", Cond) : static_cast<void>(0))
00167 #  define result_not_computable(Message) vcsn_trap_2("Result is not computable", Message)
00168 
00169 #  define pure_service_call(Service) vcsn_trap_("Pure absract service called", Service)
00170 
00171 #  define static_assertion(Cond, Message) \
00172   { utility::static_if<Cond, int, utility::contract::fail<void> >::t Message; Message = 0; }
00173 #  define static_assertion_(Cond, Message) \
00174   { typename utility::static_if<Cond, int, utility::contract::fail<void> >::t Message; Message = 0; }
00175 
00176 #  define static_error(Message)                 \
00177 {                                               \
00178   struct Message;                               \
00179   utility::contract::fail<Message> Message;     \
00180 }
00181 
00182 #  ifndef INTERNAL_CHECKS
00183 
00184 #   define recommendation(Cond) static_cast<void>(0)
00185 #   define deprecation(Feature) static_cast<void>(0)
00186 #   define weakness(Feature) static_cast<void>(0)
00187 #   define incompletion(Feature) static_cast<void>(0)
00188 
00189 #   define warning(Message) static_cast<void>(0)
00190 
00191 #  else // ! INTERNAL_CHECKS
00192 
00193 #   ifdef STRICT
00194 #    define __inconsistency(Message1, Message2) vcsn_trap_2(Message1, Message2)
00195 #   else // ! STRICT
00196 #    define __inconsistency(Message1, Message2) \
00197   static_cast<void>(std::cerr << __FILE__ << ':' << __LINE__ << ": " \
00198    << Message1 << ": " << Message2 << std::endl)
00199 #   endif // STRICT
00200 
00201 #   define recommendation(Cond) \
00202       static_cast<void>((Cond) ? \
00203         static_cast<void>(0) : \
00204         __inconsistency("Recommendation", #Cond " Failed."))
00205 #   define deprecation(Feature) __inconsistency("Deprecated feature", Feature)
00206 #   define weakness(Feature) __inconsistency("Weak feature", Feature)
00207 #   define incompletion(Feature) __inconsistency("Incomplete implementation", Feature)
00208 #   define warning(Message) __inconsistency("Warning", Message)
00209 
00210 #  endif // INTERNAL_CHECKS
00211 
00212 # else // VCSN_NDEBUG
00213 
00214 #  define static_assertion(Cond, Message) typedef void Message
00215 #  define static_assertion_(Cond, Message) typedef void Message
00216 #  define static_error(Message) typedef void Message
00217 
00218 #  define assertion(Cond) static_cast<void>(0)
00219 #  define precondition(Cond) static_cast<void>(0)
00220 #  define postcondition(Cond) static_cast<void>(0)
00221 #  define assertion_(Cond, Explanation_if_false) static_cast<void>(0)
00222 #  define precondition_(Cond, Explanation_if_false) static_cast<void>(0)
00223 #  define postcondition_(Cond, Explanation_if_false) static_cast<void>(0)
00224 
00225 #  define unreachable(Explanation) static_cast<void>(0)
00226 
00227 #  define result_not_computable_if(Cond) static_cast<void>(0)
00228 #  define result_not_computable(Message) static_cast<void>(0)
00229 
00230 #  define pure_service_call(Service) static_cast<void>(0)
00231 
00232 #  define recommendation(Cond) static_cast<void>(0)
00233 #  define deprecation(Feature) static_cast<void>(0)
00234 #  define weakness(Feature) static_cast<void>(0)
00235 #  define incompletion(Feature) static_cast<void>(0)
00236 
00237 #  define warning(Message) static_cast<void>(0)
00238 
00239 # endif // ! VCSN_NDEBUG
00240 
00241 #endif // ! VCSN_MISC_CONTRACT_HH

Generated on Fri Jul 28 12:18:30 2006 for Vaucanson by  doxygen 1.4.6