contract.hh

00001 // Copyright (C) 2001, 2002, 2003  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef METALIC_CONTRACT_HH
00029 # define METALIC_CONTRACT_HH
00030 
00031 # include <mlc/config/system.hh>
00032 
00033 # ifdef NDEBUG
00034 
00035 #  define assertion(expr)         ((void) 0)
00036 #  define invariant(expr)         ((void) 0)
00037 #  define precondition(expr)      ((void) 0)
00038 #  define postcondition(expr)     ((void) 0)
00039 
00040 # else // NDEBUG
00041 
00042 #  include <iostream>
00043 #  include <cstdlib>
00044 #  include <sstream>
00045 #  include <stdexcept>
00046 #  include <cassert>
00047 
00048 namespace mlc 
00049 {
00050 
00051 #  ifdef OLN_EXCEPTIONS
00052 
00053   // FIXME: this fails if several sources files are compiled.
00054   // The code on this function should go in a .so file.
00055 
00056   // FIXME: this fails if several sources files are compiled.
00057 
00058   inline void FailedCondition__( const char* condType,
00059                                  const char* condText,
00060                                  const char* fileName 
00061 #   ifndef RUNNING_TESTS
00062                                  ,int fileLine
00063 #   endif
00064                                  )
00065   {
00066     // put a breakpoint _here_ at debug-time
00067     std::ostringstream err;
00068 
00069     err << fileName << ':'
00070 #   ifndef RUNNING_TESTS
00071         << fileLine
00072 #   endif
00073         << ": "
00074             << condType << " `"
00075         << condText << "' failed.";
00076 #   ifndef RUNNING_TESTS
00077     throw std::runtime_error(err.str());
00078 #   else
00079     std::cerr << err.str();
00080     exit(1);
00081 #   endif
00082   }
00083 
00084 #  else // no OLN_EXCEPTIONS
00085 
00086   inline void FailedCondition__( const char* condType,
00087                                  const char* condText,
00088                                  const char* fileName
00089 #   ifndef RUNNING_TESTS
00090                                  ,int fileLine
00091 #   endif
00092                                  )
00093 {
00094   // put a breakpoint _here_ at debug-time
00095   std::cerr << fileName << ':'
00096 #   ifndef RUNNING_TESTS
00097             << fileLine
00098 #   endif
00099             << ": "
00100             << condType << " `"
00101             << condText << "' failed." << std::endl;
00102 #   ifndef RUNNING_TESTS
00103   abort();
00104 #   else
00105   exit(1);
00106 #   endif
00107 }
00108 
00109 #  endif // OLN_EXCEPTIONS
00110 
00111 } // end of mlc
00112 
00113 #  ifndef RUNNING_TESTS
00114 #   define TestCondition__(condType,expr) \
00115   ((void) ((expr) ? 0 : (::mlc::FailedCondition__( #condType, #expr, \
00116                                                    __FILE__, __LINE__ ), 0)))
00117 #  else
00118 #   define TestCondition__(condType,expr) \
00119   ((void) ((expr) ? 0 : (::mlc::FailedCondition__( #condType, #expr, \
00120                                                    __FILE__ ), 0)))
00121 #  endif
00122 
00123 // FIXME: assertions should use extern functions defined in a
00124 // separated object file. This saves a LOT of compilation
00125 // time. assert() is used temporarily to fix this problem.
00126 
00127 #  ifdef OLN_EXCEPTIONS
00128 #   define assertion(expr)         TestCondition__(Assertion,expr)
00129 #   define invariant(expr)         TestCondition__(Invariant,expr)
00130 #   define precondition(expr)      TestCondition__(Precondition,expr)
00131 #   define postcondition(expr)     TestCondition__(Postcondition,expr)
00132 #  else
00133 #   define assertion(expr)         assert(expr)
00134 #   define invariant(expr)         assert(expr)
00135 #   define precondition(expr)      assert(expr)
00136 #   define postcondition(expr)     assert(expr)
00137 #  endif
00138 
00139 # endif // NDEBUG
00140 
00141 #endif // ! METALIC_CONTRACT_HH

Generated on Thu Apr 15 20:13:07 2004 for Olena by doxygen 1.3.6-20040222