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