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