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