1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski #ifndef INCLUDED_BASEBMP_METAFUNCTIONS_HXX
25*b1cdbd2cSJim Jagielski #define INCLUDED_BASEBMP_METAFUNCTIONS_HXX
26*b1cdbd2cSJim Jagielski
27*b1cdbd2cSJim Jagielski #include <boost/mpl/integral_c.hpp>
28*b1cdbd2cSJim Jagielski #include <vigra/metaprogramming.hxx>
29*b1cdbd2cSJim Jagielski #include <vigra/numerictraits.hxx>
30*b1cdbd2cSJim Jagielski
31*b1cdbd2cSJim Jagielski namespace basebmp
32*b1cdbd2cSJim Jagielski {
33*b1cdbd2cSJim Jagielski
34*b1cdbd2cSJim Jagielski // TODO(Q3): move to generic place (o3tl?)
35*b1cdbd2cSJim Jagielski
36*b1cdbd2cSJim Jagielski /** template meta function: add const qualifier to 2nd type, if given
37*b1cdbd2cSJim Jagielski 1st type has it
38*b1cdbd2cSJim Jagielski */
39*b1cdbd2cSJim Jagielski template<typename A, typename B> struct clone_const
40*b1cdbd2cSJim Jagielski {
41*b1cdbd2cSJim Jagielski typedef B type;
42*b1cdbd2cSJim Jagielski };
43*b1cdbd2cSJim Jagielski template<typename A, typename B> struct clone_const<const A,B>
44*b1cdbd2cSJim Jagielski {
45*b1cdbd2cSJim Jagielski typedef const B type;
46*b1cdbd2cSJim Jagielski };
47*b1cdbd2cSJim Jagielski
48*b1cdbd2cSJim Jagielski /** template meta function: add const qualifier to plain type (if not
49*b1cdbd2cSJim Jagielski already there)
50*b1cdbd2cSJim Jagielski */
51*b1cdbd2cSJim Jagielski template <typename T> struct add_const
52*b1cdbd2cSJim Jagielski {
53*b1cdbd2cSJim Jagielski typedef const T type;
54*b1cdbd2cSJim Jagielski };
55*b1cdbd2cSJim Jagielski template <typename T> struct add_const<const T>
56*b1cdbd2cSJim Jagielski {
57*b1cdbd2cSJim Jagielski typedef const T type;
58*b1cdbd2cSJim Jagielski };
59*b1cdbd2cSJim Jagielski
60*b1cdbd2cSJim Jagielski /// template meta function: remove const qualifier from plain type
61*b1cdbd2cSJim Jagielski template <typename T> struct remove_const
62*b1cdbd2cSJim Jagielski {
63*b1cdbd2cSJim Jagielski typedef T type;
64*b1cdbd2cSJim Jagielski };
65*b1cdbd2cSJim Jagielski template <typename T> struct remove_const<const T>
66*b1cdbd2cSJim Jagielski {
67*b1cdbd2cSJim Jagielski typedef T type;
68*b1cdbd2cSJim Jagielski };
69*b1cdbd2cSJim Jagielski
70*b1cdbd2cSJim Jagielski //--------------------------------------------------------------
71*b1cdbd2cSJim Jagielski
72*b1cdbd2cSJim Jagielski /// Base class for an adaptable ternary functor
73*b1cdbd2cSJim Jagielski template< typename A1, typename A2, typename A3, typename R > struct TernaryFunctorBase
74*b1cdbd2cSJim Jagielski {
75*b1cdbd2cSJim Jagielski typedef A1 first_argument_type;
76*b1cdbd2cSJim Jagielski typedef A2 second_argument_type;
77*b1cdbd2cSJim Jagielski typedef A3 third_argument_type;
78*b1cdbd2cSJim Jagielski typedef R result_type;
79*b1cdbd2cSJim Jagielski };
80*b1cdbd2cSJim Jagielski
81*b1cdbd2cSJim Jagielski //--------------------------------------------------------------
82*b1cdbd2cSJim Jagielski
83*b1cdbd2cSJim Jagielski /** template meta function: ensure that given integer type is unsigned
84*b1cdbd2cSJim Jagielski
85*b1cdbd2cSJim Jagielski If given integer type is already unsigned, return as-is -
86*b1cdbd2cSJim Jagielski otherwise, convert to unsigned type of same or greater range.
87*b1cdbd2cSJim Jagielski */
88*b1cdbd2cSJim Jagielski template< typename T > struct make_unsigned;
89*b1cdbd2cSJim Jagielski
90*b1cdbd2cSJim Jagielski #define BASEBMP_MAKE_UNSIGNED(T,U) \
91*b1cdbd2cSJim Jagielski template<> struct make_unsigned<T> { \
92*b1cdbd2cSJim Jagielski typedef U type; \
93*b1cdbd2cSJim Jagielski };
94*b1cdbd2cSJim Jagielski
BASEBMP_MAKE_UNSIGNED(signed char,unsigned char)95*b1cdbd2cSJim Jagielski BASEBMP_MAKE_UNSIGNED(signed char,unsigned char)
96*b1cdbd2cSJim Jagielski BASEBMP_MAKE_UNSIGNED(unsigned char,unsigned char)
97*b1cdbd2cSJim Jagielski BASEBMP_MAKE_UNSIGNED(short,unsigned short)
98*b1cdbd2cSJim Jagielski BASEBMP_MAKE_UNSIGNED(unsigned short,unsigned short)
99*b1cdbd2cSJim Jagielski BASEBMP_MAKE_UNSIGNED(int,unsigned int)
100*b1cdbd2cSJim Jagielski BASEBMP_MAKE_UNSIGNED(unsigned int,unsigned int)
101*b1cdbd2cSJim Jagielski BASEBMP_MAKE_UNSIGNED(long,unsigned long)
102*b1cdbd2cSJim Jagielski BASEBMP_MAKE_UNSIGNED(unsigned long,unsigned long)
103*b1cdbd2cSJim Jagielski
104*b1cdbd2cSJim Jagielski #undef BASEBMP_MAKE_UNSIGNED
105*b1cdbd2cSJim Jagielski
106*b1cdbd2cSJim Jagielski /// cast integer to unsigned type of similar size
107*b1cdbd2cSJim Jagielski template< typename T > inline typename make_unsigned<T>::type unsigned_cast( T value )
108*b1cdbd2cSJim Jagielski {
109*b1cdbd2cSJim Jagielski return static_cast< typename make_unsigned<T>::type >(value);
110*b1cdbd2cSJim Jagielski }
111*b1cdbd2cSJim Jagielski
112*b1cdbd2cSJim Jagielski //--------------------------------------------------------------
113*b1cdbd2cSJim Jagielski
114*b1cdbd2cSJim Jagielski /// returns true, if given number is strictly less than 0
is_negative(T x)115*b1cdbd2cSJim Jagielski template< typename T > inline bool is_negative( T x )
116*b1cdbd2cSJim Jagielski {
117*b1cdbd2cSJim Jagielski return x < 0;
118*b1cdbd2cSJim Jagielski }
119*b1cdbd2cSJim Jagielski
120*b1cdbd2cSJim Jagielski /// Overload for ints (branch-free)
is_negative(int x)121*b1cdbd2cSJim Jagielski inline bool is_negative( int x )
122*b1cdbd2cSJim Jagielski {
123*b1cdbd2cSJim Jagielski // force logic shift (result for signed shift right is undefined)
124*b1cdbd2cSJim Jagielski return static_cast<unsigned int>(x) >> (sizeof(int)*8-1);
125*b1cdbd2cSJim Jagielski }
126*b1cdbd2cSJim Jagielski
127*b1cdbd2cSJim Jagielski //--------------------------------------------------------------
128*b1cdbd2cSJim Jagielski
129*b1cdbd2cSJim Jagielski /// Results in VigraTrueType, if T is of integer type and scalar
130*b1cdbd2cSJim Jagielski template< typename T, typename trueCase, typename falseCase >
131*b1cdbd2cSJim Jagielski struct ifScalarIntegral
132*b1cdbd2cSJim Jagielski {
133*b1cdbd2cSJim Jagielski typedef
134*b1cdbd2cSJim Jagielski typename vigra::If<
135*b1cdbd2cSJim Jagielski typename vigra::NumericTraits< T >::isIntegral,
136*b1cdbd2cSJim Jagielski typename vigra::If<
137*b1cdbd2cSJim Jagielski typename vigra::NumericTraits< T >::isScalar,
138*b1cdbd2cSJim Jagielski trueCase,
139*b1cdbd2cSJim Jagielski falseCase >::type,
140*b1cdbd2cSJim Jagielski falseCase >::type type;
141*b1cdbd2cSJim Jagielski };
142*b1cdbd2cSJim Jagielski
143*b1cdbd2cSJim Jagielski /// Results in VigraTrueType, if T is of non-integer type and scalar
144*b1cdbd2cSJim Jagielski template< typename T, typename trueCase, typename falseCase >
145*b1cdbd2cSJim Jagielski struct ifScalarNonIntegral
146*b1cdbd2cSJim Jagielski {
147*b1cdbd2cSJim Jagielski typedef
148*b1cdbd2cSJim Jagielski typename vigra::If<
149*b1cdbd2cSJim Jagielski typename vigra::NumericTraits< T >::isIntegral,
150*b1cdbd2cSJim Jagielski falseCase,
151*b1cdbd2cSJim Jagielski typename vigra::If<
152*b1cdbd2cSJim Jagielski typename vigra::NumericTraits< T >::isScalar,
153*b1cdbd2cSJim Jagielski trueCase,
154*b1cdbd2cSJim Jagielski falseCase >::type >::type type;
155*b1cdbd2cSJim Jagielski };
156*b1cdbd2cSJim Jagielski
157*b1cdbd2cSJim Jagielski /// Results in VigraTrueType, if both T1 and T2 are of integer type and scalar
158*b1cdbd2cSJim Jagielski template< typename T1, typename T2, typename trueCase, typename falseCase >
159*b1cdbd2cSJim Jagielski struct ifBothScalarIntegral
160*b1cdbd2cSJim Jagielski {
161*b1cdbd2cSJim Jagielski typedef
162*b1cdbd2cSJim Jagielski typename ifScalarIntegral<
163*b1cdbd2cSJim Jagielski T1,
164*b1cdbd2cSJim Jagielski typename ifScalarIntegral<
165*b1cdbd2cSJim Jagielski T2,
166*b1cdbd2cSJim Jagielski trueCase,
167*b1cdbd2cSJim Jagielski falseCase >::type,
168*b1cdbd2cSJim Jagielski falseCase >::type type;
169*b1cdbd2cSJim Jagielski };
170*b1cdbd2cSJim Jagielski
171*b1cdbd2cSJim Jagielski //--------------------------------------------------------------
172*b1cdbd2cSJim Jagielski
173*b1cdbd2cSJim Jagielski /// Count number of trailing zeros
174*b1cdbd2cSJim Jagielski template< unsigned int val > struct numberOfTrailingZeros
175*b1cdbd2cSJim Jagielski {
176*b1cdbd2cSJim Jagielski enum { next = val >> 1 };
177*b1cdbd2cSJim Jagielski enum { value = vigra::IfBool< (val & 1) == 0,
178*b1cdbd2cSJim Jagielski numberOfTrailingZeros<next>,
179*b1cdbd2cSJim Jagielski boost::mpl::integral_c< int,-1 > > ::type::value + 1 };
180*b1cdbd2cSJim Jagielski };
181*b1cdbd2cSJim Jagielski
182*b1cdbd2cSJim Jagielski template<> struct numberOfTrailingZeros<0>
183*b1cdbd2cSJim Jagielski {
184*b1cdbd2cSJim Jagielski enum { value = 0 };
185*b1cdbd2cSJim Jagielski };
186*b1cdbd2cSJim Jagielski
187*b1cdbd2cSJim Jagielski //--------------------------------------------------------------
188*b1cdbd2cSJim Jagielski
189*b1cdbd2cSJim Jagielski /// Count number of one bits
190*b1cdbd2cSJim Jagielski template< unsigned int val > struct bitcount
191*b1cdbd2cSJim Jagielski {
192*b1cdbd2cSJim Jagielski enum { next = val >> 1 };
193*b1cdbd2cSJim Jagielski enum { value = bitcount<next>::value + (val & 1) };
194*b1cdbd2cSJim Jagielski };
195*b1cdbd2cSJim Jagielski
196*b1cdbd2cSJim Jagielski template<> struct bitcount<0>
197*b1cdbd2cSJim Jagielski {
198*b1cdbd2cSJim Jagielski enum { value = 0 };
199*b1cdbd2cSJim Jagielski };
200*b1cdbd2cSJim Jagielski
201*b1cdbd2cSJim Jagielski //--------------------------------------------------------------
202*b1cdbd2cSJim Jagielski
203*b1cdbd2cSJim Jagielski /// Shift left for positive shift value, and right otherwise
shiftLeft(T v,int shift)204*b1cdbd2cSJim Jagielski template< typename T > inline T shiftLeft( T v, int shift )
205*b1cdbd2cSJim Jagielski {
206*b1cdbd2cSJim Jagielski return shift > 0 ? v << shift : v >> (-shift);
207*b1cdbd2cSJim Jagielski }
208*b1cdbd2cSJim Jagielski
209*b1cdbd2cSJim Jagielski /// Shift right for positive shift value, and left otherwise
shiftRight(T v,int shift)210*b1cdbd2cSJim Jagielski template< typename T > inline T shiftRight( T v, int shift )
211*b1cdbd2cSJim Jagielski {
212*b1cdbd2cSJim Jagielski return shift > 0 ? v >> shift : v << (-shift);
213*b1cdbd2cSJim Jagielski }
214*b1cdbd2cSJim Jagielski
215*b1cdbd2cSJim Jagielski
216*b1cdbd2cSJim Jagielski } // namespace basebmp
217*b1cdbd2cSJim Jagielski
218*b1cdbd2cSJim Jagielski #endif /* INCLUDED_BASEBMP_METAFUNCTIONS_HXX */
219