1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_BASEBMP_METAFUNCTIONS_HXX
21 #define INCLUDED_BASEBMP_METAFUNCTIONS_HXX
23 #include <boost/mpl/integral_c.hpp>
24 #include <vigra/metaprogramming.hxx>
25 #include <vigra/numerictraits.hxx>
32 // TODO(Q3): move to generic place (o3tl?)
34 /** template meta function: add const qualifier to 2nd type, if given
37 template<typename A
, typename B
> struct clone_const
41 template<typename A
, typename B
> struct clone_const
<const A
,B
>
46 /** template meta function: add const qualifier to plain type (if not
49 template <typename T
> struct add_const
53 template <typename T
> struct add_const
<const T
>
58 /// template meta function: remove const qualifier from plain type
59 template <typename T
> struct remove_const
63 template <typename T
> struct remove_const
<const T
>
68 //--------------------------------------------------------------
70 /// Base class for an adaptable ternary functor
71 template< typename A1
, typename A2
, typename A3
, typename R
> struct TernaryFunctorBase
73 typedef A1 first_argument_type
;
74 typedef A2 second_argument_type
;
75 typedef A3 third_argument_type
;
76 typedef R result_type
;
79 //--------------------------------------------------------------
81 /** template meta function: ensure that given integer type is unsigned
83 If given integer type is already unsigned, return as-is -
84 otherwise, convert to unsigned type of same or greater range.
86 template< typename T
> struct make_unsigned
;
88 #define BASEBMP_MAKE_UNSIGNED(T,U) \
89 template<> struct make_unsigned<T> { \
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)
102 #undef BASEBMP_MAKE_UNSIGNED
104 /// cast integer to unsigned type of similar size
105 template< typename T
> inline typename make_unsigned
<T
>::type
unsigned_cast( T value
)
107 return static_cast< typename make_unsigned
<T
>::type
>(value
);
110 //--------------------------------------------------------------
112 /// returns true, if given number is strictly less than 0
113 template< typename T
> inline bool is_negative( T x
)
118 /// Overload for ints (branch-free)
119 inline bool is_negative( int x
)
121 // force logic shift (result for signed shift right is undefined)
122 return static_cast<unsigned int>(x
) >> (sizeof(int)*8-1);
125 //--------------------------------------------------------------
127 /// Results in VigraTrueType, if T is of integer type and scalar
128 template< typename T
, typename trueCase
, typename falseCase
>
129 struct ifScalarIntegral
133 typename
vigra::NumericTraits
< T
>::isIntegral
,
135 typename
vigra::NumericTraits
< T
>::isScalar
,
138 falseCase
>::type type
;
141 /// Results in VigraTrueType, if T is of non-integer type and scalar
142 template< typename T
, typename trueCase
, typename falseCase
>
143 struct ifScalarNonIntegral
147 typename
vigra::NumericTraits
< T
>::isIntegral
,
150 typename
vigra::NumericTraits
< T
>::isScalar
,
152 falseCase
>::type
>::type type
;
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
160 typename ifScalarIntegral
<
162 typename ifScalarIntegral
<
166 falseCase
>::type type
;
169 //--------------------------------------------------------------
171 /// Count number of trailing zeros
172 template< unsigned int val
> struct numberOfTrailingZeros
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 };
180 template<> struct numberOfTrailingZeros
<0>
185 //--------------------------------------------------------------
187 /// Count number of one bits
188 template< unsigned int val
> struct bitcount
190 enum { next
= val
>> 1 };
191 enum { value
= bitcount
<next
>::value
+ (val
& 1) };
194 template<> struct bitcount
<0>
199 //--------------------------------------------------------------
201 /// Shift left for positive shift value, and right otherwise
202 template< typename T
> inline T
shiftLeft( T v
, int shift
)
204 return shift
> 0 ? v
<< shift
: v
>> (-shift
);
207 /// Shift right for positive shift value, and left otherwise
208 template< typename T
> inline T
shiftRight( T v
, int shift
)
210 return shift
> 0 ? v
>> shift
: v
<< (-shift
);
213 //--------------------------------------------------------------
215 /// Replace non-std project2nd from SGI extensions
216 template< typename T1
, typename T2
>
217 struct project2nd
: public std::binary_function
<T1
, T2
, T2
>
219 T2
operator() (const T1
&, const T2
& v
) const { return v
; }
222 } // namespace basebmp
224 #endif /* INCLUDED_BASEBMP_METAFUNCTIONS_HXX */
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */