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_ACCESSORFUNCTORS_HXX
21 #define INCLUDED_BASEBMP_ACCESSORFUNCTORS_HXX
23 #include <osl/diagnose.h>
24 #include <basebmp/metafunctions.hxx>
31 // Some common accessor functors
32 // ------------------------------------------------------------
35 /// combine two values via XOR
36 template< typename T
> struct XorFunctor
: public std::binary_function
<T
,T
,T
>
38 T
operator()( T v1
, T v2
) const { return v1
^ v2
; }
41 //-----------------------------------------------------------------------------
43 /// Base class, passing on the arg types
44 template< typename T
, typename M
> struct MaskFunctorBase
:
45 public TernaryFunctorBase
<T
,M
,T
,T
> {};
48 /** Let a mask flag decide between two values
51 Mask polarity. When true, a false in the mask denotes
52 transparency, i.e. the original value will display. And vice
57 bool polarity
> struct GenericOutputMaskFunctor
: public MaskFunctorBase
<T
,M
>
59 /// Ternary mask operation - selects v1 for !m == polarity, v2 otherwise
60 T
operator()( T v1
, M m
, T v2
) const
62 return !m
== polarity
? v1
: v2
;
66 /** Let a mask bit decide between two values (specialization for
71 bool polarity
> struct IntegerOutputMaskFunctor
;
73 typename M
> struct IntegerOutputMaskFunctor
<T
,M
,true> : public MaskFunctorBase
<T
,M
>
75 /** Mask v with state of m
77 @return v2, if m != 0, v1 otherwise.
79 T
operator()( T v1
, M m
, T v2
) const
81 typedef typename make_unsigned
<T
>::type unsigned_T
;
83 // mask will be 0, iff m == 0, and 1 otherwise
84 const T
mask( unsigned_cast
<T
>(m
| -m
) >> (sizeof(unsigned_T
)*8 - 1) );
85 return v1
*(M
)(1-mask
) + v2
*mask
;
89 typename M
> struct IntegerOutputMaskFunctor
<T
,M
,false> : public MaskFunctorBase
<T
,M
>
91 /** Mask v with state of m
93 @return v2, if m != 0, v1 otherwise.
95 T
operator()( T v1
, M m
, T v2
) const
97 typedef typename make_unsigned
<T
>::type unsigned_T
;
99 // mask will be 0, iff m == 0, and 1 otherwise
100 const T
mask( unsigned_cast
<T
>(m
| -m
) >> (sizeof(unsigned_T
)*8 - 1) );
101 return v1
*mask
+ v2
*(M
)(1-mask
);
105 /** Let a mask bit decide between two values (specialization for
106 binary-valued mask types)
108 template< typename T
, typename M
, bool polarity
> struct FastIntegerOutputMaskFunctor
;
109 template< typename T
, typename M
> struct FastIntegerOutputMaskFunctor
<T
,M
,true> :
110 public MaskFunctorBase
<T
,M
>
112 /// Specialization, only valid if mask can only attain 0 or 1
113 T
operator()( T v1
, M m
, T v2
) const
117 return v1
*(M
)(1-m
) + v2
*m
;
120 template< typename T
, typename M
> struct FastIntegerOutputMaskFunctor
<T
,M
,false> :
121 public MaskFunctorBase
<T
,M
>
123 /// Specialization, only valid if mask can only attain 0 or 1
124 T
operator()( T v1
, M m
, T v2
) const
128 return v1
*m
+ v2
*(M
)(1-m
);
132 //-----------------------------------------------------------------------------
134 /** Split a pair value from a JoinImageAccessorAdapter into its
135 individual values, and pass it on to a ternary functor
137 This wrapper is an adaptable binary functor, and can thus be used
138 with a BinarySetterFunctionAccessorAdapter. Useful e.g. for
139 out-of-image alpha channel, or a masked image.
142 An adaptable ternary functor (as can e.g. be passed to the
143 TernarySetterFunctionAccessorAdapter)
145 template< typename Functor
> struct BinaryFunctorSplittingWrapper
:
146 public std::binary_function
<typename
Functor::first_argument_type
,
147 std::pair
<typename
Functor::third_argument_type
,
148 typename
Functor::second_argument_type
>,
149 typename
Functor::result_type
>
151 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
152 // making all members public, if no member template friends
154 template<class A
> friend struct BinaryFunctorSplittingWrapper
;
159 BinaryFunctorSplittingWrapper() : maFunctor() {}
161 template< class A
> explicit
162 BinaryFunctorSplittingWrapper(
163 BinaryFunctorSplittingWrapper
<A
> const& src
) : maFunctor(src
.maFunctor
) {}
165 template< class F
> explicit
166 BinaryFunctorSplittingWrapper( F
const& func
) : maFunctor(func
) {}
168 typename
Functor::result_type
operator()(
169 typename
Functor::first_argument_type v1
,
170 std::pair
< typename
Functor::third_argument_type
,
171 typename
Functor::second_argument_type
> const& v2
) const
173 return maFunctor( v1
, v2
.second
, v2
.first
);
177 } // namespace basebmp
179 #endif /* INCLUDED_BASEBMP_ACCESSORFUNCTORS_HXX */
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */