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