Update contrib.
1 // Boost.Function library
3 // Copyright Douglas Gregor 2001-2006. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // For more information, see http://www.boost.org
10 #ifndef BOOST_FUNCTION_BASE_HEADER
11 #define BOOST_FUNCTION_BASE_HEADER
18 #include <boost/config.hpp>
19 #include <boost/assert.hpp>
20 #include <boost/type_traits/is_integral.hpp>
21 #include <boost/type_traits/composite_traits.hpp>
22 #include <boost/ref.hpp>
23 #include <boost/mpl/if.hpp>
24 #include <boost/detail/workaround.hpp>
25 #include <boost/type_traits/alignment_of.hpp>
26 #ifndef BOOST_NO_SFINAE
27 # include "boost/utility/enable_if.hpp"
29 # include "boost/mpl/bool.hpp"
31 #include <boost/function_equal.hpp>
33 // Borrowed from Boost.Python library: determines the cases where we
34 // need to use std::type_info::name to compare instead of operator==.
35 # if (defined(__GNUC__) && __GNUC__ >= 3) \
37 || ( defined(__sgi) && defined(__host_mips))
39 # define BOOST_FUNCTION_COMPARE_TYPE_ID(X,Y) \
40 (std::strcmp((X).name(),(Y).name()) == 0)
42 # define BOOST_FUNCTION_COMPARE_TYPE_ID(X,Y) ((X)==(Y))
45 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 || defined(__ICL) && __ICL <= 600 || defined(__MWERKS__) && __MWERKS__ < 0x2406 && !defined(BOOST_STRICT_CONFIG)
46 # define BOOST_FUNCTION_TARGET_FIX(x) x
48 # define BOOST_FUNCTION_TARGET_FIX(x)
51 #if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730 && !defined(BOOST_STRICT_CONFIG)
52 // Work around a compiler bug.
53 // boost::python::objects::function has to be seen by the compiler before the
54 // boost::function class template.
55 namespace boost { namespace python { namespace objects {
60 #if defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \
61 || defined(BOOST_BCB_PARTIAL_SPECIALIZATION_BUG) \
62 || !(BOOST_STRICT_CONFIG || !defined(__SUNPRO_CC) || __SUNPRO_CC > 0x540)
63 # define BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX
66 #if !BOOST_WORKAROUND(__BORLANDC__, < 0x600)
67 # define BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor,Type) \
68 typename ::boost::enable_if_c<(::boost::type_traits::ice_not< \
69 (::boost::is_integral<Functor>::value)>::value), \
72 // BCC doesn't recognize this depends on a template argument and complains
73 // about the use of 'typename'
74 # define BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor,Type) \
75 ::boost::enable_if_c<(::boost::type_traits::ice_not< \
76 (::boost::is_integral<Functor>::value)>::value), \
80 #if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX)
83 #if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730 && !defined(BOOST_STRICT_CONFIG)
84 // The library shipping with MIPSpro 7.3.1.3m has a broken allocator<void>
87 template<typename Signature,
88 typename Allocator = std::allocator<function_base> >
91 template<typename Signature, typename Allocator = std::allocator<void> >
95 template<typename Signature, typename Allocator>
96 inline void swap(function<Signature, Allocator>& f1,
97 function<Signature, Allocator>& f2)
102 } // end namespace boost
103 #endif // have partial specialization
111 * A buffer used to store small function objects in
112 * boost::function. It is a union containing function pointers,
113 * object pointers, and a structure that resembles a bound
114 * member function pointer.
116 union function_buffer
118 // For pointers to function objects
121 // For pointers to std::type_info objects
122 // (get_functor_type_tag, check_functor_type_tag).
123 const void* const_obj_ptr;
125 // For function pointers of all kinds
126 mutable void (*func_ptr)();
128 // For bound member pointers
129 struct bound_memfunc_ptr_t {
130 void (X::*memfunc_ptr)(int);
134 // To relax aliasing constraints
139 * The unusable class is a placeholder for unused function arguments
140 * It is also completely unusable except that it constructable from
141 * anything. This helps compilers without partial specialization to
142 * handle Boost.Function objects returning void.
147 template<typename T> unusable(const T&) {}
150 /* Determine the return type. This supports compilers that do not support
151 * void returns or partial specialization by silently changing the return
152 * type to "unusable".
154 template<typename T> struct function_return_type { typedef T type; };
157 struct function_return_type<void>
159 typedef unusable type;
162 // The operation type to perform on the given functor/function pointer
163 enum functor_manager_operation_type {
166 check_functor_type_tag,
170 // Tags used to decide between different types of functions
171 struct function_ptr_tag {};
172 struct function_obj_tag {};
173 struct member_ptr_tag {};
174 struct function_obj_ref_tag {};
177 class get_function_tag
179 typedef typename mpl::if_c<(is_pointer<F>::value),
181 function_obj_tag>::type ptr_or_obj_tag;
183 typedef typename mpl::if_c<(is_member_pointer<F>::value),
185 ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag;
187 typedef typename mpl::if_c<(is_reference_wrapper<F>::value),
188 function_obj_ref_tag,
189 ptr_or_obj_or_mem_tag>::type or_ref_tag;
192 typedef or_ref_tag type;
195 // The trivial manager does nothing but return the same pointer (if we
196 // are cloning) or return the null pointer (if we are deleting).
198 struct reference_manager
201 get(const function_buffer& in_buffer, function_buffer& out_buffer,
202 functor_manager_operation_type op)
205 case clone_functor_tag:
206 out_buffer.obj_ptr = in_buffer.obj_ptr;
209 case destroy_functor_tag:
210 out_buffer.obj_ptr = 0;
213 case check_functor_type_tag:
215 // DPG TBD: Since we're only storing a pointer, it's
216 // possible that the user could ask for a base class or
217 // derived class. Is that okay?
218 const std::type_info& check_type =
219 *static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
220 if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(F)))
221 out_buffer.obj_ptr = in_buffer.obj_ptr;
223 out_buffer.obj_ptr = 0;
227 case get_functor_type_tag:
228 out_buffer.const_obj_ptr = &typeid(F);
235 * Determine if boost::function can use the small-object
236 * optimization with the function object type F.
239 struct function_allows_small_object_optimization
241 BOOST_STATIC_CONSTANT
243 value = ((sizeof(F) <= sizeof(function_buffer) &&
244 (alignment_of<function_buffer>::value
245 % alignment_of<F>::value == 0))));
249 * The functor_manager class contains a static function "manage" which
250 * can clone or destroy the given function/function object pointer.
252 template<typename Functor, typename Allocator>
253 struct functor_manager
256 typedef Functor functor_type;
258 // For function pointers, the manager is trivial
260 manager(const function_buffer& in_buffer, function_buffer& out_buffer,
261 functor_manager_operation_type op, function_ptr_tag)
263 if (op == clone_functor_tag)
264 out_buffer.func_ptr = in_buffer.func_ptr;
265 else if (op == destroy_functor_tag)
266 out_buffer.func_ptr = 0;
267 else /* op == check_functor_type_tag */ {
268 const std::type_info& check_type =
269 *static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
270 if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(Functor)))
271 out_buffer.obj_ptr = &in_buffer.func_ptr;
273 out_buffer.obj_ptr = 0;
277 // Function objects that fit in the small-object buffer.
279 manager(const function_buffer& in_buffer, function_buffer& out_buffer,
280 functor_manager_operation_type op, mpl::true_)
282 if (op == clone_functor_tag) {
283 const functor_type* in_functor =
284 reinterpret_cast<const functor_type*>(&in_buffer.data);
285 new ((void*)&out_buffer.data) functor_type(*in_functor);
286 } else if (op == destroy_functor_tag) {
287 // Some compilers (Borland, vc6, ...) are unhappy with ~functor_type.
288 reinterpret_cast<functor_type*>(&out_buffer.data)->~Functor();
289 } else /* op == check_functor_type_tag */ {
290 const std::type_info& check_type =
291 *static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
292 if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(Functor)))
293 out_buffer.obj_ptr = &in_buffer.data;
295 out_buffer.obj_ptr = 0;
299 // Function objects that require heap allocation
301 manager(const function_buffer& in_buffer, function_buffer& out_buffer,
302 functor_manager_operation_type op, mpl::false_)
304 #ifndef BOOST_NO_STD_ALLOCATOR
305 typedef typename Allocator::template rebind<functor_type>::other
307 typedef typename allocator_type::pointer pointer_type;
309 typedef functor_type* pointer_type;
310 #endif // BOOST_NO_STD_ALLOCATOR
312 # ifndef BOOST_NO_STD_ALLOCATOR
313 allocator_type allocator;
314 # endif // BOOST_NO_STD_ALLOCATOR
316 if (op == clone_functor_tag) {
317 // GCC 2.95.3 gets the CV qualifiers wrong here, so we
318 // can't do the static_cast that we should do.
319 const functor_type* f =
320 (const functor_type*)(in_buffer.obj_ptr);
323 # ifndef BOOST_NO_STD_ALLOCATOR
324 pointer_type copy = allocator.allocate(1);
325 allocator.construct(copy, *f);
327 // Get back to the original pointer type
328 functor_type* new_f = static_cast<functor_type*>(copy);
330 functor_type* new_f = new functor_type(*f);
331 # endif // BOOST_NO_STD_ALLOCATOR
332 out_buffer.obj_ptr = new_f;
333 } else if (op == destroy_functor_tag) {
334 /* Cast from the void pointer to the functor pointer type */
336 static_cast<functor_type*>(out_buffer.obj_ptr);
338 # ifndef BOOST_NO_STD_ALLOCATOR
339 /* Cast from the functor pointer type to the allocator's pointer
341 pointer_type victim = static_cast<pointer_type>(f);
343 // Destroy and deallocate the functor
344 allocator.destroy(victim);
345 allocator.deallocate(victim, 1);
348 # endif // BOOST_NO_STD_ALLOCATOR
349 out_buffer.obj_ptr = 0;
350 } else /* op == check_functor_type_tag */ {
351 const std::type_info& check_type =
352 *static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
353 if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(Functor)))
354 out_buffer.obj_ptr = in_buffer.obj_ptr;
356 out_buffer.obj_ptr = 0;
360 // For function objects, we determine whether the function
361 // object can use the small-object optimization buffer or
362 // whether we need to allocate it on the heap.
364 manager(const function_buffer& in_buffer, function_buffer& out_buffer,
365 functor_manager_operation_type op, function_obj_tag)
367 manager(in_buffer, out_buffer, op,
368 mpl::bool_<(function_allows_small_object_optimization<functor_type>::value)>());
372 /* Dispatch to an appropriate manager based on whether we have a
373 function pointer or a function object pointer. */
375 manage(const function_buffer& in_buffer, function_buffer& out_buffer,
376 functor_manager_operation_type op)
378 typedef typename get_function_tag<functor_type>::type tag_type;
380 case get_functor_type_tag:
381 out_buffer.const_obj_ptr = &typeid(functor_type);
385 manager(in_buffer, out_buffer, op, tag_type());
391 // A type that is only used for comparisons against zero
392 struct useless_clear_type {};
394 #ifdef BOOST_NO_SFINAE
395 // These routines perform comparisons between a Boost.Function
396 // object and an arbitrary function object (when the last
397 // parameter is mpl::bool_<false>) or against zero (when the
398 // last parameter is mpl::bool_<true>). They are only necessary
399 // for compilers that don't support SFINAE.
400 template<typename Function, typename Functor>
402 compare_equal(const Function& f, const Functor&, int, mpl::bool_<true>)
403 { return f.empty(); }
405 template<typename Function, typename Functor>
407 compare_not_equal(const Function& f, const Functor&, int,
409 { return !f.empty(); }
411 template<typename Function, typename Functor>
413 compare_equal(const Function& f, const Functor& g, long,
416 if (const Functor* fp = f.template target<Functor>())
417 return function_equal(*fp, g);
421 template<typename Function, typename Functor>
423 compare_equal(const Function& f, const reference_wrapper<Functor>& g,
424 int, mpl::bool_<false>)
426 if (const Functor* fp = f.template target<Functor>())
427 return fp == g.get_pointer();
431 template<typename Function, typename Functor>
433 compare_not_equal(const Function& f, const Functor& g, long,
436 if (const Functor* fp = f.template target<Functor>())
437 return !function_equal(*fp, g);
441 template<typename Function, typename Functor>
443 compare_not_equal(const Function& f,
444 const reference_wrapper<Functor>& g, int,
447 if (const Functor* fp = f.template target<Functor>())
448 return fp != g.get_pointer();
451 #endif // BOOST_NO_SFINAE
454 * Stores the "manager" portion of the vtable for a
455 * boost::function object.
459 vtable_base() : manager(0) { }
460 void (*manager)(const function_buffer& in_buffer,
461 function_buffer& out_buffer,
462 functor_manager_operation_type op);
464 } // end namespace function
465 } // end namespace detail
468 * The function_base class contains the basic elements needed for the
469 * function1, function2, function3, etc. classes. It is common to all
470 * functions (and as such can be used to tell if we have one of the
471 * functionN objects).
476 function_base() : vtable(0) { }
478 /** Determine if the function is empty (i.e., has no target). */
479 bool empty() const { return !vtable; }
481 /** Retrieve the type of the stored function object, or typeid(void)
483 const std::type_info& target_type() const
485 if (!vtable) return typeid(void);
487 detail::function::function_buffer type;
488 vtable->manager(functor, type, detail::function::get_functor_type_tag);
489 return *static_cast<const std::type_info*>(type.const_obj_ptr);
492 template<typename Functor>
495 if (!vtable) return 0;
497 detail::function::function_buffer type_result;
498 type_result.const_obj_ptr = &typeid(Functor);
499 vtable->manager(functor, type_result,
500 detail::function::check_functor_type_tag);
501 return static_cast<Functor*>(type_result.obj_ptr);
504 template<typename Functor>
505 #if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
506 const Functor* target( Functor * = 0 ) const
508 const Functor* target() const
511 if (!vtable) return 0;
513 detail::function::function_buffer type_result;
514 type_result.const_obj_ptr = &typeid(Functor);
515 vtable->manager(functor, type_result,
516 detail::function::check_functor_type_tag);
517 // GCC 2.95.3 gets the CV qualifiers wrong here, so we
518 // can't do the static_cast that we should do.
519 return (const Functor*)(type_result.obj_ptr);
523 bool contains(const F& f) const
525 #if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
526 if (const F* fp = this->target( (F*)0 ))
528 if (const F* fp = this->template target<F>())
531 return function_equal(*fp, f);
537 #if defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3
538 // GCC 3.3 and newer cannot copy with the global operator==, due to
539 // problems with instantiation of function return types before it
540 // has been verified that the argument types match up.
541 template<typename Functor>
542 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
543 operator==(Functor g) const
545 if (const Functor* fp = target<Functor>())
546 return function_equal(*fp, g);
550 template<typename Functor>
551 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
552 operator!=(Functor g) const
554 if (const Functor* fp = target<Functor>())
555 return !function_equal(*fp, g);
560 public: // should be protected, but GCC 2.95.3 will fail to allow access
561 detail::function::vtable_base* vtable;
562 mutable detail::function::function_buffer functor;
566 * The bad_function_call exception class is thrown when a boost::function
569 class bad_function_call : public std::runtime_error
572 bad_function_call() : std::runtime_error("call to empty boost::function") {}
575 #ifndef BOOST_NO_SFINAE
576 inline bool operator==(const function_base& f,
577 detail::function::useless_clear_type*)
582 inline bool operator!=(const function_base& f,
583 detail::function::useless_clear_type*)
588 inline bool operator==(detail::function::useless_clear_type*,
589 const function_base& f)
594 inline bool operator!=(detail::function::useless_clear_type*,
595 const function_base& f)
601 #ifdef BOOST_NO_SFINAE
602 // Comparisons between boost::function objects and arbitrary function objects
603 template<typename Functor>
604 inline bool operator==(const function_base& f, Functor g)
606 typedef mpl::bool_<(is_integral<Functor>::value)> integral;
607 return detail::function::compare_equal(f, g, 0, integral());
610 template<typename Functor>
611 inline bool operator==(Functor g, const function_base& f)
613 typedef mpl::bool_<(is_integral<Functor>::value)> integral;
614 return detail::function::compare_equal(f, g, 0, integral());
617 template<typename Functor>
618 inline bool operator!=(const function_base& f, Functor g)
620 typedef mpl::bool_<(is_integral<Functor>::value)> integral;
621 return detail::function::compare_not_equal(f, g, 0, integral());
624 template<typename Functor>
625 inline bool operator!=(Functor g, const function_base& f)
627 typedef mpl::bool_<(is_integral<Functor>::value)> integral;
628 return detail::function::compare_not_equal(f, g, 0, integral());
632 # if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
633 // Comparisons between boost::function objects and arbitrary function
634 // objects. GCC 3.3 and before has an obnoxious bug that prevents this
636 template<typename Functor>
637 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
638 operator==(const function_base& f, Functor g)
640 if (const Functor* fp = f.template target<Functor>())
641 return function_equal(*fp, g);
645 template<typename Functor>
646 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
647 operator==(Functor g, const function_base& f)
649 if (const Functor* fp = f.template target<Functor>())
650 return function_equal(g, *fp);
654 template<typename Functor>
655 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
656 operator!=(const function_base& f, Functor g)
658 if (const Functor* fp = f.template target<Functor>())
659 return !function_equal(*fp, g);
663 template<typename Functor>
664 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
665 operator!=(Functor g, const function_base& f)
667 if (const Functor* fp = f.template target<Functor>())
668 return !function_equal(g, *fp);
673 template<typename Functor>
674 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
675 operator==(const function_base& f, reference_wrapper<Functor> g)
677 if (const Functor* fp = f.template target<Functor>())
678 return fp == g.get_pointer();
682 template<typename Functor>
683 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
684 operator==(reference_wrapper<Functor> g, const function_base& f)
686 if (const Functor* fp = f.template target<Functor>())
687 return g.get_pointer() == fp;
691 template<typename Functor>
692 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
693 operator!=(const function_base& f, reference_wrapper<Functor> g)
695 if (const Functor* fp = f.template target<Functor>())
696 return fp != g.get_pointer();
700 template<typename Functor>
701 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
702 operator!=(reference_wrapper<Functor> g, const function_base& f)
704 if (const Functor* fp = f.template target<Functor>())
705 return g.get_pointer() != fp;
709 #endif // Compiler supporting SFINAE
713 inline bool has_empty_target(const function_base* f)
718 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
719 inline bool has_empty_target(const void*)
724 inline bool has_empty_target(...)
729 } // end namespace function
730 } // end namespace detail
731 } // end namespace boost
733 #undef BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL
734 #undef BOOST_FUNCTION_COMPARE_TYPE_ID
736 #endif // BOOST_FUNCTION_BASE_HEADER