[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

vigra/static_assert.hxx
00001 /************************************************************************/
00002 /*                                                                      */
00003 /*               Copyright 2004-2005 by Ullrich Koethe                  */
00004 /*                                                                      */
00005 /*    This file is part of the VIGRA computer vision library.           */
00006 /*    The VIGRA Website is                                              */
00007 /*        http://hci.iwr.uni-heidelberg.de/vigra/                       */
00008 /*    Please direct questions, bug reports, and contributions to        */
00009 /*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
00010 /*        vigra@informatik.uni-hamburg.de                               */
00011 /*                                                                      */
00012 /*    Permission is hereby granted, free of charge, to any person       */
00013 /*    obtaining a copy of this software and associated documentation    */
00014 /*    files (the "Software"), to deal in the Software without           */
00015 /*    restriction, including without limitation the rights to use,      */
00016 /*    copy, modify, merge, publish, distribute, sublicense, and/or      */
00017 /*    sell copies of the Software, and to permit persons to whom the    */
00018 /*    Software is furnished to do so, subject to the following          */
00019 /*    conditions:                                                       */
00020 /*                                                                      */
00021 /*    The above copyright notice and this permission notice shall be    */
00022 /*    included in all copies or substantial portions of the             */
00023 /*    Software.                                                         */
00024 /*                                                                      */
00025 /*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
00026 /*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
00027 /*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
00028 /*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
00029 /*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
00030 /*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
00031 /*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
00032 /*    OTHER DEALINGS IN THE SOFTWARE.                                   */                
00033 /*                                                                      */
00034 /************************************************************************/
00035 
00036 #ifndef VIGRA_STATIC_ASSERT_HXX
00037 #define VIGRA_STATIC_ASSERT_HXX
00038 
00039 // based on the static assertion design in boost::mpl (see www.boost.org)
00040 
00041 #define VIGRA_PREPROCESSOR_CONCATENATE(a, b) VIGRA_PREPROCESSOR_CONCATENATE_IMPL(a, b)
00042 #define VIGRA_PREPROCESSOR_CONCATENATE_IMPL(a, b) a ## b
00043 
00044 namespace vigra {
00045 
00046 namespace staticAssert {
00047 
00048 template <bool Predicate>
00049 struct AssertBool;
00050 
00051 template <>
00052 struct AssertBool<true>
00053 {
00054     typedef int type;
00055     typedef void * not_type;
00056 };
00057 
00058 template <>
00059 struct AssertBool<false>
00060 {
00061     typedef void * type;
00062     typedef int not_type;
00063 };
00064 
00065 template <class T>
00066 struct Assert;
00067 
00068 template <>
00069 struct Assert<VigraTrueType>
00070 {
00071     typedef int type;
00072     typedef void * not_type;
00073 };
00074 
00075 template <>
00076 struct Assert<VigraFalseType>
00077 {
00078     typedef void * type;
00079     typedef int not_type;
00080 };
00081 
00082 struct failure{};
00083 struct success {};
00084 inline int check( success ) { return 0; }
00085 
00086 template< typename Predicate >
00087 failure ************ (Predicate::************ 
00088       assertImpl( void (*)(Predicate), typename Predicate::not_type )
00089     );
00090 
00091 template< typename Predicate >
00092 success
00093 assertImpl( void (*)(Predicate), typename Predicate::type );
00094 
00095 /* Usage:
00096 
00097 1. Define an assertion class, derived from vigra::staticAssert::Assert,
00098    whose name serves as an error message:
00099    
00100     template <int N>
00101     struct FixedPoint_overflow_error__More_than_31_bits_requested
00102     : vigra::staticAssert::AssertBool<(N < 32)>
00103     {};
00104 
00105 2. Call VIGRA_STATIC_ASSERT() with the assertion class:
00106 
00107     template <int N>
00108     void test()
00109     {
00110         // signal error if N > 31
00111         VIGRA_STATIC_ASSERT((FixedPoint_overflow_error__More_than_31_bits_requested<N>));
00112     }
00113     
00114 TODO: provide more assertion base classes for other (non boolean) types of tests
00115 */
00116 #if !defined(__GNUC__) || __GNUC__ > 2
00117 #define VIGRA_STATIC_ASSERT(Predicate) \
00118 enum { \
00119     VIGRA_PREPROCESSOR_CONCATENATE(vigra_assertion_in_line_, __LINE__) = sizeof( \
00120          staticAssert::check( \
00121               staticAssert::assertImpl( (void (*) Predicate)0, 1 ) \
00122             ) \
00123         ) \
00124 }
00125 #else
00126 #define VIGRA_STATIC_ASSERT(Predicate)
00127 #endif
00128 
00129 } // namespace staticAssert
00130 
00131 } // namespace vigra
00132 
00133 #endif // VIGRA_STATIC_ASSERT_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.7.0 (Thu Aug 25 2011)