MADNESS 0.10.1
mem_func_wrapper.h
Go to the documentation of this file.
1/*
2 This file is a part of MADNESS.
3 Copyright (C) 2014 Virginia Tech
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 For more information please contact:
20
21 Robert J. Harrison
22 Oak Ridge National Laboratory
23 One Bethel Valley Road
24 P.O. Box 2008, MS-6367
25
26 email: harrisonrj@ornl.gov
27 tel: 865-241-3937
28 fax: 865-572-0680
29*/
30
31/**
32 \file mem_func_wrapper.h
33 \brief Defines tools for encapsulating a pointer to a member function.
34 \ingroup libraries
35
36 The member pointer is stored, along with a pointer to the object with which
37 it should be dereferenced.
38*/
39
40#ifndef MADNESS_WORLD_MEM_FUNC_WRAPPER_H__INCLUDED
41#define MADNESS_WORLD_MEM_FUNC_WRAPPER_H__INCLUDED
42
44
45namespace madness {
46 namespace detail {
47
48 /// Default pointer to a. object of type \c T.
49
50 /// Returns a null pointer, by default.
51 /// \tparam T The pointer type.
52 template <typename T>
54
55 /// Get a default pointer.
56
57 /// \return \c nullptr.
58 static T init() {
59 return nullptr;
60 }
61 }; // struct DefaultInitPtr
62
63
64 /// Default shared pointer to an object of type \c T.
65
66 /// Returns a \"NULL\" shared pointer, by default. Specialization for
67 /// shared pointers.
68 /// \tparam T The type pointed to by the shared pointer.
69 template <typename T>
70 struct DefaultInitPtr<std::shared_ptr<T> > {
71
72 /// Get a default shared pointer.
73
74 /// \return Default shared pointer.
75 static std::shared_ptr<T> init() {
76 return std::shared_ptr<T>();
77 }
78 }; // struct DefaultInitPtr<std::shared_ptr<T> >
79
80 /// Functor wrapper for object and member function pointers.
81
82 /// This class encapsulates a pointer to an object and a member
83 /// pointer to a function of that object's type.
84 ///
85 /// \tparam ptrT Pointer type.
86 /// \tparam memfnT Member function pointer type.
87 /// \tparam resT Result type of the member function.
88 template <typename ptrT, typename memfnT, typename resT>
90 private:
91 ptrT ptr_; ///< Pointer to the object.
92 memfnT memfn_; ///< Member function of the object's type.
93
94 friend memfnT get_mem_func_ptr<ptrT, memfnT, resT>(const MemFuncWrapper<ptrT, memfnT, resT>&);
95
96 public:
97 /// Alias for a wrapper that returns \c void.
99
100 /// Alias for the function's result type.
101 typedef resT result_type;
102
103 /// Alias for the type of the member function pointer.
104 typedef memfnT memfn_type;
105
106 /// \brief Constructor that sets the pointer to the default value
107 /// from `DefaultInitPtr<ptrT>`.
109 : ptr_(DefaultInitPtr<ptrT>::init()), memfn_()
110 { }
111
112 /// \brief Copy constructor that copies the object pointer and
113 /// member function pointer.
115 ptr_(other.ptr_), memfn_(other.memfn_)
116 { }
117
118 /// \brief Constructs a wrapper from an object pointer and a member
119 /// function pointer.
120 ///
121 /// \param[in] ptr The object pointer.
122 /// \param[in] memfn The member function pointer.
123 MemFuncWrapper(ptrT ptr, memfnT memfn) :
124 ptr_(ptr), memfn_(memfn)
125 { }
126
127 /// Copy assignment operator.
128 ///
129 /// \param[in] other The wrapper to be copied.
130 /// \return This wrapper, which is now a copy of \c other.
132 ptr_ = other.ptr_;
133 memfn_ = other.memfn_;
134 return *this;
135 }
136
137 /// \brief Evaluates the member function, when dereferenced from the
138 /// object pointer.
139
140 /// \tparam argTs Argument type pack.
141 /// \param[in,out] args Argument parameter pack.
142 /// \return The member function's return value.
143 template <typename... argTs>
144 result_type operator()(argTs&&... args) const {
146 return ((*ptr_).*memfn_)(std::forward<argTs>(args)...);
147 }
148
149 /// Serializes a \c MemFuncWrapper.
150
151 /// \tparam Archive The archive type.
152 /// \param[in,out] ar The archive.
153 template <typename Archive>
154 void serialize(const Archive& ar) {
155 ar & ptr_ & memfn_;
156 }
157
158 friend memfnT get_mem_func_ptr(const MemFuncWrapper_& wrapper) {
159 return wrapper.memfn_;
160 }
161 }; // class MemFuncWrapper
162
163
164 /// Functor wrapper for object and member function pointers.
165
166 /// This is a specialization of \c MemFuncWrapper for the case where
167 /// the member function has \c void return type.
168 /// \tparam ptrT Pointer type.
169 /// \tparam memfnT Member function pointer type.
170 template <typename ptrT, typename memfnT>
171 class MemFuncWrapper<ptrT, memfnT, void> {
172 private:
173 ptrT ptr_; ///< Pointer to the object.
174 memfnT memfn_; ///< Pointer to the desired member function.
175
176 friend memfnT get_mem_func_ptr<ptrT, memfnT, void>(const MemFuncWrapper<ptrT, memfnT, void>&);
177
178 public:
179 /// Alias for a member function with \c void return type.
181
182 /// Alias for the function's return type.
183 typedef void result_type;
184
185 /// Alias for the member function pointer type.
186 typedef memfnT memfn_type;
187
188 /// Construct a wrapper with the default pointer for this type.
190 : ptr_(DefaultInitPtr<ptrT>::init()), memfn_()
191 { }
192
193 /// Copy constructor.
194
195 /// \param[in] other The wrapper to copy.
197 ptr_(other.ptr_), memfn_(other.memfn_)
198 { }
199
200 /// \brief Construct a wrapper from an object pointer and a member
201 /// function pointer.
202
203 /// \param[in] ptr The object pointer.
204 /// \param[in] memfn The member function pointer.
205 MemFuncWrapper(ptrT ptr, memfnT memfn) :
206 ptr_(ptr), memfn_(memfn)
207 { }
208
209 /// Copy assignment operator.
210 ///
211 /// \param[in] other The wrapper to be copied.
212 /// \return This wrapper, which is now a copy of \c other.
214 ptr_ = other.ptr_;
215 memfn_ = other.memfn_;
216 return *this;
217 }
218
219 /// \brief Evaluates the member function, when dereferenced from the
220 /// object pointer.
221
222 /// \tparam argTs Argument type pack.
223 /// \param[in,out] args Argument parameter pack.
224 template <typename... argTs>
225 void operator()(argTs&&... args) const {
227 ((*ptr_).*memfn_)(std::forward<argTs>(args)...);
228 }
229
230 /// Serializes a \c MemFuncWrapper.
231
232 /// \tparam Archive The archive type.
233 /// \param[in,out] ar The archive.
234 template <typename Archive>
235 void serialize(const Archive& ar) {
236 ar & ptr_ & memfn_;
237 }
238
239 }; // class MemFuncWrapper<ptrT, memfnT, void>
240
241 /// \brief Create a member function wrapper (\c MemFuncWrapper) from an
242 /// object and a member function pointer.
243
244 /// \tparam objT The object type.
245 /// \tparam memfnT The member function pointer type.
246 /// \param[in] obj The object.
247 /// \param[in] memfn The member function pointer.
248 /// \return A wrapped member function pointer.
249 template <typename objT, typename memfnT>
250 MemFuncWrapper<objT*, memfnT, typename result_of<memfnT>::type>
251 wrap_mem_fn(objT& obj, memfnT memfn) {
252 return MemFuncWrapper<objT*, memfnT,
253 typename memfunc_traits<memfnT>::result_type>(& obj, memfn);
254 }
255
256 /// \brief Create a member function wrapper (\c MemFuncWrapper) from a
257 /// const object and a member function pointer.
258
259 /// \tparam objT The object type.
260 /// \tparam memfnT The member function pointer type.
261 /// \param[in] obj The object.
262 /// \param[in] memfn The member function pointer.
263 /// \return A wrapped member function pointer.
264 template <typename objT, typename memfnT>
265 MemFuncWrapper<const objT*, memfnT, typename result_of<memfnT>::type>
266 wrap_mem_fn(const objT& obj, memfnT memfn) {
267 return MemFuncWrapper<const objT*, memfnT,
268 typename memfunc_traits<memfnT>::result_type>(& obj, memfn);
269 }
270
271 /// \brief Create a member function wrapper (\c MemFuncWrapper) from a
272 /// pointer and a member function pointer.
273
274 /// \tparam objT The object type.
275 /// \tparam memfnT The member function pointer type.
276 /// \param[in] obj Pointer to the object.
277 /// \param[in] memfn The member function pointer.
278 /// \return A wrapped member function pointer.
279 template <typename objT, typename memfnT>
280 MemFuncWrapper<objT*, memfnT, typename result_of<memfnT>::type>
281 wrap_mem_fn(objT* obj, memfnT memfn) {
282 return MemFuncWrapper<objT*, memfnT,
283 typename memfunc_traits<memfnT>::result_type>(obj, memfn);
284 }
285
286 /// \brief Create a member function wrapper (\c MemFuncWrapper) from a
287 /// const pointer and a member function pointer.
288
289 /// \tparam objT The object type.
290 /// \tparam memfnT The member function pointer type.
291 /// \param[in] obj Pointer to the object.
292 /// \param[in] memfn The member function pointer.
293 /// \return A wrapped member function pointer.
294 template <typename objT, typename memfnT>
295 MemFuncWrapper<const objT*, memfnT, typename result_of<memfnT>::type>
296 wrap_mem_fn(const objT* obj, memfnT memfn) {
297 return MemFuncWrapper<const objT*, memfnT,
298 typename memfunc_traits<memfnT>::result_type>(obj, memfn);
299 }
300
301 /// \brief Create a member function wrapper (\c MemFuncWrapper) from a
302 /// shared pointer and a member function pointer.
303
304 /// \tparam objT The object type.
305 /// \tparam memfnT The member function pointer type.
306 /// \param[in] obj Shared pointer to the object.
307 /// \param[in] memfn The member function pointer.
308 /// \return A wrapped member function pointer.
309 template <typename objT, typename memfnT>
310 MemFuncWrapper<std::shared_ptr<objT>, memfnT, typename result_of<memfnT>::type>
311 wrap_mem_fn(std::shared_ptr<objT>& obj, memfnT memfn) {
313 typename memfunc_traits<memfnT>::result_type>(obj, memfn);
314 }
315
316 /// \brief Create a member function wrapper (\c MemFuncWrapper) from a
317 /// const shared pointer and a member function pointer.
318
319 /// \tparam objT The object type.
320 /// \tparam memfnT The member function pointer type.
321 /// \param[in] obj Shared pointer to the object.
322 /// \param[in] memfn The member function pointer.
323 /// \return A wrapped member function pointer.
324 template <typename objT, typename memfnT>
325 MemFuncWrapper<std::shared_ptr<objT>, memfnT, typename result_of<memfnT>::type>
326 wrap_mem_fn(const std::shared_ptr<objT>& obj, memfnT memfn) {
328 typename memfunc_traits<memfnT>::result_type>(obj, memfn);
329 }
330
331 /// Returns the member function pointer from a wrapper.
332
333 /// \tparam ptrT Pointer type.
334 /// \tparam memfnT Member function pointer type.
335 /// \tparam resT Member function return type.
336 /// \param wrapper Wrapper to the member function.
337 /// \return The member function pointer.
338 template <typename ptrT, typename memfnT, typename resT>
340 return wrapper.memfn_;
341 }
342
343 } // namespace detail
344} // namespace madness
345
346#endif // MADNESS_WORLD_MEM_FUNC_WRAPPER_H__INCLUDED
Functor wrapper for object and member function pointers.
Definition mem_func_wrapper.h:171
MemFuncWrapper(const MemFuncWrapper_ &other)
Copy constructor.
Definition mem_func_wrapper.h:196
memfnT memfn_type
Alias for the member function pointer type.
Definition mem_func_wrapper.h:186
memfnT memfn_
Pointer to the desired member function.
Definition mem_func_wrapper.h:174
ptrT ptr_
Pointer to the object.
Definition mem_func_wrapper.h:173
MemFuncWrapper()
Construct a wrapper with the default pointer for this type.
Definition mem_func_wrapper.h:189
MemFuncWrapper< ptrT, memfnT, void > MemFuncWrapper_
Alias for a member function with void return type.
Definition mem_func_wrapper.h:180
MemFuncWrapper_ & operator=(const MemFuncWrapper_ &other)
Definition mem_func_wrapper.h:213
void serialize(const Archive &ar)
Serializes a MemFuncWrapper.
Definition mem_func_wrapper.h:235
MemFuncWrapper(ptrT ptr, memfnT memfn)
Construct a wrapper from an object pointer and a member function pointer.
Definition mem_func_wrapper.h:205
void result_type
Alias for the function's return type.
Definition mem_func_wrapper.h:183
void operator()(argTs &&... args) const
Evaluates the member function, when dereferenced from the object pointer.
Definition mem_func_wrapper.h:225
Functor wrapper for object and member function pointers.
Definition mem_func_wrapper.h:89
MemFuncWrapper(const MemFuncWrapper_ &other)
Copy constructor that copies the object pointer and member function pointer.
Definition mem_func_wrapper.h:114
MemFuncWrapper_ & operator=(const MemFuncWrapper_ &other)
Definition mem_func_wrapper.h:131
MemFuncWrapper< ptrT, memfnT, void > MemFuncWrapper_
Alias for a wrapper that returns void.
Definition mem_func_wrapper.h:98
friend memfnT get_mem_func_ptr(const MemFuncWrapper_ &wrapper)
Definition mem_func_wrapper.h:158
result_type operator()(argTs &&... args) const
Evaluates the member function, when dereferenced from the object pointer.
Definition mem_func_wrapper.h:144
memfnT memfn_type
Alias for the type of the member function pointer.
Definition mem_func_wrapper.h:104
memfnT memfn_
Member function of the object's type.
Definition mem_func_wrapper.h:92
MemFuncWrapper(ptrT ptr, memfnT memfn)
Constructs a wrapper from an object pointer and a member function pointer.
Definition mem_func_wrapper.h:123
resT result_type
Alias for the function's result type.
Definition mem_func_wrapper.h:101
MemFuncWrapper()
Constructor that sets the pointer to the default value from DefaultInitPtr<ptrT>.
Definition mem_func_wrapper.h:108
void serialize(const Archive &ar)
Serializes a MemFuncWrapper.
Definition mem_func_wrapper.h:154
ptrT ptr_
Pointer to the object.
Definition mem_func_wrapper.h:91
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:34
Defines madness::MadnessException for exception handling.
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition madness_exception.h:134
MemFuncWrapper< objT *, memfnT, typename result_of< memfnT >::type > wrap_mem_fn(objT &obj, memfnT memfn)
Create a member function wrapper (MemFuncWrapper) from an object and a member function pointer.
Definition mem_func_wrapper.h:251
memfnT get_mem_func_ptr(const MemFuncWrapper< ptrT, memfnT, resT > &wrapper)
Returns the member function pointer from a wrapper.
Definition mem_func_wrapper.h:339
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
Definition mraimpl.h:50
static std::shared_ptr< T > init()
Get a default shared pointer.
Definition mem_func_wrapper.h:75
Default pointer to a. object of type T.
Definition mem_func_wrapper.h:53
static T init()
Get a default pointer.
Definition mem_func_wrapper.h:58
Member function traits in the spirit of boost function traits.
Definition function_traits.h:21
fnT::result_type type
Definition function_traits.h:97