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

details vigra/static_assert.hxx VIGRA

00001 /************************************************************************/
00002 /*                                                                      */
00003 /*               Copyright 2004-2005 by Ullrich Koethe                  */
00004 /*       Cognitive Systems Group, University of Hamburg, Germany        */
00005 /*                                                                      */
00006 /*    This file is part of the VIGRA computer vision library.           */
00007 /*    ( Version 1.3.3, Aug 18 2005 )                                    */
00008 /*    You may use, modify, and distribute this software according       */
00009 /*    to the terms stated in the LICENSE file included in               */
00010 /*    the VIGRA distribution.                                           */
00011 /*                                                                      */
00012 /*    The VIGRA Website is                                              */
00013 /*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
00014 /*    Please direct questions, bug reports, and contributions to        */
00015 /*        koethe@informatik.uni-hamburg.de                              */
00016 /*                                                                      */
00017 /*  THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY EXPRESS OR          */
00018 /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED      */
00019 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
00020 /*                                                                      */
00021 /************************************************************************/
00022 
00023 #ifndef VIGRA_STATIC_ASSERT_HXX
00024 #define VIGRA_STATIC_ASSERT_HXX
00025 
00026 // based on the static assertion design in boost::mpl (see www.boost.org)
00027 
00028 #define VIGRA_PREPROCESSOR_CONCATENATE(a, b) VIGRA_PREPROCESSOR_CONCATENATE_IMPL(a, b)
00029 #define VIGRA_PREPROCESSOR_CONCATENATE_IMPL(a, b) a ## b
00030 
00031 namespace vigra {
00032 
00033 namespace staticAssert {
00034 
00035 template <bool Predicate>
00036 struct AssertBool;
00037 
00038 template <>
00039 struct AssertBool<true>
00040 {
00041     typedef int type;
00042     typedef void * not_type;
00043 };
00044 
00045 template <>
00046 struct AssertBool<false>
00047 {
00048     typedef void * type;
00049     typedef int not_type;
00050 };
00051 
00052 struct failure{};
00053 struct success {};
00054 inline int check( success ) { return 0; }
00055 
00056 template< typename Predicate >
00057 failure ************ (Predicate::************ 
00058       assertImpl( void (*)(Predicate), typename Predicate::not_type )
00059     );
00060 
00061 template< typename Predicate >
00062 success
00063 assertImpl( void (*)(Predicate), typename Predicate::type );
00064 
00065 /* Usage:
00066 
00067 1. Define an assertion class, derived from vigra::staticAssert::Assert,
00068    whose name serves as an error message:
00069    
00070     template <int N>
00071     struct FixedPoint_overflow_error__More_than_31_bits_requested
00072     : vigra::staticAssert::AssertBool<(N < 32)>
00073     {};
00074 
00075 2. Call VIGRA_STATIC_ASSERT() with the assertion class:
00076 
00077     template <int N>
00078     void test()
00079     {
00080         // signal error if N > 31
00081         VIGRA_STATIC_ASSERT((FixedPoint_overflow_error__More_than_31_bits_requested<N>));
00082     }
00083     
00084 TODO: provide more assertion base classes for other (non boolean) types of tests
00085 */
00086 #define VIGRA_STATIC_ASSERT(Predicate) \
00087 enum { \
00088     VIGRA_PREPROCESSOR_CONCATENATE(vigra_assertion_in_line_, __LINE__) = sizeof( \
00089          staticAssert::check( \
00090               staticAssert::assertImpl( (void (*) Predicate)0, 1 ) \
00091             ) \
00092         ) \
00093 }
00094 
00095 } // namespace staticAssert
00096 
00097 } // namespace vigra
00098 
00099 #endif // VIGRA_STATIC_ASSERT_HXX

© Ullrich Köthe (koethe@informatik.uni-hamburg.de)
Cognitive Systems Group, University of Hamburg, Germany

html generated using doxygen and Python
VIGRA 1.3.3 (18 Aug 2005)