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