summaryrefslogtreecommitdiff
path: root/basebmp/inc/accessoradapters.hxx
blob: 109fe92e12f9e0aa0470982a4a3212378fdda1f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_BASEBMP_INC_ACCESSORADAPTERS_HXX
#define INCLUDED_BASEBMP_INC_ACCESSORADAPTERS_HXX

#include <vigra/numerictraits.hxx>

namespace basebmp
{

/** Interpose given accessor's set and get methods with two unary
    functors.

    @tpl WrappedAccessor
    Wrapped type must provide the usual get and set accessor methods,
    with the usual signatures (see StandardAccessor for a conforming
    example).

    @tpl GetterFunctor
    An Adaptable Unary Function (i.e. providing result_type and
    argument_type typedefs)

    @tpl SetterFunctor
    An Adaptable Unary Function (i.e. providing result_type and
    argument_type typedefs)
 */
template< class WrappedAccessor,
          typename GetterFunctor,
          typename SetterFunctor > class UnaryFunctionAccessorAdapter
{
public:
    typedef typename GetterFunctor::result_type   value_type;
    typedef typename SetterFunctor::argument_type argument_type;

#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
// making all members public, if no member template friends
private:
    template<class A, typename G, typename S> friend class UnaryFunctionAccessorAdapter;
#endif

    // we don't derive from wrapped type, to avoid ambiguities
    // regarding templatized getter/setter methods.
    WrappedAccessor maAccessor;
    GetterFunctor   maGetterFunctor;
    SetterFunctor   maSetterFunctor;

public:
    UnaryFunctionAccessorAdapter() :
        maAccessor(),
        maGetterFunctor(),
        maSetterFunctor()
    {}

    template< class A > explicit
    UnaryFunctionAccessorAdapter( UnaryFunctionAccessorAdapter< A,
                                                                GetterFunctor,
                                                                SetterFunctor > const& rSrc ) :
        maAccessor( rSrc.maAccessor ),
        maGetterFunctor( rSrc.maGetterFunctor ),
        maSetterFunctor( rSrc.maSetterFunctor )
    {}

    template< class T > explicit UnaryFunctionAccessorAdapter( T const& accessor ) :
        maAccessor( accessor ),
        maGetterFunctor(),
        maSetterFunctor()
    {}

    template< class T > UnaryFunctionAccessorAdapter( T             accessor,
                                                      GetterFunctor getterFunctor,
                                                      SetterFunctor setterFunctor) :
        maAccessor( accessor ),
        maGetterFunctor( getterFunctor ),
        maSetterFunctor( setterFunctor )
    {}


    typename SetterFunctor::result_type setter(argument_type v) const
    {
        return maSetterFunctor(v);
    }

    template< class Iterator >
    value_type operator()(Iterator const& i) const
    {
        return maGetterFunctor( maAccessor(i) );
    }

    template< class Iterator, class Difference >
    value_type operator()(Iterator const& i, Difference const& diff) const
    {
        return maGetterFunctor( maAccessor(i,diff) );
    }



    template< typename V, class Iterator >
    void set(V const& value, Iterator const& i) const
    {
        maAccessor.set(
            maSetterFunctor(
                vigra::detail::RequiresExplicitCast<argument_type>::cast(value) ),
            i );
    }

};



/** Interpose given accessor's set methods with a binary function,
    taking both old and new value.

    The wrappee's getter methods kept as-is.

    @tpl WrappedAccessor
    Wrapped type must provide the usual get and set accessor methods,
    with the usual signatures (see StandardAccessor for a conforming
    example). Furthermore, must provide a nested typedef value_type.

    @tpl SetterFunctor
    An adaptable binary function (i.e. providing nested typedefs for
    result_type and first and second argument type)
 */
template< class WrappedAccessor,
          typename SetterFunctor > class BinarySetterFunctionAccessorAdapter
{
public:
    typedef typename WrappedAccessor::value_type         value_type;
    typedef typename SetterFunctor::second_argument_type argument_type;

#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
// making all members public, if no member template friends
private:
    template<class A, typename S> friend class BinarySetterFunctionAccessorAdapter;
#endif

    WrappedAccessor    maAccessor;
    SetterFunctor      maFunctor;

public:
    BinarySetterFunctionAccessorAdapter() :
        maAccessor(),
        maFunctor()
    {}

    template< class A > explicit
    BinarySetterFunctionAccessorAdapter(
        BinarySetterFunctionAccessorAdapter< A,
                                             SetterFunctor > const& rSrc ) :
        maAccessor( rSrc.maAccessor ),
        maFunctor( rSrc.maFunctor )
    {}

    template< class T > explicit BinarySetterFunctionAccessorAdapter( T const& accessor ) :
        maAccessor( accessor ),
        maFunctor()
    {}

    template< class T > BinarySetterFunctionAccessorAdapter( T             accessor,
                                                             SetterFunctor functor ) :
        maAccessor( accessor ),
        maFunctor( functor )
    {}




    template< class Iterator >
    value_type operator()(Iterator const& i) const
    {
        return maAccessor(i);
    }

    template< class Iterator, class Difference >
    value_type operator()(Iterator const& i, Difference const& diff) const
    {
        return maAccessor(i,diff);
    }



    template< typename V, class Iterator >
    void set(V const& value, Iterator const& i) const
    {
        maAccessor.set(
            maFunctor(maAccessor(i),
                      vigra::detail::RequiresExplicitCast<argument_type>::cast(value)),
            i );
    }

};



/** Write through a CompositeIterator's first wrapped iterator, by
    piping the first wrapped iterator value, the second iterator
    value, and the specified new value through a ternary function.

    Passed iterator must fulfill the CompositeIterator concept. Note
    that the getter/setter methods are not templatized regarding the
    iterator type, to make the mask calculation optimization below
    safe (see the maskedAccessor template metafunction below)

    @tpl WrappedAccessor1
    Wrapped type must provide the usual get and set accessor methods,
    with the usual signatures (see StandardAccessor for a conforming
    example). Furthermore, the type must provide a nested typedef
    value_type (the selection of WrappedAccessor1 as the provider for
    that typedef is rather arbitrary. Could have been
    WrappedAccessor2, too. So sue me)

    @tpl Functor
    An adaptable ternary function (i.e. providing nested typedefs for
    result_type and first, second and third argument type)
 */
template< class WrappedAccessor1,
          class WrappedAccessor2,
          typename Functor > class TernarySetterFunctionAccessorAdapter
{
public:
    typedef typename WrappedAccessor1::value_type value_type;
    typedef typename Functor::third_argument_type argument_type;

#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
// making all members public, if no member template friends
private:
    template<class A1, class A2, typename F> friend class TernarySetterFunctionAccessorAdapter;
#endif

    WrappedAccessor1 ma1stAccessor;
    WrappedAccessor2 ma2ndAccessor;
    Functor          maFunctor;

public:
    TernarySetterFunctionAccessorAdapter() :
        ma1stAccessor(),
        ma2ndAccessor(),
        maFunctor()
    {}

    template< class T > explicit TernarySetterFunctionAccessorAdapter( T const& accessor ) :
        ma1stAccessor( accessor ),
        ma2ndAccessor(),
        maFunctor()
    {}

    template< class A1, class A2 > explicit
    TernarySetterFunctionAccessorAdapter(
        TernarySetterFunctionAccessorAdapter< A1,
                                              A2,
                                              Functor > const& rSrc ) :
        ma1stAccessor( rSrc.ma1stAccessor ),
        ma2ndAccessor( rSrc.ma2ndAccessor ),
        maFunctor( rSrc.maFunctor )
    {}

    template< class T1, class T2 >
    TernarySetterFunctionAccessorAdapter( T1 accessor1,
                                          T2 accessor2 ) :
        ma1stAccessor( accessor1 ),
        ma2ndAccessor( accessor2 ),
        maFunctor()
    {}

    template< class T1, class T2 >
    TernarySetterFunctionAccessorAdapter( T1      accessor1,
                                          T2      accessor2,
                                          Functor func ) :
        ma1stAccessor( accessor1 ),
        ma2ndAccessor( accessor2 ),
        maFunctor( func )
    {}



    WrappedAccessor1 const& get1stWrappedAccessor() const { return ma1stAccessor; }
    WrappedAccessor1&       get1stWrappedAccessor() { return ma1stAccessor; }



    template< class Iterator >
    value_type operator()(Iterator const& i) const
    {
        return ma1stAccessor(i.first());
    }

    template< class Iterator, class Difference >
    value_type operator()(Iterator const& i, Difference const& diff) const
    {
        return ma1stAccessor(i.second(),diff);
    }



    template< typename V, class Iterator >
    void set(V const& value, Iterator const& i) const
    {
        ma1stAccessor.set(
            maFunctor(ma1stAccessor(i.first()),
                      ma2ndAccessor(i.second()),
                      vigra::detail::RequiresExplicitCast<argument_type>::cast(value)),
            i.first() );
    }

};



/** Access two distinct images simultaneously

    Passed iterator must fulfill the CompositeIterator concept
    (i.e. wrap the two image's iterators into one
    CompositeIterator). The getter and setter methods expect and
    return a pair of values, with types equal to the two accessors
    value types

    @tpl WrappedAccessor1
    Wrapped type must provide the usual get and set accessor methods,
    with the usual signatures (see StandardAccessor for a conforming
    example). Furthermore, the type must provide a nested typedef
    value_type.

    @tpl WrappedAccessor2
    Wrapped type must provide the usual get and set accessor methods,
    with the usual signatures (see StandardAccessor for a conforming
    example). Furthermore, the type must provide a nested typedef
    value_type.
 */
template< class WrappedAccessor1,
          class WrappedAccessor2 > class JoinImageAccessorAdapter
{
public:
    // TODO(F3): Need numeric traits and a few free functions to
    // actually calculate with a pair (semantic: apply every operation
    // individually to the contained types)
    typedef std::pair<typename WrappedAccessor1::value_type,
                      typename WrappedAccessor2::value_type>    value_type;

#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
// making all members public, if no member template friends
private:
    template<class A1, class A2> friend class JoinImageAccessorAdapter;
#endif

    WrappedAccessor1 ma1stAccessor;
    WrappedAccessor2 ma2ndAccessor;

public:
    JoinImageAccessorAdapter() :
        ma1stAccessor(),
        ma2ndAccessor()
    {}

    template< class T > explicit JoinImageAccessorAdapter( T const& accessor ) :
        ma1stAccessor( accessor ),
        ma2ndAccessor()
    {}

    template< class A1, class A2 > explicit
    JoinImageAccessorAdapter(
        JoinImageAccessorAdapter< A1,
                                  A2 > const& rSrc ) :
        ma1stAccessor( rSrc.ma1stAccessor ),
        ma2ndAccessor( rSrc.ma2ndAccessor )
    {}

    template< class T1, class T2 >
    JoinImageAccessorAdapter( T1 accessor1,
                              T2 accessor2 ) :
        ma1stAccessor( accessor1 ),
        ma2ndAccessor( accessor2 )
    {}




    template< class Iterator >
    value_type operator()(Iterator const& i) const
    {
        return std::make_pair(ma1stAccessor(i.first()),
                              ma2ndAccessor(i.second()));
    }

    template< class Iterator, class Difference >
    value_type operator()(Iterator const& i, Difference const& diff) const
    {
        return std::make_pair(ma1stAccessor(i.first(),diff),
                              ma2ndAccessor(i.second(),diff));
    }



};

} // namespace basebmp

#endif /* INCLUDED_BASEBMP_INC_ACCESSORADAPTERS_HXX */

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */