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, 2006 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 vcsn {
00040   namespace misc {
00041     namespace contract {
00042 
00119 
00120       static inline
00121       void trap (const char *file, int line,
00122                  const char *location,
00123                  const std::string& message)
00124       {
00125 #  ifdef EXCEPTION_TRAPS
00126         std::ostringstream os;
00127         os << file << ':' << line << ':'
00128            << (location ? location : "")
00129            << (location ? ": " : " ")
00130            << message;
00131         throw std::logic_error (os.str ());
00132 #  else // ! EXCEPTION_TRAPS
00133         std::cerr << file << ':' << line << ':'
00134                   << (location ? location : "")
00135                   << std::endl
00136                   << '\t' << message
00137                   << std::endl;
00138         abort ();
00139 #  endif // EXCEPTION_TRAPS
00140       }
00141 
00142       template<typename T>
00143       struct fail;
00144     }
00145   }
00146 }
00147 
00148 #  define vcsn_trap_(Message, Cond)                                     \
00149   vcsn::misc::contract::trap (__FILE__, __LINE__, PRETTY_FUNCTION (),   \
00150                               std::string (Message) + ": " #Cond)
00151 #  define vcsn_trap__(Message, Cond, Explanation)                       \
00152   vcsn::misc::contract::trap (__FILE__, __LINE__, PRETTY_FUNCTION (),   \
00153                               std::string (Message) + ": " #Cond " // " + Explanation)
00154 #  define vcsn_trap_2(Message1, Message2)                               \
00155   vcsn::misc::contract::trap (__FILE__, __LINE__, PRETTY_FUNCTION (),   \
00156                               std::string (Message1) + ": " + Message2)
00157 
00158 #  define assertion(Cond) static_cast<void> ((Cond) ? static_cast<void> (0) : vcsn_trap_ ("Assertion failed", Cond))
00159 #  define precondition(Cond) static_cast<void> ((Cond) ? static_cast<void> (0) : vcsn_trap_ ("Precondition failed", Cond))
00160 #  define postcondition(Cond) static_cast<void> ((Cond) ? static_cast<void> (0) : vcsn_trap_ ("Postcondition failed", Cond))
00161 
00162 #  define unreachable(Explanation) vcsn_trap_2 ("Unreachable code reached", Explanation)
00163 
00164 #  define assertion_(Cond, Explanation_if_false) static_cast<void> ((Cond) ? static_cast<void> (0) : vcsn_trap__ ("Assertion failed", Cond, Explanation_if_false))
00165 #  define precondition_(Cond, Explanation_if_false) static_cast<void> ((Cond) ? static_cast<void> (0) : vcsn_trap__ ("Precondition failed", Cond, Explanation_if_false))
00166 #  define postcondition_(Cond, Explanation_if_false) static_cast<void> ((Cond) ? static_cast<void> (0) : vcsn_trap__ ("Postcondition failed", Cond, Explanation_if_false))
00167 
00168 #  define result_not_computable_if(Cond) static_cast<void> ((Cond) ? vcsn_trap_ ("Result is not computable", Cond) : static_cast<void> (0))
00169 #  define result_not_computable(Message) vcsn_trap_2 ("Result is not computable", Message)
00170 
00171 #  define pure_service_call(Service) vcsn_trap_ ("Pure absract service called", Service)
00172 
00173 #  define static_assertion(Cond, Message)                               \
00174   { vcsn::misc::static_if<Cond, int, vcsn::misc::contract::fail<void> >::t Message; Message = 0; }
00175 #  define static_assertion_(Cond, Message)                              \
00176   { typename vcsn::misc::static_if<Cond, int, vcsn::misc::contract::fail<void> >::t Message; Message = 0; }
00177 
00178 #  define static_error(Message)                 \
00179   {                                                     \
00180     struct Message;                                     \
00181     vcsn::misc::contract::fail<Message> Message;        \
00182   }
00183 
00184 #  ifndef INTERNAL_CHECKS
00185 
00186 #   define recommendation(Cond) static_cast<void> (0)
00187 #   define deprecation(Feature) static_cast<void> (0)
00188 #   define weakness(Feature) static_cast<void> (0)
00189 #   define incompletion(Feature) static_cast<void> (0)
00190 
00191 #   define warning(Message) static_cast<void> (0)
00192 
00193 #  else // ! INTERNAL_CHECKS
00194 
00195 #   ifdef STRICT
00196 #    define __inconsistency(Message1, Message2) vcsn_trap_2 (Message1, Message2)
00197 #   else // ! STRICT
00198 #    define __inconsistency(Message1, Message2)                 \
00199   static_cast<void> (std::cerr << __FILE__ << ':' << __LINE__ << ": "   \
00200                      << Message1 << ": " << Message2 << std::endl)
00201 #   endif // STRICT
00202 
00203 #   define recommendation(Cond)                                 \
00204   static_cast<void> ((Cond) ?                                           \
00205                      static_cast<void> (0) :                            \
00206                      __inconsistency ("Recommendation", #Cond " Failed."))
00207 #   define deprecation(Feature) __inconsistency ("Deprecated feature", Feature)
00208 #   define weakness(Feature) __inconsistency ("Weak feature", Feature)
00209 #   define incompletion(Feature) __inconsistency ("Incomplete implementation", Feature)
00210 #   define warning(Message) __inconsistency ("Warning", Message)
00211 
00212 #  endif // INTERNAL_CHECKS
00213 
00214 # else // VCSN_NDEBUG
00215 
00216 #  define static_assertion(Cond, Message) typedef void Message
00217 #  define static_assertion_(Cond, Message) typedef void Message
00218 #  define static_error(Message) typedef void Message
00219 
00220 #  define assertion(Cond) static_cast<void> (0)
00221 #  define precondition(Cond) static_cast<void> (0)
00222 #  define postcondition(Cond) static_cast<void> (0)
00223 #  define assertion_(Cond, Explanation_if_false) static_cast<void> (0)
00224 #  define precondition_(Cond, Explanation_if_false) static_cast<void> (0)
00225 #  define postcondition_(Cond, Explanation_if_false) static_cast<void> (0)
00226 
00227 #  define unreachable(Explanation) static_cast<void> (0)
00228 
00229 #  define result_not_computable_if(Cond) static_cast<void> (0)
00230 #  define result_not_computable(Message) static_cast<void> (0)
00231 
00232 #  define pure_service_call(Service) static_cast<void> (0)
00233 
00234 #  define recommendation(Cond) static_cast<void> (0)
00235 #  define deprecation(Feature) static_cast<void> (0)
00236 #  define weakness(Feature) static_cast<void> (0)
00237 #  define incompletion(Feature) static_cast<void> (0)
00238 
00239 #  define warning(Message) static_cast<void> (0)
00240 
00241 # endif // ! VCSN_NDEBUG
00242 
00243 #endif // ! VCSN_MISC_CONTRACT_HH

Generated on Sat Jul 29 17:12:58 2006 for Vaucanson by  doxygen 1.4.6