c++-gtk-utils
mem_fun.h
Go to the documentation of this file.
1 /* Copyright (C) 2009 Chris Vine
2 
3 The library comprised in this file or of which this file is part is
4 distributed by Chris Vine under the GNU Lesser General Public
5 License as follows:
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public License
9  as published by the Free Software Foundation; either version 2.1 of
10  the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License, version 2.1, for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License, version 2.1, along with this library (see the file LGPL.TXT
19  which came with this source code package in the c++-gtk-utils
20  sub-directory); if not, write to the Free Software Foundation, Inc.,
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 However, it is not intended that the object code of a program whose
24 source code instantiates a template from this file or uses macros or
25 inline functions (of any length) should by reason only of that
26 instantiation or use be subject to the restrictions of use in the GNU
27 Lesser General Public License. With that in mind, the words "and
28 macros, inline functions and instantiations of templates (of any
29 length)" shall be treated as substituted for the words "and small
30 macros and small inline functions (ten lines or less in length)" in
31 the fourth paragraph of section 5 of that licence. This does not
32 affect any other reason why object code may be subject to the
33 restrictions in that licence (nor for the avoidance of doubt does it
34 affect the application of section 2 of that licence to modifications
35 of the source code in this file).
36 
37 */
38 
39 #ifndef CGU_MEM_FUN_H
40 #define CGU_MEM_FUN_H
41 
42 /**
43  * @file mem_fun.h
44  * @brief This file contains an adaptor to create a functor from a
45  * class member function to pass to C++ standard algorithms.
46  *
47  * \#include <c++-gtk-utils/mem_fun.h>
48  *
49  * Cgu::MemFun::make() is an adaptor which allows a non-static class
50  * member function to be called by standard algorithms such as
51  * std::for_each() or std::transform(), whereby the member function is
52  * passed a contained object as an argument on iterating through the
53  * container. It can also be used to make predicates from member
54  * functions to pass to other algorithms, or indeed to generate a
55  * general functor object representing a member function with object
56  * and one or two arguments. It does the equivalent of std::ptr_fun()
57  * for ordinary functions.
58  *
59  * For example, to iterate over a std::vector<int> container object
60  * named 'vec', calling a class method 'void MyObj::my_method(int)' on
61  * each iteration, Cgu::MemFun::make() could be invoked as:
62  *
63  * @code
64  * using namespace Cgu;
65  * std::for_each(vec.begin(), vec.end(),
66  * MemFun::make(my_obj, &MyObj::my_method));
67  * @endcode
68  *
69  * As in the case of std::ptr_fun(), when called via std::for_each(),
70  * the member function called must be a unary function, although an
71  * additional argument can be bound with std::bind2nd() or
72  * std::bind1st() to enable it to be used with binary class functions.
73  * (Note that in many implementations of std::bind*, neither the free
74  * nor the bound argument can be a reference, whether const or
75  * non-const. A reference argument can be employed with
76  * Cgu::MemFun::make() where std::bind* is not used.)
77  *
78  * If (in C++98) the member function is const, then the unary
79  * Cgu::MemFun::make() can be mimicked by a combination of
80  * std::mem_fun() and std::bind1st(), so that (if const) the above
81  * example could be reproduced as:
82  *
83  * @code
84  * std::for_each(vec.begin(), vec.end(),
85  * std::bind1st(std::mem_fun(&MyObj::my_method), &my_obj));
86  * @endcode
87  *
88  * The binary version of Cgu::MemFun::make() cannot be reproduced by
89  * any of the built-in function adaptors available in C++98/C++03.
90  */
91 
92 /**
93  * @namespace Cgu::MemFun
94  * @brief This namespace contains an adaptor to create a functor from a
95  * class member function to pass to C++ standard algorithms.
96  *
97  * \#include <c++-gtk-utils/mem_fun.h>
98  *
99  * Cgu::MemFun::make() is an adaptor which allows a non-static class
100  * member function to be called by standard algorithms such as
101  * std::for_each() or std::transform(), whereby the member function is
102  * passed a contained object as an argument on iterating through the
103  * container. It can also be used to make predicates from member
104  * functions to pass to other algorithms, or indeed to generate a
105  * general functor object representing a member function with object
106  * and one or two arguments. It does the equivalent of std::ptr_fun()
107  * for ordinary functions.
108  *
109  * For example, to iterate over a std::vector<int> container object
110  * named 'vec', calling a class method 'void MyObj::my_method(int)' on
111  * each iteration, Cgu::MemFun::make() could be invoked as:
112  *
113  * @code
114  * using namespace Cgu;
115  * std::for_each(vec.begin(), vec.end(),
116  * MemFun::make(my_obj, &MyObj::my_method));
117  * @endcode
118  *
119  * As in the case of std::ptr_fun(), when called via std::for_each(),
120  * the member function called must be a unary function, although an
121  * additional argument can be bound with std::bind2nd() or
122  * std::bind1st() to enable it to be used with binary class functions.
123  * (Note that in many implementations of std::bind*, neither the free
124  * nor the bound argument can be a reference, whether const or
125  * non-const. A reference argument can be employed with
126  * Cgu::MemFun::make() where std::bind* is not used.)
127  *
128  * If (in C++98) the member function is const, then the unary
129  * Cgu::MemFun::make() can be mimicked by a combination of
130  * std::mem_fun() and std::bind1st(), so that (if const) the above
131  * example could be reproduced as:
132  *
133  * @code
134  * std::for_each(vec.begin(), vec.end(),
135  * std::bind1st(std::mem_fun(&MyObj::my_method), &my_obj));
136  * @endcode
137  *
138  * The binary version of Cgu::MemFun::make() cannot be reproduced by
139  * any of the built-in function adaptors available in C++98/C++03.
140  */
141 
142 #include <functional>
144 
145 namespace Cgu {
146 
147 namespace MemFun {
148 
149 template <class Ret, class Arg, class T>
150 class Functor1: public std::unary_function<Arg, Ret> {
151  T* obj;
152  Ret (T::*func)(Arg);
153 public:
154  Ret operator()(Arg arg) const {return (obj->*func)(arg);}
155  Functor1(T& obj_, Ret (T::*func_)(Arg)): obj(&obj_), func(func_) {}
156 };
157 
158 template <class Ret, class Arg, class T>
160  Ret (T::*func)(Arg)) {
161  return Functor1<Ret, Arg, T>(t, func);
162 }
163 
164 /* const version, for binding to const methods */
165 
166 template <class Ret, class Arg, class T>
167 class Functor1_const: public std::unary_function<Arg, Ret> {
168  const T* obj;
169  Ret (T::*func)(Arg) const;
170 public:
171  Ret operator()(Arg arg) const {return (obj->*func)(arg);}
172  Functor1_const(const T& obj_, Ret (T::*func_)(Arg) const): obj(&obj_), func(func_) {}
173 };
174 
175 template <class Ret, class Arg, class T>
177  Ret (T::*func)(Arg) const) {
178  return Functor1_const<Ret, Arg, T>(t, func);
179 }
180 
181 /* two argument version for use with std::bind* */
182 
183 template <class Ret, class Arg1, class Arg2, class T>
184 class Functor2: public std::binary_function<Arg1, Arg2, Ret> {
185  T* obj;
186  Ret (T::*func)(Arg1, Arg2);
187 public:
188  Ret operator()(Arg1 arg1, Arg2 arg2) const {return (obj->*func)(arg1, arg2);}
189  Functor2(T& obj_, Ret (T::*func_)(Arg1, Arg2)): obj(&obj_), func(func_) {}
190 };
191 
192 template <class Ret, class Arg1, class Arg2, class T>
194  Ret (T::*func)(Arg1, Arg2)) {
195  return Functor2<Ret, Arg1, Arg2, T>(t, func);
196 }
197 
198 /* const version, for binding to const methods */
199 
200 template <class Ret, class Arg1, class Arg2, class T>
201 class Functor2_const: public std::binary_function<Arg1, Arg2, Ret> {
202  const T* obj;
203  Ret (T::*func)(Arg1, Arg2) const;
204 public:
205  Ret operator()(Arg1 arg1, Arg2 arg2) const {return (obj->*func)(arg1, arg2);}
206  Functor2_const(const T& obj_, Ret (T::*func_)(Arg1, Arg2) const): obj(&obj_), func(func_) {}
207 };
208 
209 template <class Ret, class Arg1, class Arg2, class T>
211  Ret (T::*func)(Arg1, Arg2) const) {
212  return Functor2_const<Ret, Arg1, Arg2, T>(t, func);
213 }
214 
215 } // namespace MemFun
216 
217 } // namespace Cgu
218 
219 #endif
Cgu::MemFun::make
Functor1< Ret, Arg, T > make(T &t, Ret(T::*func)(Arg))
Definition: mem_fun.h:159
Cgu
Definition: application.h:45
Cgu::MemFun::Functor1::operator()
Ret operator()(Arg arg) const
Definition: mem_fun.h:154
Cgu::MemFun::Functor2_const::Functor2_const
Functor2_const(const T &obj_, Ret(T::*func_)(Arg1, Arg2) const)
Definition: mem_fun.h:206
Cgu::MemFun::Functor2_const::operator()
Ret operator()(Arg1 arg1, Arg2 arg2) const
Definition: mem_fun.h:205
Cgu::MemFun::Functor2_const
Definition: mem_fun.h:201
Cgu::MemFun::Functor2::operator()
Ret operator()(Arg1 arg1, Arg2 arg2) const
Definition: mem_fun.h:188
Cgu::MemFun::Functor1_const::Functor1_const
Functor1_const(const T &obj_, Ret(T::*func_)(Arg) const)
Definition: mem_fun.h:172
Cgu::MemFun::Functor2
Definition: mem_fun.h:184
Cgu::MemFun::Functor1_const
Definition: mem_fun.h:167
Cgu::MemFun::Functor1
Definition: mem_fun.h:150
Cgu::MemFun::Functor1_const::operator()
Ret operator()(Arg arg) const
Definition: mem_fun.h:171
Cgu::MemFun::Functor1::Functor1
Functor1(T &obj_, Ret(T::*func_)(Arg))
Definition: mem_fun.h:155
Cgu::MemFun::Functor2::Functor2
Functor2(T &obj_, Ret(T::*func_)(Arg1, Arg2))
Definition: mem_fun.h:189
cgu_config.h