summaryrefslogtreecommitdiff
path: root/include/basebmp/metafunctions.hxx
blob: 1f3f1806042a15c7bdb5d6de845b42f88413187c (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_BASEBMP_METAFUNCTIONS_HXX
#define INCLUDED_BASEBMP_METAFUNCTIONS_HXX

#include <boost/mpl/integral_c.hpp>
#include <vigra/metaprogramming.hxx>
#include <vigra/numerictraits.hxx>

#include <functional>

namespace basebmp
{

// TODO(Q3): move to generic place (o3tl?)

/** template meta function: add const qualifier to 2nd type, if given
    1st type has it
*/
template<typename A, typename B> struct clone_const
{
    typedef B type;
};
template<typename A, typename B> struct clone_const<const A,B>
{
    typedef const B type;
};

/** template meta function: add const qualifier to plain type (if not
    already there)
 */
template <typename T> struct add_const
{
    typedef const T type;
};
template <typename T> struct add_const<const T>
{
    typedef const T type;
};

/// template meta function: remove const qualifier from plain type
template <typename T> struct remove_const
{
    typedef T type;
};
template <typename T> struct remove_const<const T>
{
    typedef T type;
};



/// Base class for an adaptable ternary functor
template< typename A1, typename A2, typename A3, typename R > struct TernaryFunctorBase
{
    typedef A1 first_argument_type;
    typedef A2 second_argument_type;
    typedef A3 third_argument_type;
    typedef R  result_type;
};



/** template meta function: ensure that given integer type is unsigned

    If given integer type is already unsigned, return as-is -
    otherwise, convert to unsigned type of same or greater range.
 */
template< typename T > struct make_unsigned;

#define BASEBMP_MAKE_UNSIGNED(T,U)        \
    template<> struct make_unsigned<T> { \
        typedef U type; \
    };

BASEBMP_MAKE_UNSIGNED(signed char,unsigned char)
BASEBMP_MAKE_UNSIGNED(unsigned char,unsigned char)
BASEBMP_MAKE_UNSIGNED(short,unsigned short)
BASEBMP_MAKE_UNSIGNED(unsigned short,unsigned short)
BASEBMP_MAKE_UNSIGNED(int,unsigned int)
BASEBMP_MAKE_UNSIGNED(unsigned int,unsigned int)
BASEBMP_MAKE_UNSIGNED(long,unsigned long)
BASEBMP_MAKE_UNSIGNED(unsigned long,unsigned long)

#undef BASEBMP_MAKE_UNSIGNED

/// cast integer to unsigned type of similar size
template< typename T > inline typename make_unsigned<T>::type unsigned_cast( T value )
{
    return static_cast< typename make_unsigned<T>::type >(value);
}



/// returns true, if given number is strictly less than 0
template< typename T > inline bool is_negative( T x )
{
    return x < 0;
}

/// Overload for ints (branch-free)
inline bool is_negative( int x )
{
    // force logic shift (result for signed shift right is undefined)
    return static_cast<unsigned int>(x) >> (sizeof(int)*8-1);
}



/// Results in VigraTrueType, if T is of integer type and scalar
template< typename T, typename trueCase, typename falseCase >
struct ifScalarIntegral
{
    typedef
    typename vigra::If<
        typename vigra::NumericTraits< T >::isIntegral,
        typename vigra::If<
            typename vigra::NumericTraits< T >::isScalar,
            trueCase,
            falseCase >::type,
        falseCase >::type type;
};

/// Results in VigraTrueType, if T is of non-integer type and scalar
template< typename T, typename trueCase, typename falseCase >
struct ifScalarNonIntegral
{
    typedef
    typename vigra::If<
        typename vigra::NumericTraits< T >::isIntegral,
        falseCase,
        typename vigra::If<
            typename vigra::NumericTraits< T >::isScalar,
            trueCase,
            falseCase >::type >::type type;
};

/// Results in VigraTrueType, if both T1 and T2 are of integer type and scalar
template< typename T1, typename T2, typename trueCase, typename falseCase >
struct ifBothScalarIntegral
{
    typedef
    typename ifScalarIntegral<
        T1,
        typename ifScalarIntegral<
            T2,
            trueCase,
            falseCase >::type,
        falseCase >::type type;
};



/// Count number of trailing zeros
template< unsigned int val > struct numberOfTrailingZeros
{
    enum { next = val >> 1 };
    enum { value = vigra::IfBool< (val & 1) == 0,
                                  numberOfTrailingZeros<next>,
                                  boost::mpl::integral_c< int,-1 > > ::type::value + 1 };
};

template<> struct numberOfTrailingZeros<0>
{
    enum { value = 0 };
};



/// Count number of one bits
template< unsigned int val > struct bitcount
{
    enum { next = val >> 1 };
    enum { value = bitcount<next>::value + (val & 1) };
};

template<> struct bitcount<0>
{
    enum { value = 0 };
};



/// Shift left for positive shift value, and right otherwise
template< typename T > inline T shiftLeft( T v, int shift )
{
    return shift > 0 ? v << shift : v >> (-shift);
}

/// Shift right for positive shift value, and left otherwise
template< typename T > inline T shiftRight( T v, int shift )
{
    return shift > 0 ? v >> shift : v << (-shift);
}

} // namespace basebmp

#endif /* INCLUDED_BASEBMP_METAFUNCTIONS_HXX */

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */