xref: /aoo42x/main/autodoc/inc/cosv/tpl/funcall.hxx (revision 8a106958)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef CSV_TPL_FUNCALL_HXX
25 #define CSV_TPL_FUNCALL_HXX
26 
27 // BASE CLASSES
28 #include <algorithm>
29 
30 
31 
32 
33 namespace csv
34 {
35 namespace func
36 {
37 
38 
39 /** @concept "csv:: Function Objects"
40 
41     A set of function objects that can be generated from any kind of
42     function or member function with none or one parameter by the
43     helper function ->make_func().
44 
45     Naming Scheme
46     =============
47 
48     The naming scheme consists of three variables
49       f - the kind of function
50       p - the parameter of the function
51       c - call operator() of the function object with these arguments
52 
53     Each of those may have the following values:
54       f:
55         f - free, no owning class
56         c - const member function of a class
57         m - modifying member function of a class
58       p:
59         n - no parameter
60         c - const parameter by reference
61         m - modifyable parameter by reference,
62         v - parameter by value
63       c:
64         n - none
65         o - the owning object on which the function shall be called
66         a - the argument of the function
67         b - both, the object on which the function shall be called
68             and the argument of the function
69 
70     Which gives the following 35 possible combinations:
71     ff_pn_cn
72     ff_pc_cn
73     ff_pc_ca
74     ff_pm_cn
75     ff_pm_ca
76     ff_pv_cn
77     ff_pv_ca
78 
79     fc_pn_cn
80     fc_pn_co
81     fc_pc_cn
82     fc_pc_co
83     fc_pc_ca
84     fc_pc_cb
85     fc_pm_cn
86     fc_pm_co
87     fc_pm_ca
88     fc_pm_cb
89     fc_pv_cn
90     fc_pv_co
91     fc_pv_ca
92     fc_pv_cb
93 
94     fm_pn_cn
95     fm_pn_co
96     fm_pc_cn
97     fm_pc_co
98     fm_pc_ca
99     fm_pc_cb
100     fm_pm_cn
101     fm_pm_co
102     fm_pm_ca
103     fm_pm_cb
104     fm_pv_cn
105     fm_pv_co
106     fm_pv_ca
107     fm_pv_cb
108 
109     These function objects are complicate to handle, so they can be created
110     with the overloaded function
111         <function_object> csv::make_func(<function_type>, <argument_types>);
112 
113     For the rare, but possible case that the owning class and the function
114     argument have the same type, these clarifying variations to make_func()
115     can be used:
116         make_func_callwith_obj(), make_func_callwith_arg().
117 */
118 
119 
120 /** Function object.
121 
122     @concept ->"csv::func Function Objects"
123     @see    csv::make_func()
124 */
125 template <class R>
126 struct ff_pn_cn
127 {
128     typedef R           result_type;
129     typedef R (*        function_type )();
130 
operator ()csv::func::ff_pn_cn131     R                   operator()() const
132                             { return (*f)(); }
133 
ff_pn_cncsv::func::ff_pn_cn134                         ff_pn_cn(
135                             function_type       i_f)
136                             :   f(i_f)  {}
137   private:
138     function_type       f;
139 };
140 
141 
142 /** Function object.
143 
144     @concept ->"csv::func Function Objects"
145     @see    csv::make_func()
146 */
147 template <class R, class C>
148 struct fc_pn_co
149 {
150     typedef R           result_type;
151     typedef R (C::*     function_type )() const;
152 
operator ()csv::func::fc_pn_co153     R                   operator()(
154                             const C &           i_c ) const
155                             { return (i_c.*f)(); }
156 
fc_pn_cocsv::func::fc_pn_co157                         fc_pn_co(
158                             function_type       i_f)
159                             :   f(i_f)  {}
160   private:
161     function_type       f;
162 };
163 
164 
165 
166 /** Function object.
167 
168     @concept ->"csv::func Function Objects"
169     @see    csv::make_func()
170 */
171 template <class R, class C, class P>
172 struct fc_pm_co
173 {
174     typedef R           result_type;
175     typedef R (C::*     function_type )(P&) const;
176 
operator ()csv::func::fc_pm_co177     R                   operator()(
178                             const C &           i_c ) const
179                             { return (i_c.*f)(p); }
180 
fc_pm_cocsv::func::fc_pm_co181                         fc_pm_co(
182                             function_type       i_f,
183                             P &                 i_p)
184                             :   f(i_f), p(i_p) {}
185   private:
186     function_type       f;
187     P &                 p;
188 };
189 
190 
191 
192 
193 
194 
195 
196 }   // namespace func
197 
198 
199 /** Creates a function object of type ff_pn_cn.
200     @concept ->"csv::func Function Objects"
201 */
202 template <class R>
203 inline func::ff_pn_cn<R>
make_func(R (* i_f)())204 make_func( R(*i_f)() )
205 {
206     return func::ff_pn_cn<R>(i_f);
207 }
208 
209 ///** Creates a function object of type ff_py_cn.
210 //    @concept ->"csv::func Function Objects"
211 //*/
212 //template <class R, class P>
213 //inline func::ff_py_cn<R,P>
214 //make_func( R(*i_f)(P), P i_p )
215 //{
216 //    return func::ff_py_cn<R,A>(i_f, i_p);
217 //}
218 //
219 ///** Creates a function object of type ff_py_ca.
220 //    @concept ->"csv::func Function Objects"
221 //*/
222 //template <class R, class P>
223 //inline func::ff_py_ca<R,P>
224 //make_func( R(*i_f)(P) )
225 //{
226 //    return func::ff_py_ca<R,P>(i_f);
227 //}
228 
229 
230 /** Creates a function object of type fc_pn_co.
231     @concept ->"csv::func Function Objects"
232 */
233 template <class R, class C>
234 inline func::fc_pn_co<R,C>
make_func(R (C::* i_f)()const)235 make_func( R(C::*i_f)() const )
236 {
237     return func::fc_pn_co<R,C>(i_f);
238 }
239 
240 
241 
242 /** Creates a function object of type fc_pm_co.
243     @concept ->"csv::func Function Objects"
244 */
245 template <class R, class C, class P>
246 inline func::fc_pm_co<R,C,P>
make_func(R (C::* i_f)(P &)const,P & i_p)247 make_func( R(C::*i_f)(P &) const, P & i_p)
248 {
249     return func::fc_pm_co<R,C,P>(i_f, i_p);
250 }
251 
252 
253 
254 /* Because std::for_each is defined as a non-modifying algorithm
255    it is redefined here. It is also provided for containers.
256 */
257 
258 template <class I, class F>
259 F
for_each(I i_itBegin,I i_itEnd,F io_functionToBeCalled)260 for_each(I i_itBegin, I i_itEnd, F io_functionToBeCalled)
261 {
262     for (I it = i_itBegin; it != i_itEnd; ++it)
263     {
264         io_functionToBeCalled(*it);
265     }
266     return io_functionToBeCalled;
267 }
268 
269 template <class C, class F>
270 F
for_each_in(const C & i_container,F io_functionToBeCalled)271 for_each_in(const C & i_container, F io_functionToBeCalled)
272 {
273     typename C::const_iterator const
274         itEnd = i_container.end();
275     for ( typename C::const_iterator it = i_container.begin();
276           it != itEnd;
277           ++it )
278     {
279         io_functionToBeCalled(*it);
280     }
281     return io_functionToBeCalled;
282 }
283 
284 template <class C, class F>
285 F
for_each_in(C & i_container,F io_functionToBeCalled)286 for_each_in(C & i_container, F io_functionToBeCalled)
287 {
288     typename C::iterator const
289         itEnd = i_container.end();
290     for ( typename C::iterator it = i_container.begin();
291           it != itEnd;
292           ++it )
293     {
294         io_functionToBeCalled(*it);
295     }
296     return io_functionToBeCalled;
297 }
298 
299 
300 
301 
302 }   // namespace csv
303 #endif
304