LCOV - code coverage report
Current view: top level - include/basebmp - accessoradapters.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 36 38 94.7 %
Date: 2014-11-03 Functions: 125 671 18.6 %
Legend: Lines: hit not hit

          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_ACCESSORADAPTERS_HXX
      21             : #define INCLUDED_BASEBMP_ACCESSORADAPTERS_HXX
      22             : 
      23             : #include <vigra/numerictraits.hxx>
      24             : 
      25             : namespace basebmp
      26             : {
      27             : 
      28             : /** Interpose given accessor's set and get methods with two unary
      29             :     functors.
      30             : 
      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).
      35             : 
      36             :     @tpl GetterFunctor
      37             :     An Adaptable Unary Function (i.e. providing result_type and
      38             :     argument_type typedefs)
      39             : 
      40             :     @tpl SetterFunctor
      41             :     An Adaptable Unary Function (i.e. providing result_type and
      42             :     argument_type typedefs)
      43             :  */
      44             : template< class WrappedAccessor,
      45             :           typename GetterFunctor,
      46             :           typename SetterFunctor > class UnaryFunctionAccessorAdapter
      47             : {
      48             : public:
      49             :     typedef typename GetterFunctor::result_type   value_type;
      50             :     typedef typename SetterFunctor::argument_type argument_type;
      51             : 
      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
      57             : 
      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;
      63             : 
      64             : public:
      65     1536150 :     UnaryFunctionAccessorAdapter() :
      66             :         maAccessor(),
      67             :         maGetterFunctor(),
      68             :         maSetterFunctor()
      69     1536150 :     {}
      70             : 
      71             :     template< class A > explicit
      72     4608450 :     UnaryFunctionAccessorAdapter( UnaryFunctionAccessorAdapter< A,
      73             :                                                                 GetterFunctor,
      74             :                                                                 SetterFunctor > const& rSrc ) :
      75             :         maAccessor( rSrc.maAccessor ),
      76             :         maGetterFunctor( rSrc.maGetterFunctor ),
      77     4608450 :         maSetterFunctor( rSrc.maSetterFunctor )
      78     4608450 :     {}
      79             : 
      80             :     template< class T > explicit UnaryFunctionAccessorAdapter( T const& accessor ) :
      81             :         maAccessor( accessor ),
      82             :         maGetterFunctor(),
      83             :         maSetterFunctor()
      84             :     {}
      85             : 
      86             :     template< class T > UnaryFunctionAccessorAdapter( T             accessor,
      87             :                                                       GetterFunctor getterFunctor,
      88             :                                                       SetterFunctor setterFunctor) :
      89             :         maAccessor( accessor ),
      90             :         maGetterFunctor( getterFunctor ),
      91             :         maSetterFunctor( setterFunctor )
      92             :     {}
      93             : 
      94             : 
      95             : 
      96             :     WrappedAccessor const& getWrappedAccessor() const { return maAccessor; }
      97             :     WrappedAccessor&       getWrappedAccessor() { return maAccessor; }
      98             : 
      99             : 
     100             : 
     101             :     value_type getter(typename GetterFunctor::argument_type v) const
     102             :     {
     103             :         return maGetterFunctor(v);
     104             :     }
     105     8205000 :     typename SetterFunctor::result_type setter(argument_type v) const
     106             :     {
     107     8205000 :         return maSetterFunctor(v);
     108             :     }
     109             : 
     110             : 
     111             : 
     112             :     template< class Iterator >
     113   274461771 :     value_type operator()(Iterator const& i) const
     114             :     {
     115   274461771 :         return maGetterFunctor( maAccessor(i) );
     116             :     }
     117             : 
     118             :     template< class Iterator, class Difference >
     119             :     value_type operator()(Iterator const& i, Difference const& diff) const
     120             :     {
     121             :         return maGetterFunctor( maAccessor(i,diff) );
     122             :     }
     123             : 
     124             : 
     125             : 
     126             :     template< typename V, class Iterator >
     127   215218432 :     void set(V const& value, Iterator const& i) const
     128             :     {
     129   215218432 :         maAccessor.set(
     130             :             maSetterFunctor(
     131   215218432 :                 vigra::detail::RequiresExplicitCast<argument_type>::cast(value) ),
     132             :             i );
     133   215218432 :     }
     134             : 
     135             :     template< typename V, class Iterator, class Difference >
     136             :     void set(V const& value, Iterator const& i, Difference const& diff) const
     137             :     {
     138             :         maAccessor.set(
     139             :             maSetterFunctor(
     140             :                 vigra::detail::RequiresExplicitCast<argument_type>::cast(value) ),
     141             :             i,
     142             :             diff );
     143             :     }
     144             : 
     145             : };
     146             : 
     147             : 
     148             : 
     149             : /** Interpose given accessor's set methods with a binary function,
     150             :     taking both old and new value.
     151             : 
     152             :     The wrappee's getter methods kept as-is.
     153             : 
     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.
     158             : 
     159             :     @tpl SetterFunctor
     160             :     An adaptable binary function (i.e. providing nested typedefs for
     161             :     result_type and first and second argument type)
     162             :  */
     163             : template< class WrappedAccessor,
     164             :           typename SetterFunctor > class BinarySetterFunctionAccessorAdapter
     165             : {
     166             : public:
     167             :     typedef typename WrappedAccessor::value_type         value_type;
     168             :     typedef typename SetterFunctor::second_argument_type argument_type;
     169             : 
     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
     175             : 
     176             :     WrappedAccessor    maAccessor;
     177             :     SetterFunctor      maFunctor;
     178             : 
     179             : public:
     180             :     BinarySetterFunctionAccessorAdapter() :
     181             :         maAccessor(),
     182             :         maFunctor()
     183             :     {}
     184             : 
     185             :     template< class A > explicit
     186             :     BinarySetterFunctionAccessorAdapter(
     187             :         BinarySetterFunctionAccessorAdapter< A,
     188             :                                              SetterFunctor > const& rSrc ) :
     189             :         maAccessor( rSrc.maAccessor ),
     190             :         maFunctor( rSrc.maFunctor )
     191             :     {}
     192             : 
     193     7341211 :     template< class T > explicit BinarySetterFunctionAccessorAdapter( T const& accessor ) :
     194             :         maAccessor( accessor ),
     195     3680267 :         maFunctor()
     196     7341211 :     {}
     197             : 
     198             :     template< class T > BinarySetterFunctionAccessorAdapter( T             accessor,
     199             :                                                              SetterFunctor functor ) :
     200             :         maAccessor( accessor ),
     201             :         maFunctor( functor )
     202             :     {}
     203             : 
     204             : 
     205             : 
     206             :     WrappedAccessor const& getWrappedAccessor() const { return maAccessor; }
     207             :     WrappedAccessor&       getWrappedAccessor() { return maAccessor; }
     208             : 
     209             : 
     210             : 
     211             :     typename SetterFunctor::result_type setter(
     212             :         typename SetterFunctor::first_argument_type v1,
     213             :         argument_type                               v2 ) const
     214             :     {
     215             :         return maSetterFunctor(v1,v2);
     216             :     }
     217             : 
     218             : 
     219             : 
     220             :     template< class Iterator >
     221           0 :     value_type operator()(Iterator const& i) const
     222             :     {
     223           0 :         return maAccessor(i);
     224             :     }
     225             : 
     226             :     template< class Iterator, class Difference >
     227             :     value_type operator()(Iterator const& i, Difference const& diff) const
     228             :     {
     229             :         return maAccessor(i,diff);
     230             :     }
     231             : 
     232             : 
     233             : 
     234             :     template< typename V, class Iterator >
     235    83230908 :     void set(V const& value, Iterator const& i) const
     236             :     {
     237    83230908 :         maAccessor.set(
     238             :             maFunctor(maAccessor(i),
     239    83230632 :                       vigra::detail::RequiresExplicitCast<argument_type>::cast(value)),
     240             :             i );
     241    83230908 :     }
     242             : 
     243             :     template< typename V, class Iterator, class Difference >
     244             :     void set(V const& value, Iterator const& i, Difference const& diff) const
     245             :     {
     246             :         maAccessor.set(
     247             :             maFunctor(maAccessor(i,diff),
     248             :                       vigra::detail::RequiresExplicitCast<argument_type>::cast(value)),
     249             :             i,
     250             :             diff );
     251             :     }
     252             : 
     253             : };
     254             : 
     255             : 
     256             : 
     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.
     260             : 
     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)
     265             : 
     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)
     273             : 
     274             :     @tpl Functor
     275             :     An adaptable ternary function (i.e. providing nested typedefs for
     276             :     result_type and first, second and third argument type)
     277             :  */
     278             : template< class WrappedAccessor1,
     279             :           class WrappedAccessor2,
     280             :           typename Functor > class TernarySetterFunctionAccessorAdapter
     281             : {
     282             : public:
     283             :     typedef typename WrappedAccessor1::value_type value_type;
     284             :     typedef typename Functor::third_argument_type argument_type;
     285             : 
     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
     291             : 
     292             :     WrappedAccessor1 ma1stAccessor;
     293             :     WrappedAccessor2 ma2ndAccessor;
     294             :     Functor          maFunctor;
     295             : 
     296             : public:
     297             :     TernarySetterFunctionAccessorAdapter() :
     298             :         ma1stAccessor(),
     299             :         ma2ndAccessor(),
     300             :         maFunctor()
     301             :     {}
     302             : 
     303    14643776 :     template< class T > explicit TernarySetterFunctionAccessorAdapter( T const& accessor ) :
     304             :         ma1stAccessor( accessor ),
     305             :         ma2ndAccessor(),
     306     5491416 :         maFunctor()
     307    14643776 :     {}
     308             : 
     309             :     template< class A1, class A2 > explicit
     310             :     TernarySetterFunctionAccessorAdapter(
     311             :         TernarySetterFunctionAccessorAdapter< A1,
     312             :                                               A2,
     313             :                                               Functor > const& rSrc ) :
     314             :         ma1stAccessor( rSrc.ma1stAccessor ),
     315             :         ma2ndAccessor( rSrc.ma2ndAccessor ),
     316             :         maFunctor( rSrc.maFunctor )
     317             :     {}
     318             : 
     319             :     template< class T1, class T2 >
     320             :     TernarySetterFunctionAccessorAdapter( T1 accessor1,
     321             :                                           T2 accessor2 ) :
     322             :         ma1stAccessor( accessor1 ),
     323             :         ma2ndAccessor( accessor2 ),
     324             :         maFunctor()
     325             :     {}
     326             : 
     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 )
     334             :     {}
     335             : 
     336             : 
     337             : 
     338             :     WrappedAccessor1 const& get1stWrappedAccessor() const { return ma1stAccessor; }
     339         725 :     WrappedAccessor1&       get1stWrappedAccessor() { return ma1stAccessor; }
     340             : 
     341             :     WrappedAccessor2 const& get2ndWrappedAccessor() const { return ma2ndAccessor; }
     342             :     WrappedAccessor2&       get2ndWrappedAccessor() { return ma2ndAccessor; }
     343             : 
     344             : 
     345             : 
     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
     350             :     {
     351             :         return maSetterFunctor(v1,v2,v3);
     352             :     }
     353             : 
     354             : 
     355             : 
     356             :     template< class Iterator >
     357      208800 :     value_type operator()(Iterator const& i) const
     358             :     {
     359      208800 :         return ma1stAccessor(i.first());
     360             :     }
     361             : 
     362             :     template< class Iterator, class Difference >
     363             :     value_type operator()(Iterator const& i, Difference const& diff) const
     364             :     {
     365             :         return ma1stAccessor(i.second(),diff);
     366             :     }
     367             : 
     368             : 
     369             : 
     370             :     template< typename V, class Iterator >
     371  3150261680 :     void set(V const& value, Iterator const& i) const
     372             :     {
     373  6300522796 :         ma1stAccessor.set(
     374             :             maFunctor(ma1stAccessor(i.first()),
     375             :                       ma2ndAccessor(i.second()),
     376  3150261116 :                       vigra::detail::RequiresExplicitCast<argument_type>::cast(value)),
     377             :             i.first() );
     378  3150261680 :     }
     379             : 
     380             :     template< typename V, class Iterator, class Difference >
     381             :     void set(V const& value, Iterator const& i, Difference const& diff) const
     382             :     {
     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 );
     389             :     }
     390             : 
     391             : };
     392             : 
     393             : 
     394             : 
     395             : /** Access two distinct images simultaneously
     396             : 
     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
     402             : 
     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.
     408             : 
     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.
     414             :  */
     415             : template< class WrappedAccessor1,
     416     1466705 :           class WrappedAccessor2 > class JoinImageAccessorAdapter
     417             : {
     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;
     424             : 
     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
     430             : 
     431             :     WrappedAccessor1 ma1stAccessor;
     432             :     WrappedAccessor2 ma2ndAccessor;
     433             : 
     434             : public:
     435             :     JoinImageAccessorAdapter() :
     436             :         ma1stAccessor(),
     437             :         ma2ndAccessor()
     438             :     {}
     439             : 
     440             :     template< class T > explicit JoinImageAccessorAdapter( T const& accessor ) :
     441             :         ma1stAccessor( accessor ),
     442             :         ma2ndAccessor()
     443             :     {}
     444             : 
     445             :     template< class A1, class A2 > explicit
     446             :     JoinImageAccessorAdapter(
     447             :         JoinImageAccessorAdapter< A1,
     448             :                                   A2 > const& rSrc ) :
     449             :         ma1stAccessor( rSrc.ma1stAccessor ),
     450             :         ma2ndAccessor( rSrc.ma2ndAccessor )
     451             :     {}
     452             : 
     453             :     template< class T1, class T2 >
     454       19323 :     JoinImageAccessorAdapter( T1 accessor1,
     455             :                               T2 accessor2 ) :
     456             :         ma1stAccessor( accessor1 ),
     457       19319 :         ma2ndAccessor( accessor2 )
     458       19323 :     {}
     459             : 
     460             : 
     461             : 
     462             :     WrappedAccessor1 const& get1stWrappedAccessor() const { return ma1stAccessor; }
     463             :     WrappedAccessor1&       get1stWrappedAccessor() { return ma1stAccessor; }
     464             : 
     465             :     WrappedAccessor2 const& get2ndWrappedAccessor() const { return ma2ndAccessor; }
     466             :     WrappedAccessor2&       get2ndWrappedAccessor() { return ma2ndAccessor; }
     467             : 
     468             : 
     469             : 
     470             :     template< class Iterator >
     471    61577985 :     value_type operator()(Iterator const& i) const
     472             :     {
     473             :         return std::make_pair(ma1stAccessor(i.first()),
     474    61577985 :                               ma2ndAccessor(i.second()));
     475             :     }
     476             : 
     477             :     template< class Iterator, class Difference >
     478             :     value_type operator()(Iterator const& i, Difference const& diff) const
     479             :     {
     480             :         return std::make_pair(ma1stAccessor(i.first(),diff),
     481             :                               ma2ndAccessor(i.second(),diff));
     482             :     }
     483             : 
     484             : 
     485             : 
     486             :     template< typename V, class Iterator >
     487             :     void set(V const& value, Iterator const& i) const
     488             :     {
     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() );
     497             :     }
     498             : 
     499             :     template< typename V, class Iterator, class Difference >
     500             :     void set(V const& value, Iterator const& i, Difference const& diff) const
     501             :     {
     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 );
     512             :     }
     513             : 
     514             : };
     515             : 
     516             : } // namespace basebmp
     517             : 
     518             : #endif /* INCLUDED_BASEBMP_ACCESSORADAPTERS_HXX */
     519             : 
     520             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10