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_ACCESSORADAPTERS_HXX
21 #define INCLUDED_BASEBMP_ACCESSORADAPTERS_HXX
23 #include <vigra/numerictraits.hxx>
28 /** Interpose given accessor's set and get methods with two unary
32 Wrapped type must provide the usual get and set accessor methods,
33 with the usual signatures (see StandardAccessor for a conforming
37 An Adaptable Unary Function (i.e. providing result_type and
38 argument_type typedefs)
41 An Adaptable Unary Function (i.e. providing result_type and
42 argument_type typedefs)
44 template< class WrappedAccessor
,
45 typename GetterFunctor
,
46 typename SetterFunctor
> class UnaryFunctionAccessorAdapter
49 typedef typename
GetterFunctor::result_type value_type
;
50 typedef typename
SetterFunctor::argument_type argument_type
;
52 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
53 // making all members public, if no member template friends
55 template<class A
, typename G
, typename S
> friend class UnaryFunctionAccessorAdapter
;
58 // we don't derive from wrapped type, to avoid ambiguities
59 // regarding templatized getter/setter methods.
60 WrappedAccessor maAccessor
;
61 GetterFunctor maGetterFunctor
;
62 SetterFunctor maSetterFunctor
;
65 UnaryFunctionAccessorAdapter() :
71 template< class A
> explicit
72 UnaryFunctionAccessorAdapter( UnaryFunctionAccessorAdapter
< A
,
74 SetterFunctor
> const& rSrc
) :
75 maAccessor( rSrc
.maAccessor
),
76 maGetterFunctor( rSrc
.maGetterFunctor
),
77 maSetterFunctor( rSrc
.maSetterFunctor
)
80 template< class T
> explicit UnaryFunctionAccessorAdapter( T
const& accessor
) :
81 maAccessor( accessor
),
86 template< class T
> UnaryFunctionAccessorAdapter( T accessor
,
87 GetterFunctor getterFunctor
,
88 SetterFunctor setterFunctor
) :
89 maAccessor( accessor
),
90 maGetterFunctor( getterFunctor
),
91 maSetterFunctor( setterFunctor
)
94 // -------------------------------------------------------
96 WrappedAccessor
const& getWrappedAccessor() const { return maAccessor
; }
97 WrappedAccessor
& getWrappedAccessor() { return maAccessor
; }
99 // -------------------------------------------------------
101 value_type
getter(typename
GetterFunctor::argument_type v
) const
103 return maGetterFunctor(v
);
105 typename
SetterFunctor::result_type
setter(argument_type v
) const
107 return maSetterFunctor(v
);
110 // -------------------------------------------------------
112 template< class Iterator
>
113 value_type
operator()(Iterator
const& i
) const
115 return maGetterFunctor( maAccessor(i
) );
118 template< class Iterator
, class Difference
>
119 value_type
operator()(Iterator
const& i
, Difference
const& diff
) const
121 return maGetterFunctor( maAccessor(i
,diff
) );
124 // -------------------------------------------------------
126 template< typename V
, class Iterator
>
127 void set(V
const& value
, Iterator
const& i
) const
131 vigra::detail::RequiresExplicitCast
<argument_type
>::cast(value
) ),
135 template< typename V
, class Iterator
, class Difference
>
136 void set(V
const& value
, Iterator
const& i
, Difference
const& diff
) const
140 vigra::detail::RequiresExplicitCast
<argument_type
>::cast(value
) ),
147 //-----------------------------------------------------------------------------
149 /** Interpose given accessor's set methods with a binary function,
150 taking both old and new value.
152 The wrappee's getter methods kept as-is.
155 Wrapped type must provide the usual get and set accessor methods,
156 with the usual signatures (see StandardAccessor for a conforming
157 example). Furthermore, must provide a nested typedef value_type.
160 An adaptable binary function (i.e. providing nested typedefs for
161 result_type and first and second argument type)
163 template< class WrappedAccessor
,
164 typename SetterFunctor
> class BinarySetterFunctionAccessorAdapter
167 typedef typename
WrappedAccessor::value_type value_type
;
168 typedef typename
SetterFunctor::second_argument_type argument_type
;
170 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
171 // making all members public, if no member template friends
173 template<class A
, typename S
> friend class BinarySetterFunctionAccessorAdapter
;
176 WrappedAccessor maAccessor
;
177 SetterFunctor maFunctor
;
180 BinarySetterFunctionAccessorAdapter() :
185 template< class A
> explicit
186 BinarySetterFunctionAccessorAdapter(
187 BinarySetterFunctionAccessorAdapter
< A
,
188 SetterFunctor
> const& rSrc
) :
189 maAccessor( rSrc
.maAccessor
),
190 maFunctor( rSrc
.maFunctor
)
193 template< class T
> explicit BinarySetterFunctionAccessorAdapter( T
const& accessor
) :
194 maAccessor( accessor
),
198 template< class T
> BinarySetterFunctionAccessorAdapter( T accessor
,
199 SetterFunctor functor
) :
200 maAccessor( accessor
),
204 // -------------------------------------------------------
206 WrappedAccessor
const& getWrappedAccessor() const { return maAccessor
; }
207 WrappedAccessor
& getWrappedAccessor() { return maAccessor
; }
209 // -------------------------------------------------------
211 typename
SetterFunctor::result_type
setter(
212 typename
SetterFunctor::first_argument_type v1
,
213 argument_type v2
) const
215 return maSetterFunctor(v1
,v2
);
218 // -------------------------------------------------------
220 template< class Iterator
>
221 value_type
operator()(Iterator
const& i
) const
223 return maAccessor(i
);
226 template< class Iterator
, class Difference
>
227 value_type
operator()(Iterator
const& i
, Difference
const& diff
) const
229 return maAccessor(i
,diff
);
232 // -------------------------------------------------------
234 template< typename V
, class Iterator
>
235 void set(V
const& value
, Iterator
const& i
) const
238 maFunctor(maAccessor(i
),
239 vigra::detail::RequiresExplicitCast
<argument_type
>::cast(value
)),
243 template< typename V
, class Iterator
, class Difference
>
244 void set(V
const& value
, Iterator
const& i
, Difference
const& diff
) const
247 maFunctor(maAccessor(i
,diff
),
248 vigra::detail::RequiresExplicitCast
<argument_type
>::cast(value
)),
255 //-----------------------------------------------------------------------------
257 /** Write through a CompositeIterator's first wrapped iterator, by
258 piping the first wrapped iterator value, the second iterator
259 value, and the specified new value through a ternary function.
261 Passed iterator must fulfill the CompositeIterator concept. Note
262 that the getter/setter methods are not templatized regarding the
263 iterator type, to make the mask calculation optimization below
264 safe (see the maskedAccessor template metafunction below)
266 @tpl WrappedAccessor1
267 Wrapped type must provide the usual get and set accessor methods,
268 with the usual signatures (see StandardAccessor for a conforming
269 example). Furthermore, the type must provide a nested typedef
270 value_type (the selection of WrappedAccessor1 as the provider for
271 that typedef is rather arbitrary. Could have been
272 WrappedAccessor2, too. So sue me)
275 An adaptable ternary function (i.e. providing nested typedefs for
276 result_type and first, second and third argument type)
278 template< class WrappedAccessor1
,
279 class WrappedAccessor2
,
280 typename Functor
> class TernarySetterFunctionAccessorAdapter
283 typedef typename
WrappedAccessor1::value_type value_type
;
284 typedef typename
Functor::third_argument_type argument_type
;
286 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
287 // making all members public, if no member template friends
289 template<class A1
, class A2
, typename F
> friend class TernarySetterFunctionAccessorAdapter
;
292 WrappedAccessor1 ma1stAccessor
;
293 WrappedAccessor2 ma2ndAccessor
;
297 TernarySetterFunctionAccessorAdapter() :
303 template< class T
> explicit TernarySetterFunctionAccessorAdapter( T
const& accessor
) :
304 ma1stAccessor( accessor
),
309 template< class A1
, class A2
> explicit
310 TernarySetterFunctionAccessorAdapter(
311 TernarySetterFunctionAccessorAdapter
< A1
,
313 Functor
> const& rSrc
) :
314 ma1stAccessor( rSrc
.ma1stAccessor
),
315 ma2ndAccessor( rSrc
.ma2ndAccessor
),
316 maFunctor( rSrc
.maFunctor
)
319 template< class T1
, class T2
>
320 TernarySetterFunctionAccessorAdapter( T1 accessor1
,
322 ma1stAccessor( accessor1
),
323 ma2ndAccessor( accessor2
),
327 template< class T1
, class T2
>
328 TernarySetterFunctionAccessorAdapter( T1 accessor1
,
331 ma1stAccessor( accessor1
),
332 ma2ndAccessor( accessor2
),
336 // -------------------------------------------------------
338 WrappedAccessor1
const& get1stWrappedAccessor() const { return ma1stAccessor
; }
339 WrappedAccessor1
& get1stWrappedAccessor() { return ma1stAccessor
; }
341 WrappedAccessor2
const& get2ndWrappedAccessor() const { return ma2ndAccessor
; }
342 WrappedAccessor2
& get2ndWrappedAccessor() { return ma2ndAccessor
; }
344 // -------------------------------------------------------
346 typename
Functor::result_type
setter(
347 typename
Functor::first_argument_type v1
,
348 typename
Functor::second_argument_type v2
,
349 argument_type v3
) const
351 return maSetterFunctor(v1
,v2
,v3
);
354 // -------------------------------------------------------
356 template< class Iterator
>
357 value_type
operator()(Iterator
const& i
) const
359 return ma1stAccessor(i
.first());
362 template< class Iterator
, class Difference
>
363 value_type
operator()(Iterator
const& i
, Difference
const& diff
) const
365 return ma1stAccessor(i
.second(),diff
);
368 // -------------------------------------------------------
370 template< typename V
, class Iterator
>
371 void set(V
const& value
, Iterator
const& i
) const
374 maFunctor(ma1stAccessor(i
.first()),
375 ma2ndAccessor(i
.second()),
376 vigra::detail::RequiresExplicitCast
<argument_type
>::cast(value
)),
380 template< typename V
, class Iterator
, class Difference
>
381 void set(V
const& value
, Iterator
const& i
, Difference
const& diff
) const
384 maFunctor(ma1stAccessor(i
.first(), diff
),
385 ma2ndAccessor(i
.second(),diff
),
386 vigra::detail::RequiresExplicitCast
<argument_type
>::cast(value
)),
393 //-----------------------------------------------------------------------------
395 /** Access two distinct images simultaneously
397 Passed iterator must fulfill the CompositeIterator concept
398 (i.e. wrap the two image's iterators into one
399 CompositeIterator). The getter and setter methods expect and
400 return a pair of values, with types equal to the two accessors
403 @tpl WrappedAccessor1
404 Wrapped type must provide the usual get and set accessor methods,
405 with the usual signatures (see StandardAccessor for a conforming
406 example). Furthermore, the type must provide a nested typedef
409 @tpl WrappedAccessor2
410 Wrapped type must provide the usual get and set accessor methods,
411 with the usual signatures (see StandardAccessor for a conforming
412 example). Furthermore, the type must provide a nested typedef
415 template< class WrappedAccessor1
,
416 class WrappedAccessor2
> class JoinImageAccessorAdapter
419 // TODO(F3): Need numeric traits and a few free functions to
420 // actually calculate with a pair (semantic: apply every operation
421 // individually to the contained types)
422 typedef std::pair
<typename
WrappedAccessor1::value_type
,
423 typename
WrappedAccessor2::value_type
> value_type
;
425 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
426 // making all members public, if no member template friends
428 template<class A1
, class A2
> friend class JoinImageAccessorAdapter
;
431 WrappedAccessor1 ma1stAccessor
;
432 WrappedAccessor2 ma2ndAccessor
;
435 JoinImageAccessorAdapter() :
440 template< class T
> explicit JoinImageAccessorAdapter( T
const& accessor
) :
441 ma1stAccessor( accessor
),
445 template< class A1
, class A2
> explicit
446 JoinImageAccessorAdapter(
447 JoinImageAccessorAdapter
< A1
,
449 ma1stAccessor( rSrc
.ma1stAccessor
),
450 ma2ndAccessor( rSrc
.ma2ndAccessor
)
453 template< class T1
, class T2
>
454 JoinImageAccessorAdapter( T1 accessor1
,
456 ma1stAccessor( accessor1
),
457 ma2ndAccessor( accessor2
)
460 // -------------------------------------------------------
462 WrappedAccessor1
const& get1stWrappedAccessor() const { return ma1stAccessor
; }
463 WrappedAccessor1
& get1stWrappedAccessor() { return ma1stAccessor
; }
465 WrappedAccessor2
const& get2ndWrappedAccessor() const { return ma2ndAccessor
; }
466 WrappedAccessor2
& get2ndWrappedAccessor() { return ma2ndAccessor
; }
468 // -------------------------------------------------------
470 template< class Iterator
>
471 value_type
operator()(Iterator
const& i
) const
473 return std::make_pair(ma1stAccessor(i
.first()),
474 ma2ndAccessor(i
.second()));
477 template< class Iterator
, class Difference
>
478 value_type
operator()(Iterator
const& i
, Difference
const& diff
) const
480 return std::make_pair(ma1stAccessor(i
.first(),diff
),
481 ma2ndAccessor(i
.second(),diff
));
484 // -------------------------------------------------------
486 template< typename V
, class Iterator
>
487 void set(V
const& value
, Iterator
const& i
) const
490 vigra::detail::RequiresExplicitCast
<typename
WrappedAccessor1::value_type
>::cast(
494 vigra::detail::RequiresExplicitCast
<typename
WrappedAccessor2::value_type
>::cast(
499 template< typename V
, class Iterator
, class Difference
>
500 void set(V
const& value
, Iterator
const& i
, Difference
const& diff
) const
503 vigra::detail::RequiresExplicitCast
<typename
WrappedAccessor1::value_type
>::cast(
508 vigra::detail::RequiresExplicitCast
<typename
WrappedAccessor2::value_type
>::cast(
516 } // namespace basebmp
518 #endif /* INCLUDED_BASEBMP_ACCESSORADAPTERS_HXX */
520 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */