summaryrefslogtreecommitdiff
path: root/cosv/inc/cosv/tpl/funcall.hxx
blob: 8ec988b8f9ae404f042d8d862e10b94d0dcb32cb (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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: funcall.hxx,v $
 * $Revision: 1.3 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#ifndef CSV_TPL_FUNCALL_HXX
#define CSV_TPL_FUNCALL_HXX

// BASE CLASSES
#include <algorithm>




namespace csv
{
namespace func
{


/** @concept "csv:: Function Objects"

    A set of function objects that can be generated from any kind of
    function or member function with none or one parameter by the
    helper function ->make_func().

    Naming Scheme
    =============

    The naming scheme consists of three variables
      f - the kind of function
      p - the parameter of the function
      c - call operator() of the function object with these arguments

    Each of those may have the following values:
      f:
        f - free, no owning class
        c - const member function of a class
        m - modifying member function of a class
      p:
        n - no parameter
        c - const parameter by reference
        m - modifyable parameter by reference,
        v - parameter by value
      c:
        n - none
        o - the owning object on which the function shall be called
        a - the argument of the function
        b - both, the object on which the function shall be called
            and the argument of the function

    Which gives the following 35 possible combinations:
    ff_pn_cn
    ff_pc_cn
    ff_pc_ca
    ff_pm_cn
    ff_pm_ca
    ff_pv_cn
    ff_pv_ca

    fc_pn_cn
    fc_pn_co
    fc_pc_cn
    fc_pc_co
    fc_pc_ca
    fc_pc_cb
    fc_pm_cn
    fc_pm_co
    fc_pm_ca
    fc_pm_cb
    fc_pv_cn
    fc_pv_co
    fc_pv_ca
    fc_pv_cb

    fm_pn_cn
    fm_pn_co
    fm_pc_cn
    fm_pc_co
    fm_pc_ca
    fm_pc_cb
    fm_pm_cn
    fm_pm_co
    fm_pm_ca
    fm_pm_cb
    fm_pv_cn
    fm_pv_co
    fm_pv_ca
    fm_pv_cb

    These function objects are complicate to handle, so they can be created
    with the overloaded function
        <function_object> csv::make_func(<function_type>, <argument_types>);

    For the rare, but possible case that the owning class and the function
    argument have the same type, these clarifying variations to make_func()
    can be used:
        make_func_callwith_obj(), make_func_callwith_arg().
*/


/** Function object.

    @concept ->"csv::func Function Objects"
    @see    csv::make_func()
*/
template <class R>
struct ff_pn_cn
{
    typedef R           result_type;
    typedef R (*        function_type )();

    R                   operator()() const
                            { return (*f)(); }

                        ff_pn_cn(
                            function_type       i_f)
                            :   f(i_f)  {}
  private:
    function_type       f;
};


/** Function object.

    @concept ->"csv::func Function Objects"
    @see    csv::make_func()
*/
template <class R, class C>
struct fc_pn_co
{
    typedef R           result_type;
    typedef R (C::*     function_type )() const;

    R                   operator()(
                            const C &           i_c ) const
                            { return (i_c.*f)(); }

                        fc_pn_co(
                            function_type       i_f)
                            :   f(i_f)  {}
  private:
    function_type       f;
};



/** Function object.

    @concept ->"csv::func Function Objects"
    @see    csv::make_func()
*/
template <class R, class C, class P>
struct fc_pm_co
{
    typedef R           result_type;
    typedef R (C::*     function_type )(P&) const;

    R                   operator()(
                            const C &           i_c ) const
                            { return (i_c.*f)(p); }

                        fc_pm_co(
                            function_type       i_f,
                            P &                 i_p)
                            :   f(i_f), p(i_p) {}
  private:
    function_type       f;
    P &                 p;
};







}   // namespace func


/** Creates a function object of type ff_pn_cn.
    @concept ->"csv::func Function Objects"
*/
template <class R>
inline func::ff_pn_cn<R>
make_func( R(*i_f)() )
{
    return func::ff_pn_cn<R>(i_f);
}

///** Creates a function object of type ff_py_cn.
//    @concept ->"csv::func Function Objects"
//*/
//template <class R, class P>
//inline func::ff_py_cn<R,P>
//make_func( R(*i_f)(P), P i_p )
//{
//    return func::ff_py_cn<R,A>(i_f, i_p);
//}
//
///** Creates a function object of type ff_py_ca.
//    @concept ->"csv::func Function Objects"
//*/
//template <class R, class P>
//inline func::ff_py_ca<R,P>
//make_func( R(*i_f)(P) )
//{
//    return func::ff_py_ca<R,P>(i_f);
//}


/** Creates a function object of type fc_pn_co.
    @concept ->"csv::func Function Objects"
*/
template <class R, class C>
inline func::fc_pn_co<R,C>
make_func( R(C::*i_f)() const )
{
    return func::fc_pn_co<R,C>(i_f);
}



/** Creates a function object of type fc_pm_co.
    @concept ->"csv::func Function Objects"
*/
template <class R, class C, class P>
inline func::fc_pm_co<R,C,P>
make_func( R(C::*i_f)(P &) const, P & i_p)
{
    return func::fc_pm_co<R,C,P>(i_f, i_p);
}



/* Because std::for_each is defined as a non-modifying algorithm
   it is redefined here. It is also provided for containers.
*/

template <class I, class F>
F
for_each(I i_itBegin, I i_itEnd, F io_functionToBeCalled)
{
    for (I it = i_itBegin; it != i_itEnd; ++it)
    {
        io_functionToBeCalled(*it);
    }
    return io_functionToBeCalled;
}

template <class C, class F>
F
for_each_in(const C & i_container, F io_functionToBeCalled)
{
    typename C::const_iterator const
        itEnd = i_container.end();
    for ( typename C::const_iterator it = i_container.begin();
          it != itEnd;
          ++it )
    {
        io_functionToBeCalled(*it);
    }
    return io_functionToBeCalled;
}

template <class C, class F>
F
for_each_in(C & i_container, F io_functionToBeCalled)
{
    typename C::iterator const
        itEnd = i_container.end();
    for ( typename C::iterator it = i_container.begin();
          it != itEnd;
          ++it )
    {
        io_functionToBeCalled(*it);
    }
    return io_functionToBeCalled;
}




}   // namespace csv
#endif