bump product version to 4.2.0.1
[LibreOffice.git] / include / basebmp / accessoradapters.hxx
blobf4a05dc331c2c4ba1cff09646e186cab63a26636
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 namespace basebmp
28 /** Interpose given accessor's set and get methods with two unary
29 functors.
31 @tpl WrappedAccessor
32 Wrapped type must provide the usual get and set accessor methods,
33 with the usual signatures (see StandardAccessor for a conforming
34 example).
36 @tpl GetterFunctor
37 An Adaptable Unary Function (i.e. providing result_type and
38 argument_type typedefs)
40 @tpl SetterFunctor
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
48 public:
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
54 private:
55 template<class A, typename G, typename S> friend class UnaryFunctionAccessorAdapter;
56 #endif
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;
64 public:
65 UnaryFunctionAccessorAdapter() :
66 maAccessor(),
67 maGetterFunctor(),
68 maSetterFunctor()
71 template< class A > explicit
72 UnaryFunctionAccessorAdapter( UnaryFunctionAccessorAdapter< A,
73 GetterFunctor,
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 ),
82 maGetterFunctor(),
83 maSetterFunctor()
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
129 maAccessor.set(
130 maSetterFunctor(
131 vigra::detail::RequiresExplicitCast<argument_type>::cast(value) ),
132 i );
135 template< typename V, class Iterator, class Difference >
136 void set(V const& value, Iterator const& i, Difference const& diff) const
138 maAccessor.set(
139 maSetterFunctor(
140 vigra::detail::RequiresExplicitCast<argument_type>::cast(value) ),
142 diff );
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.
154 @tpl WrappedAccessor
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.
159 @tpl SetterFunctor
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
166 public:
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
172 private:
173 template<class A, typename S> friend class BinarySetterFunctionAccessorAdapter;
174 #endif
176 WrappedAccessor maAccessor;
177 SetterFunctor maFunctor;
179 public:
180 BinarySetterFunctionAccessorAdapter() :
181 maAccessor(),
182 maFunctor()
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 ),
195 maFunctor()
198 template< class T > BinarySetterFunctionAccessorAdapter( T accessor,
199 SetterFunctor functor ) :
200 maAccessor( accessor ),
201 maFunctor( functor )
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
237 maAccessor.set(
238 maFunctor(maAccessor(i),
239 vigra::detail::RequiresExplicitCast<argument_type>::cast(value)),
240 i );
243 template< typename V, class Iterator, class Difference >
244 void set(V const& value, Iterator const& i, Difference const& diff) const
246 maAccessor.set(
247 maFunctor(maAccessor(i,diff),
248 vigra::detail::RequiresExplicitCast<argument_type>::cast(value)),
250 diff );
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)
274 @tpl Functor
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
282 public:
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
288 private:
289 template<class A1, class A2, typename F> friend class TernarySetterFunctionAccessorAdapter;
290 #endif
292 WrappedAccessor1 ma1stAccessor;
293 WrappedAccessor2 ma2ndAccessor;
294 Functor maFunctor;
296 public:
297 TernarySetterFunctionAccessorAdapter() :
298 ma1stAccessor(),
299 ma2ndAccessor(),
300 maFunctor()
303 template< class T > explicit TernarySetterFunctionAccessorAdapter( T const& accessor ) :
304 ma1stAccessor( accessor ),
305 ma2ndAccessor(),
306 maFunctor()
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,
321 T2 accessor2 ) :
322 ma1stAccessor( accessor1 ),
323 ma2ndAccessor( accessor2 ),
324 maFunctor()
327 template< class T1, class T2 >
328 TernarySetterFunctionAccessorAdapter( T1 accessor1,
329 T2 accessor2,
330 Functor func ) :
331 ma1stAccessor( accessor1 ),
332 ma2ndAccessor( accessor2 ),
333 maFunctor( func )
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
373 ma1stAccessor.set(
374 maFunctor(ma1stAccessor(i.first()),
375 ma2ndAccessor(i.second()),
376 vigra::detail::RequiresExplicitCast<argument_type>::cast(value)),
377 i.first() );
380 template< typename V, class Iterator, class Difference >
381 void set(V const& value, Iterator const& i, Difference const& diff) const
383 ma1stAccessor.set(
384 maFunctor(ma1stAccessor(i.first(), diff),
385 ma2ndAccessor(i.second(),diff),
386 vigra::detail::RequiresExplicitCast<argument_type>::cast(value)),
387 i.first(),
388 diff );
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
401 value types
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
407 value_type.
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
413 value_type.
415 template< class WrappedAccessor1,
416 class WrappedAccessor2 > class JoinImageAccessorAdapter
418 public:
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
427 private:
428 template<class A1, class A2> friend class JoinImageAccessorAdapter;
429 #endif
431 WrappedAccessor1 ma1stAccessor;
432 WrappedAccessor2 ma2ndAccessor;
434 public:
435 JoinImageAccessorAdapter() :
436 ma1stAccessor(),
437 ma2ndAccessor()
440 template< class T > explicit JoinImageAccessorAdapter( T const& accessor ) :
441 ma1stAccessor( accessor ),
442 ma2ndAccessor()
445 template< class A1, class A2 > explicit
446 JoinImageAccessorAdapter(
447 JoinImageAccessorAdapter< A1,
448 A2 > const& rSrc ) :
449 ma1stAccessor( rSrc.ma1stAccessor ),
450 ma2ndAccessor( rSrc.ma2ndAccessor )
453 template< class T1, class T2 >
454 JoinImageAccessorAdapter( T1 accessor1,
455 T2 accessor2 ) :
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
489 ma1stAccessor.set(
490 vigra::detail::RequiresExplicitCast<typename WrappedAccessor1::value_type>::cast(
491 value.first),
492 i.first() );
493 ma2ndAccessor.set(
494 vigra::detail::RequiresExplicitCast<typename WrappedAccessor2::value_type>::cast(
495 value.second),
496 i.second() );
499 template< typename V, class Iterator, class Difference >
500 void set(V const& value, Iterator const& i, Difference const& diff) const
502 ma1stAccessor.set(
503 vigra::detail::RequiresExplicitCast<typename WrappedAccessor1::value_type>::cast(
504 value.first),
505 i.first(),
506 diff );
507 ma2ndAccessor.set(
508 vigra::detail::RequiresExplicitCast<typename WrappedAccessor2::value_type>::cast(
509 value.second),
510 i.second(),
511 diff );
516 } // namespace basebmp
518 #endif /* INCLUDED_BASEBMP_ACCESSORADAPTERS_HXX */
520 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */