library of assembled shared sources

http://lass.cocamware.com

enforcer.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 /** @defgroup Enforcers Enforcers
00044  *  @brief enforcers are release-time counterpart of assertions to ease condition verifying
00045  *  @author [Bramz]
00046  *
00047  *  Enforcers are a tool described by Alexandrescu [1]
00048  *  [1] ALEXANDRESCU A. & MARGINEAN P. (2003), Enforcements. June 2003, C++ Experts Forum,
00049  *  http://www.cuj.com/documents/s=8250/cujcexp2106alexandr/alexandr.htm
00050  */
00051 
00052 #ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_ENFORCER_H
00053 #define LASS_GUARDIAN_OF_INCLUSION_UTIL_ENFORCER_H
00054 
00055 #include "util_common.h"
00056 #include "impl/lass_errno.h"
00057 
00058 namespace lass
00059 {
00060 namespace util
00061 {
00062 
00063 /** Exception thrown by enforcers
00064  *  @ingroup Enforcers
00065  */
00066 class EnforceFailure: public Exception
00067 {
00068 public:
00069     EnforceFailure(const std::string& msg, const std::string& loc): Exception(msg, loc) {}
00070 private:
00071     LASS_UTIL_EXCEPTION_PRIVATE_IMPL(EnforceFailure)
00072 };
00073 
00074 }
00075 }
00076 
00077 /** Enforces the expression to be "true", by using operator!.
00078  *  @ingroup Enforcers
00079  *
00080  *  taken from:
00081  *  ALEXANDRESCU A. & MARGINEAN P. (2003), Enforcements. June 2003, C++ Experts Forum,
00082  *  http://www.cuj.com.
00083  *
00084  *  http://www.cuj.com/documents/s=8250/cujcexp2106alexandr
00085  */
00086 #define LASS_ENFORCE(expression)\
00087     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00088         ::lass::util::impl::TruePredicate,\
00089         ::lass::util::impl::DefaultRaiser,\
00090         (expression), \
00091         int(0), \
00092         "Expression '" LASS_STRINGIFY(expression) "' failed in '" LASS_HERE "'.")
00093 
00094 
00095 
00096 /** Enforces a pointer to be different than the NULL pointer.
00097  *  @ingroup Enforcers
00098  */
00099 #define LASS_ENFORCE_POINTER(pointer) \
00100     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00101         ::lass::util::impl::TruePredicate,\
00102         ::lass::util::impl::DefaultRaiser,\
00103         (pointer), \
00104         int(0), \
00105         "Null pointer '" LASS_STRINGIFY(pointer) "' detected in '" LASS_HERE "'.")
00106 
00107 
00108 
00109 /** Enforces a stream to be in the good state.
00110  *  @ingroup Enforcers
00111  */
00112 #define LASS_ENFORCE_STREAM(stream)\
00113     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00114         ::lass::util::impl::StreamPredicate,\
00115         ::lass::util::impl::DefaultRaiser,\
00116         (stream),\
00117         int(0), \
00118         "Failing stream '" LASS_STRINGIFY(stream) "' detected in '" LASS_HERE "'.") 
00119 
00120 
00121 
00122 /** Enforces the result of the expression to be zero.
00123  *  @ingroup Enforcers
00124  */
00125 #define LASS_ENFORCE_ZERO(expression) \
00126     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00127         ::lass::util::impl::EqualPredicate,\
00128         ::lass::util::impl::ZeroRaiser>(\
00129         (expression), \
00130         int(0), \
00131         "'" LASS_STRINGIFY(expression) "' in '" LASS_HERE "'.")
00132 
00133 
00134 
00135 /** Enforces the return code of a CLIB function call to be different than -1.
00136  *  @ingroup Enforcers
00137  *  Some CLIB functions return -1 on failure.  An error code indicating the failure
00138  *  can be found using errno.  LASS_ENFORCE_CLIB will enforce that the return
00139  *  value of the function call is not -1.  If it is -1, it will rais a runtime
00140  *  exception with the error code errno and its string message translated by strerror().
00141  */
00142 #define LASS_ENFORCE_CLIB(functionCall)\
00143     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00144         ::lass::util::impl::UnequalPredicate,\
00145         ::lass::util::impl::ClibRaiser,\
00146         (functionCall), \
00147         int(-1), \
00148         "'" LASS_STRINGIFY(functionCall) "' in '" LASS_HERE "'")
00149 
00150 #define LASS_ENFORCE_CLIB_EX(functionCall, errorValue)\
00151     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00152         ::lass::util::impl::UnequalPredicate,\
00153         ::lass::util::impl::ClibRaiser,\
00154         (functionCall), \
00155         (errorValue), \
00156         "'" LASS_STRINGIFY(functionCall) "' in '" LASS_HERE "'")
00157 
00158 #define LASS_WARN_CLIB(functionCall)\
00159     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00160         ::lass::util::impl::UnequalPredicate,\
00161         ::lass::util::impl::ClibWarner,\
00162         (functionCall), \
00163         int(-1), \
00164         "'" LASS_STRINGIFY(functionCall) "' in '" LASS_HERE "'")
00165 
00166     
00167 /** Enforces the return code of a CLIB function call to be zero.
00168  *  @ingroup Enforcers
00169  *  Some CLIB functions return zero on success and an error code on failure.
00170  *  This error code can be translated to a string message with strerror().
00171  *  LASS_ENFORCE_CLIB_RC will enforce that the return code of the function call
00172  *  is zero.  If it's not, it will raise a runtime exception with the translated
00173  *  error code.
00174  */
00175 #define LASS_ENFORCE_CLIB_RC(errorCode)\
00176     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00177         ::lass::util::impl::EqualPredicate,\
00178         ::lass::util::impl::ClibRcRaiser,\
00179         (errorCode), \
00180         int(0), \
00181         "'" LASS_STRINGIFY(errorCode) "' in '" LASS_HERE "'")
00182 
00183     
00184 /** Enforces the return code of a CLIB function call to be zero.
00185  *  @ingroup Enforcers
00186  *  Some CLIB functions return zero on success and an error code on failure.
00187  *  This error code can be translated to a string message with strerror().
00188  *  LASS_ENFORCE_CLIB_RC will enforce that the return code of the function call
00189  *  is zero.  If it's not, it will raise a runtime exception with the translated
00190  *  error code.
00191  */
00192 #define LASS_WARN_CLIB_RC(errorCode)\
00193     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00194         ::lass::util::impl::EqualPredicate,\
00195         ::lass::util::impl::ClibRcWarner,\
00196         (errorCode), \
00197         int(0), \
00198         "'" LASS_STRINGIFY(errorCode) "' in '" LASS_HERE "'")
00199 
00200 
00201         
00202 /** Enforces the HRESULT of a COM call to be 0 or more.
00203  *  @ingroup Enforcers
00204  */
00205 #define LASS_ENFORCE_COM(comResult) \
00206     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00207         ::lass::util::impl::GreaterEqualPredicate,\
00208         ::lass::util::impl::ComRaiser,\
00209         (comResult), \
00210         int(0), \
00211         "'" LASS_STRINGIFY(comResult) "' in '" LASS_HERE "'.")
00212 
00213 
00214 
00215 #ifdef LASS_HAS_GETLASTERROR
00216 
00217 /** Enforces a Win API call to return non-zero and uses GetLastError() to retrieve error code.
00218  *  @ingroup Enforcers
00219  */
00220 #define LASS_ENFORCE_WINAPI(functionCall)\
00221     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00222         ::lass::util::impl::WinAPIPredicate,\
00223         ::lass::util::impl::LastErrorRaiser,\
00224         (functionCall), \
00225         int(0), \
00226         "'" LASS_STRINGIFY(functionCall) "' in '" LASS_HERE "'")
00227 
00228 #define LASS_WARN_WINAPI(functionCall)\
00229     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00230         ::lass::util::impl::WinAPIPredicate,\
00231         ::lass::util::impl::LastErrorWarner,\
00232         (functionCall), \
00233         int(0), \
00234         "'" LASS_STRINGIFY(functionCall) "' in '" LASS_HERE "'")
00235 
00236 
00237 /** Enforces the handle to be different than INVALID_HANDLE_VALUE and uses GetLastError() to retrieve error code.
00238  *  @ingroup Enforcers
00239  */
00240 #define LASS_ENFORCE_HANDLE(handle) \
00241     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00242         ::lass::util::impl::UnequalPredicate,\
00243         ::lass::util::impl::LastErrorRaiser,\
00244         (handle),\
00245         INVALID_HANDLE_VALUE, \
00246         "Invalid handle '" LASS_STRINGIFY(handle) "' in '" LASS_HERE "'.")
00247 
00248 #endif
00249 
00250 
00251 
00252 /** Enforces an index to be in the half open range [0, iSize).
00253  *  @ingroup Enforcers
00254  */
00255 #define LASS_ENFORCE_INDEX(index, size)\
00256     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00257         ::lass::util::impl::IndexPredicate,\
00258         ::lass::util::impl::IndexRaiser,\
00259         (index), (size), LASS_HERE)
00260 
00261 
00262 
00263 /** Enforces a pointer to an object to be dynamic casted to related type.
00264  *  @ingroup Enforcers
00265  *  @param t_DestPointer 
00266  *      the type of the pointer to be casted to.  
00267  *  @param v_pointer 
00268  *      a pointer to be casted (note that we don't cast references, use
00269  *      LASS_ENFORCE_DYNAMIC_REF_CAST for that)
00270  */
00271 #define LASS_ENFORCE_DYNAMIC_CAST(t_DestPointer, v_pointer)\
00272     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00273         ::lass::util::impl::TruePredicate,\
00274         ::lass::util::impl::DefaultRaiser,\
00275         (dynamic_cast<t_DestPointer>(v_pointer)),\
00276         int(0), \
00277         "Unable to cast to '" LASS_STRINGIFY(t_DestPointer) "': '" LASS_STRINGIFY(v_pointer)\
00278         "' is a null pointer or is not of the correct type, '" LASS_HERE "'.")
00279 
00280 
00281 
00282 /** Enforces a python shared pointer to be dynamic casted to a related python shared pointer.
00283  *  @ingroup Enforcers
00284  *  @param t_DestPyObjectPtr
00285  *      the type of python shared pointer to be casted to. 
00286  *      So if you want to cast to @c PyObjectPtr<Foo>::Type, @a t_DestPyObjectPtr should
00287  *      be @c PyObjectPtr<Foo>::Type.
00288  *  @param v_pyObjectPtr
00289  *      a python shared pointer to be casted.
00290  */
00291 #define LASS_ENFORCE_DYNAMIC_PY_CAST(t_DestPyObjectPtr, v_pyObjectPtr)\
00292     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00293         ::lass::util::impl::TruePredicate,\
00294         ::lass::util::impl::DefaultRaiser,\
00295         ::lass::python::dynamicPyCast<t_DestPyObjectPtr>(v_pyObjectPtr),\
00296         int(0), \
00297         "Unable to cast to '" LASS_STRINGIFY(t_DestPyObjectPtr) "': '" LASS_STRINGIFY(v_pyObjectPtr)\
00298         "' is a null pointer or is not of the correct type, '" LASS_HERE "'.")
00299 
00300 
00301 
00302 /** always-failing enforcer to block unreachable code paths
00303  */
00304 #define LASS_ENFORCE_UNREACHABLE\
00305     *LASS_UTIL_IMPL_MAKE_ENFORCER(\
00306         ::lass::util::impl::TruePredicate,\
00307         ::lass::util::impl::DefaultRaiser,\
00308         (false),\
00309         int(0), \
00310         "You have reached unreachable code in '" LASS_HERE "'.")
00311 
00312 #include "impl/enforcer_impl.h"
00313 #include <string>
00314 #include <sstream>
00315 
00316 #endif

Generated on Mon Nov 10 14:20:04 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo