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

static_assert.hxx |

1 /************************************************************************/
2 /* */
3 /* Copyright 2004-2005 by Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35
36 #ifndef VIGRA_STATIC_ASSERT_HXX
37 #define VIGRA_STATIC_ASSERT_HXX
38
39 // based on the static assertion design in boost::mpl (see www.boost.org)
40
41 #define VIGRA_PREPROCESSOR_CONCATENATE(a, b) VIGRA_PREPROCESSOR_CONCATENATE_IMPL(a, b)
42 #define VIGRA_PREPROCESSOR_CONCATENATE_IMPL(a, b) a ## b
43
44 namespace vigra {
45
46 namespace staticAssert {
47
48 template <bool Predicate>
49 struct AssertBool;
50
51 template <>
52 struct AssertBool<true>
53 {
54 typedef int type;
55 typedef void * not_type;
56 };
57
58 template <>
59 struct AssertBool<false>
60 {
61 typedef void * type;
62 typedef int not_type;
63 };
64
65 template <class T>
66 struct Assert;
67
68 template <>
69 struct Assert<VigraTrueType>
70 {
71 typedef int type;
72 typedef void * not_type;
73 };
74
75 template <>
76 struct Assert<VigraFalseType>
77 {
78 typedef void * type;
79 typedef int not_type;
80 };
81
82 struct failure{};
83 struct success {};
84 inline int check( success ) { return 0; }
85
86 template< typename Predicate >
87 failure ************ (Predicate::************
88 assertImpl( void (*)(Predicate), typename Predicate::not_type )
89 );
90
91 template< typename Predicate >
92 success
93 assertImpl( void (*)(Predicate), typename Predicate::type );
94
95 /* Usage:
96
97 1. Define an assertion class, derived from vigra::staticAssert::Assert,
98 whose name serves as an error message:
99
100 template <int N>
101 struct FixedPoint_overflow_error__More_than_31_bits_requested
102 : vigra::staticAssert::AssertBool<(N < 32)>
103 {};
104
105 2. Call VIGRA_STATIC_ASSERT() with the assertion class:
106
107 template <int N>
108 void test()
109 {
110 // signal error if N > 31
111 VIGRA_STATIC_ASSERT((FixedPoint_overflow_error__More_than_31_bits_requested<N>));
112 }
113
114 TODO: provide more assertion base classes for other (non boolean) types of tests
115 */
116 #if !defined(__GNUC__) || __GNUC__ > 2
117 #define VIGRA_STATIC_ASSERT(Predicate) \
118 enum { \
119 VIGRA_PREPROCESSOR_CONCATENATE(vigra_assertion_in_line_, __LINE__) = sizeof( \
120 staticAssert::check( \
121 staticAssert::assertImpl( (void (*) Predicate)0, 1 ) \
122 ) \
123 ) \
124 }
125 #else
126 #define VIGRA_STATIC_ASSERT(Predicate)
127 #endif
128
129 } // namespace staticAssert
130
131 } // namespace vigra
132
133 #endif // VIGRA_STATIC_ASSERT_HXX
vigra
Definition: accessor.hxx:43

© 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.11.0