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