File: static_assert.hxx

package info (click to toggle)
gamera 1%3A3.4.2%2Bgit20160808.1725654-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,312 kB
  • ctags: 24,991
  • sloc: xml: 122,324; ansic: 52,869; cpp: 50,664; python: 35,034; makefile: 118; sh: 101
file content (135 lines) | stat: -rw-r--r-- 4,882 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/************************************************************************/
/*                                                                      */
/*               Copyright 2004-2005 by Ullrich Koethe                  */
/*       Cognitive Systems Group, University of Hamburg, Germany        */
/*                                                                      */
/*    This file is part of the VIGRA computer vision library.           */
/*    ( Version 1.6.0, Aug 13 2008 )                                    */
/*    The VIGRA Website is                                              */
/*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
/*    Please direct questions, bug reports, and contributions to        */
/*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
/*        vigra@informatik.uni-hamburg.de                               */
/*                                                                      */
/*    Permission is hereby granted, free of charge, to any person       */
/*    obtaining a copy of this software and associated documentation    */
/*    files (the "Software"), to deal in the Software without           */
/*    restriction, including without limitation the rights to use,      */
/*    copy, modify, merge, publish, distribute, sublicense, and/or      */
/*    sell copies of the Software, and to permit persons to whom the    */
/*    Software is furnished to do so, subject to the following          */
/*    conditions:                                                       */
/*                                                                      */
/*    The above copyright notice and this permission notice shall be    */
/*    included in all copies or substantial portions of the             */
/*    Software.                                                         */
/*                                                                      */
/*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
/*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
/*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
/*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
/*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
/*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
/*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
/*    OTHER DEALINGS IN THE SOFTWARE.                                   */                
/*                                                                      */
/************************************************************************/

#ifndef VIGRA_STATIC_ASSERT_HXX
#define VIGRA_STATIC_ASSERT_HXX

// based on the static assertion design in boost::mpl (see www.boost.org)

#define VIGRA_PREPROCESSOR_CONCATENATE(a, b) VIGRA_PREPROCESSOR_CONCATENATE_IMPL(a, b)
#define VIGRA_PREPROCESSOR_CONCATENATE_IMPL(a, b) a ## b

namespace vigra {

namespace staticAssert {

template <bool Predicate>
struct AssertBool;

template <>
struct AssertBool<true>
{
    typedef int type;
    typedef void * not_type;
};

template <>
struct AssertBool<false>
{
    typedef void * type;
    typedef int not_type;
};

template <class T>
struct Assert;

template <>
struct Assert<VigraTrueType>
{
    typedef int type;
    typedef void * not_type;
};

template <>
struct Assert<VigraFalseType>
{
    typedef void * type;
    typedef int not_type;
};

struct failure{};
struct success {};
inline int check( success ) { return 0; }

template< typename Predicate >
failure ************ (Predicate::************ 
      assertImpl( void (*)(Predicate), typename Predicate::not_type )
    );

template< typename Predicate >
success
assertImpl( void (*)(Predicate), typename Predicate::type );

/* Usage:

1. Define an assertion class, derived from vigra::staticAssert::Assert,
   whose name serves as an error message:
   
    template <int N>
    struct FixedPoint_overflow_error__More_than_31_bits_requested
    : vigra::staticAssert::AssertBool<(N < 32)>
    {};

2. Call VIGRA_STATIC_ASSERT() with the assertion class:

    template <int N>
    void test()
    {
        // signal error if N > 31
        VIGRA_STATIC_ASSERT((FixedPoint_overflow_error__More_than_31_bits_requested<N>));
    }
    
TODO: provide more assertion base classes for other (non boolean) types of tests
*/
#if !defined(__GNUC__) || __GNUC__ > 2
#define VIGRA_STATIC_ASSERT(Predicate) \
enum { \
    VIGRA_PREPROCESSOR_CONCATENATE(vigra_assertion_in_line_, __LINE__) = sizeof( \
         staticAssert::check( \
              staticAssert::assertImpl( (void (*) Predicate)0, 1 ) \
            ) \
        ) \
}
#else
#define VIGRA_STATIC_ASSERT(Predicate)
#endif

} // namespace staticAssert

} // namespace vigra

#endif // VIGRA_STATIC_ASSERT_HXX