1 // Boost.Function library
3 // Copyright (C) 2001 Doug Gregor (gregod@cs.rpi.edu)
5 // Permission to copy, use, sell and distribute this software is granted
6 // provided this copyright notice appears in all copies.
7 // Permission to modify the code and to distribute modified code is granted
8 // provided this copyright notice appears in all copies, and a notice
9 // that the code was modified is included with the copyright notice.
11 // This software is provided "as is" without express or implied warranty,
12 // and with no claim as to its suitability for any purpose.
14 // For more information, see http://www.boost.org
16 #ifndef BOOST_FUNCTION_BASE_HEADER
17 #define BOOST_FUNCTION_BASE_HEADER
24 #include <boost/config.hpp>
25 #include <boost/type_traits.hpp>
26 #include <boost/ref.hpp>
28 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 || defined(__ICL) && __ICL <= 600 || defined(__MWERKS__) && __MWERKS__ < 0x2406
29 # define BOOST_FUNCTION_TARGET_FIX(x) x
31 # define BOOST_FUNCTION_TARGET_FIX(x)
37 template<bool> struct truth {};
40 * The ct_if implementation is temporary code. When a Boost metaprogramming
41 * library is introduced, Boost.Function will use it instead.
46 template<typename Then, typename Else>
55 template<typename Then, typename Else>
62 template<bool Condition>
65 typedef SelectThen type;
69 struct Selector<false>
71 typedef SelectElse type;
73 } // end namespace intimate
75 template<bool Condition, typename Then, typename Else>
78 typedef typename intimate::Selector<Condition>::type select;
79 typedef typename select::template Result<Then,Else>::type type;
83 * A union of a function pointer and a void pointer. This is necessary
84 * because 5.2.10/6 allows reinterpret_cast<> to safely cast between
85 * function pointer types and 5.2.9/10 allows static_cast<> to safely
86 * cast between a void pointer and an object pointer. But it is not legal
87 * to cast between a function pointer and a void* (in either direction),
88 * so function requires a union of the two. */
92 const void* const_obj_ptr;
95 explicit any_pointer(void* p) : obj_ptr(p) {}
96 explicit any_pointer(const void* p) : const_obj_ptr(p) {}
97 explicit any_pointer(void (*p)()) : func_ptr(p) {}
101 * The unusable class is a placeholder for unused function arguments
102 * It is also completely unusable except that it constructable from
103 * anything. This helps compilers without partial specialization to
104 * handle Boost.Function objects returning void.
109 template<typename T> unusable(const T&) {}
112 /* Determine the return type. This supports compilers that do not support
113 * void returns or partial specialization by silently changing the return
114 * type to "unusable".
116 template<typename T> struct function_return_type { typedef T type; };
119 struct function_return_type<void>
121 typedef unusable type;
124 // The operation type to perform on the given functor/function pointer
125 enum functor_manager_operation_type {
130 // Tags used to decide between different types of functions
131 struct function_ptr_tag {};
132 struct function_obj_tag {};
133 struct member_ptr_tag {};
134 struct function_obj_ref_tag {};
135 struct stateless_function_obj_tag {};
138 class get_function_tag
140 typedef typename ct_if<(is_pointer<F>::value),
142 function_obj_tag>::type ptr_or_obj_tag;
144 typedef typename ct_if<(is_member_pointer<F>::value),
146 ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag;
148 typedef typename ct_if<(is_reference_wrapper<F>::value),
149 function_obj_ref_tag,
150 ptr_or_obj_or_mem_tag>::type or_ref_tag;
153 typedef typename ct_if<(is_stateless<F>::value),
154 stateless_function_obj_tag,
155 or_ref_tag>::type type;
158 // The trivial manager does nothing but return the same pointer (if we
159 // are cloning) or return the null pointer (if we are deleting).
160 inline any_pointer trivial_manager(any_pointer f,
161 functor_manager_operation_type op)
163 if (op == clone_functor_tag)
166 return any_pointer(reinterpret_cast<void*>(0));
170 * The functor_manager class contains a static function "manage" which
171 * can clone or destroy the given function/function object pointer.
173 template<typename Functor, typename Allocator>
174 struct functor_manager
177 typedef Functor functor_type;
179 // For function pointers, the manager is trivial
180 static inline any_pointer
181 manager(any_pointer function_ptr,
182 functor_manager_operation_type op,
185 if (op == clone_functor_tag)
188 return any_pointer(static_cast<void (*)()>(0));
191 // For function object pointers, we clone the pointer to each
192 // function has its own version.
193 static inline any_pointer
194 manager(any_pointer function_obj_ptr,
195 functor_manager_operation_type op,
198 #ifndef BOOST_NO_STD_ALLOCATOR
199 typedef typename Allocator::template rebind<functor_type>::other
201 typedef typename allocator_type::pointer pointer_type;
203 typedef functor_type* pointer_type;
204 #endif // BOOST_NO_STD_ALLOCATOR
206 # ifndef BOOST_NO_STD_ALLOCATOR
207 allocator_type allocator;
208 # endif // BOOST_NO_STD_ALLOCATOR
210 if (op == clone_functor_tag) {
212 static_cast<functor_type*>(function_obj_ptr.obj_ptr);
215 # ifndef BOOST_NO_STD_ALLOCATOR
216 pointer_type copy = allocator.allocate(1);
217 allocator.construct(copy, *f);
219 // Get back to the original pointer type
220 functor_type* new_f = static_cast<functor_type*>(copy);
222 functor_type* new_f = new functor_type(*f);
223 # endif // BOOST_NO_STD_ALLOCATOR
224 return any_pointer(static_cast<void*>(new_f));
227 /* Cast from the void pointer to the functor pointer type */
229 reinterpret_cast<functor_type*>(function_obj_ptr.obj_ptr);
231 # ifndef BOOST_NO_STD_ALLOCATOR
232 /* Cast from the functor pointer type to the allocator's pointer
234 pointer_type victim = static_cast<pointer_type>(f);
236 // Destroy and deallocate the functor
237 allocator.destroy(victim);
238 allocator.deallocate(victim, 1);
241 # endif // BOOST_NO_STD_ALLOCATOR
243 return any_pointer(static_cast<void*>(0));
247 /* Dispatch to an appropriate manager based on whether we have a
248 function pointer or a function object pointer. */
250 manage(any_pointer functor_ptr, functor_manager_operation_type op)
252 typedef typename get_function_tag<functor_type>::type tag_type;
253 return manager(functor_ptr, op, tag_type());
257 // value=1 if the given type is not "unusable"
261 BOOST_STATIC_CONSTANT(int, value = 1);
264 // value=0 for unusable types
266 struct count_if_used<unusable>
268 BOOST_STATIC_CONSTANT(int, value = 0);
271 // Count the number of arguments (from the given set) which are not
272 // "unusable" (therefore, count those arguments that are used).
273 template<typename T1, typename T2, typename T3, typename T4,
274 typename T5, typename T6, typename T7, typename T8,
275 typename T9, typename T10>
276 struct count_used_args
278 BOOST_STATIC_CONSTANT(int, value =
279 (count_if_used<T1>::value +
280 count_if_used<T2>::value +
281 count_if_used<T3>::value +
282 count_if_used<T4>::value +
283 count_if_used<T5>::value +
284 count_if_used<T6>::value +
285 count_if_used<T7>::value +
286 count_if_used<T8>::value +
287 count_if_used<T9>::value +
288 count_if_used<T10>::value));
290 } // end namespace function
291 } // end namespace detail
294 * The function_base class contains the basic elements needed for the
295 * function1, function2, function3, etc. classes. It is common to all
296 * functions (and as such can be used to tell if we have one of the
297 * functionN objects).
302 function_base() : manager(0), functor(static_cast<void*>(0)) {}
304 // Is this function empty?
305 bool empty() const { return !manager; }
307 public: // should be protected, but GCC 2.95.3 will fail to allow access
308 detail::function::any_pointer (*manager)(
309 detail::function::any_pointer,
310 detail::function::functor_manager_operation_type);
311 detail::function::any_pointer functor;
313 #if (defined __SUNPRO_CC) && (__SUNPRO_CC <= 0x530) && !(defined BOOST_NO_COMPILER_CONFIG)
314 // Sun C++ 5.3 can't handle the safe_bool idiom, so don't use it
315 operator bool () const { return !this->empty(); }
322 typedef void (dummy::*safe_bool)();
325 operator safe_bool () const
326 { return (this->empty())? 0 : &dummy::nonnull; }
328 safe_bool operator!() const
329 { return (this->empty())? &dummy::nonnull : 0; }
333 /* Poison comparison between Boost.Function objects (because it is
334 * meaningless). The comparisons would otherwise be allowed because of the
335 * conversion required to allow syntax such as:
336 * boost::function<int, int> f;
339 void operator==(const function_base&, const function_base&);
340 void operator!=(const function_base&, const function_base&);
344 inline bool has_empty_target(const function_base* f)
349 inline bool has_empty_target(...)
353 } // end namespace function
354 } // end namespace detail
356 // The default function policy is to do nothing before and after the call.
357 struct empty_function_policy
359 inline void precall(const function_base*) {}
360 inline void postcall(const function_base*) {}
363 // The default function mixin does nothing. The assignment and
364 // copy-construction operators are all defined because MSVC defines broken
366 struct empty_function_mixin
368 empty_function_mixin() {}
369 empty_function_mixin(const empty_function_mixin&) {}
371 empty_function_mixin& operator=(const empty_function_mixin&)
378 #endif // BOOST_FUNCTION_BASE_HEADER