[ 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
44namespace vigra {
45
46namespace staticAssert {
47
48template <bool Predicate>
49struct AssertBool;
50
51template <>
52struct AssertBool<true>
53{
54 typedef int type;
55 typedef void * not_type;
56};
57
58template <>
59struct AssertBool<false>
60{
61 typedef void * type;
62 typedef int not_type;
63};
64
65template <class T>
66struct Assert;
67
68template <>
69struct Assert<VigraTrueType>
70{
71 typedef int type;
72 typedef void * not_type;
73};
74
75template <>
76struct Assert<VigraFalseType>
77{
78 typedef void * type;
79 typedef int not_type;
80};
81
82struct failure{};
83struct success {};
84inline int check( success ) { return 0; }
85
86template< typename Predicate >
87failure ************ Predicate::************
88 assertImpl( void (*)(Predicate), typename Predicate::not_type );
89
90template< typename Predicate >
91success
92assertImpl( void (*)(Predicate), typename Predicate::type );
93
94/* Usage:
95
961. Define an assertion class, derived from vigra::staticAssert::Assert,
97 whose name serves as an error message:
98
99 template <int N>
100 struct FixedPoint_overflow_error__More_than_31_bits_requested
101 : vigra::staticAssert::AssertBool<(N < 32)>
102 {};
103
1042. Call VIGRA_STATIC_ASSERT() with the assertion class:
105
106 template <int N>
107 void test()
108 {
109 // signal error if N > 31
110 VIGRA_STATIC_ASSERT((FixedPoint_overflow_error__More_than_31_bits_requested<N>));
111 }
112
113TODO: provide more assertion base classes for other (non boolean) types of tests
114*/
115#if !defined(__GNUC__) || __GNUC__ > 2
116#define VIGRA_STATIC_ASSERT(Predicate) \
117enum { \
118 VIGRA_PREPROCESSOR_CONCATENATE(vigra_assertion_in_line_, __LINE__) = sizeof( \
119 staticAssert::check( \
120 staticAssert::assertImpl( (void (*) Predicate)0, 1 ) \
121 ) \
122 ) \
123}
124#else
125#define VIGRA_STATIC_ASSERT(Predicate)
126#endif
127
128} // namespace staticAssert
129
130} // namespace vigra
131
132#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.12.2 (Mon Apr 14 2025)