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 functor_type* out_functor =
288 reinterpret_cast<functor_type*>(&out_buffer.data);
289 // Some compilers (Borland, vc6, ...) are unhappy with ~functor_type.
290 out_functor->~Functor();
291 } else /* op == check_functor_type_tag */ {
292 const std::type_info& check_type =
293 *static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
294 if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(Functor)))
295 out_buffer.obj_ptr = &in_buffer.data;
297 out_buffer.obj_ptr = 0;
301 // Function objects that require heap allocation
303 manager(const function_buffer& in_buffer, function_buffer& out_buffer,
304 functor_manager_operation_type op, mpl::false_)
306 #ifndef BOOST_NO_STD_ALLOCATOR
307 typedef typename Allocator::template rebind<functor_type>::other
309 typedef typename allocator_type::pointer pointer_type;
311 typedef functor_type* pointer_type;
312 #endif // BOOST_NO_STD_ALLOCATOR
314 # ifndef BOOST_NO_STD_ALLOCATOR
315 allocator_type allocator;
316 # endif // BOOST_NO_STD_ALLOCATOR
318 if (op == clone_functor_tag) {
319 // GCC 2.95.3 gets the CV qualifiers wrong here, so we
320 // can't do the static_cast that we should do.
321 const functor_type* f =
322 (const functor_type*)(in_buffer.obj_ptr);
325 # ifndef BOOST_NO_STD_ALLOCATOR
326 pointer_type copy = allocator.allocate(1);
327 allocator.construct(copy, *f);
329 // Get back to the original pointer type
330 functor_type* new_f = static_cast<functor_type*>(copy);
332 functor_type* new_f = new functor_type(*f);
333 # endif // BOOST_NO_STD_ALLOCATOR
334 out_buffer.obj_ptr = new_f;
335 } else if (op == destroy_functor_tag) {
336 /* Cast from the void pointer to the functor pointer type */
338 static_cast<functor_type*>(out_buffer.obj_ptr);
340 # ifndef BOOST_NO_STD_ALLOCATOR
341 /* Cast from the functor pointer type to the allocator's pointer
343 pointer_type victim = static_cast<pointer_type>(f);
345 // Destroy and deallocate the functor
346 allocator.destroy(victim);
347 allocator.deallocate(victim, 1);
350 # endif // BOOST_NO_STD_ALLOCATOR
351 out_buffer.obj_ptr = 0;
352 } else /* op == check_functor_type_tag */ {
353 const std::type_info& check_type =
354 *static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
355 if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(Functor)))
356 out_buffer.obj_ptr = in_buffer.obj_ptr;
358 out_buffer.obj_ptr = 0;
362 // For function objects, we determine whether the function
363 // object can use the small-object optimization buffer or
364 // whether we need to allocate it on the heap.
366 manager(const function_buffer& in_buffer, function_buffer& out_buffer,
367 functor_manager_operation_type op, function_obj_tag)
369 manager(in_buffer, out_buffer, op,
370 mpl::bool_<(function_allows_small_object_optimization<functor_type>::value)>());
374 /* Dispatch to an appropriate manager based on whether we have a
375 function pointer or a function object pointer. */
377 manage(const function_buffer& in_buffer, function_buffer& out_buffer,
378 functor_manager_operation_type op)
380 typedef typename get_function_tag<functor_type>::type tag_type;
382 case get_functor_type_tag:
383 out_buffer.const_obj_ptr = &typeid(functor_type);
387 manager(in_buffer, out_buffer, op, tag_type());
393 // A type that is only used for comparisons against zero
394 struct useless_clear_type {};
396 #ifdef BOOST_NO_SFINAE
397 // These routines perform comparisons between a Boost.Function
398 // object and an arbitrary function object (when the last
399 // parameter is mpl::bool_<false>) or against zero (when the
400 // last parameter is mpl::bool_<true>). They are only necessary
401 // for compilers that don't support SFINAE.
402 template<typename Function, typename Functor>
404 compare_equal(const Function& f, const Functor&, int, mpl::bool_<true>)
405 { return f.empty(); }
407 template<typename Function, typename Functor>
409 compare_not_equal(const Function& f, const Functor&, int,
411 { return !f.empty(); }
413 template<typename Function, typename Functor>
415 compare_equal(const Function& f, const Functor& g, long,
418 if (const Functor* fp = f.template target<Functor>())
419 return function_equal(*fp, g);
423 template<typename Function, typename Functor>
425 compare_equal(const Function& f, const reference_wrapper<Functor>& g,
426 int, mpl::bool_<false>)
428 if (const Functor* fp = f.template target<Functor>())
429 return fp == g.get_pointer();
433 template<typename Function, typename Functor>
435 compare_not_equal(const Function& f, const Functor& g, long,
438 if (const Functor* fp = f.template target<Functor>())
439 return !function_equal(*fp, g);
443 template<typename Function, typename Functor>
445 compare_not_equal(const Function& f,
446 const reference_wrapper<Functor>& g, int,
449 if (const Functor* fp = f.template target<Functor>())
450 return fp != g.get_pointer();
453 #endif // BOOST_NO_SFINAE
456 * Stores the "manager" portion of the vtable for a
457 * boost::function object.
461 vtable_base() : manager(0) { }
462 void (*manager)(const function_buffer& in_buffer,
463 function_buffer& out_buffer,
464 functor_manager_operation_type op);
466 } // end namespace function
467 } // end namespace detail
470 * The function_base class contains the basic elements needed for the
471 * function1, function2, function3, etc. classes. It is common to all
472 * functions (and as such can be used to tell if we have one of the
473 * functionN objects).
478 function_base() : vtable(0) { }
480 /** Determine if the function is empty (i.e., has no target). */
481 bool empty() const { return !vtable; }
483 /** Retrieve the type of the stored function object, or typeid(void)
485 const std::type_info& target_type() const
487 if (!vtable) return typeid(void);
489 detail::function::function_buffer type;
490 vtable->manager(functor, type, detail::function::get_functor_type_tag);
491 return *static_cast<const std::type_info*>(type.const_obj_ptr);
494 template<typename Functor>
497 if (!vtable) return 0;
499 detail::function::function_buffer type_result;
500 type_result.const_obj_ptr = &typeid(Functor);
501 vtable->manager(functor, type_result,
502 detail::function::check_functor_type_tag);
503 return static_cast<Functor*>(type_result.obj_ptr);
506 template<typename Functor>
507 #if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
508 const Functor* target( Functor * = 0 ) const
510 const Functor* target() const
513 if (!vtable) return 0;
515 detail::function::function_buffer type_result;
516 type_result.const_obj_ptr = &typeid(Functor);
517 vtable->manager(functor, type_result,
518 detail::function::check_functor_type_tag);
519 // GCC 2.95.3 gets the CV qualifiers wrong here, so we
520 // can't do the static_cast that we should do.
521 return (const Functor*)(type_result.obj_ptr);
525 bool contains(const F& f) const
527 #if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
528 if (const F* fp = this->target( (F*)0 ))
530 if (const F* fp = this->template target<F>())
533 return function_equal(*fp, f);
539 #if defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3
540 // GCC 3.3 and newer cannot copy with the global operator==, due to
541 // problems with instantiation of function return types before it
542 // has been verified that the argument types match up.
543 template<typename Functor>
544 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
545 operator==(Functor g) const
547 if (const Functor* fp = target<Functor>())
548 return function_equal(*fp, g);
552 template<typename Functor>
553 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
554 operator!=(Functor g) const
556 if (const Functor* fp = target<Functor>())
557 return !function_equal(*fp, g);
562 public: // should be protected, but GCC 2.95.3 will fail to allow access
563 detail::function::vtable_base* vtable;
564 mutable detail::function::function_buffer functor;
568 * The bad_function_call exception class is thrown when a boost::function
571 class bad_function_call : public std::runtime_error
574 bad_function_call() : std::runtime_error("call to empty boost::function") {}
577 #ifndef BOOST_NO_SFINAE
578 inline bool operator==(const function_base& f,
579 detail::function::useless_clear_type*)
584 inline bool operator!=(const function_base& f,
585 detail::function::useless_clear_type*)
590 inline bool operator==(detail::function::useless_clear_type*,
591 const function_base& f)
596 inline bool operator!=(detail::function::useless_clear_type*,
597 const function_base& f)
603 #ifdef BOOST_NO_SFINAE
604 // Comparisons between boost::function objects and arbitrary function objects
605 template<typename Functor>
606 inline bool operator==(const function_base& f, Functor g)
608 typedef mpl::bool_<(is_integral<Functor>::value)> integral;
609 return detail::function::compare_equal(f, g, 0, integral());
612 template<typename Functor>
613 inline bool operator==(Functor g, const function_base& f)
615 typedef mpl::bool_<(is_integral<Functor>::value)> integral;
616 return detail::function::compare_equal(f, g, 0, integral());
619 template<typename Functor>
620 inline bool operator!=(const function_base& f, Functor g)
622 typedef mpl::bool_<(is_integral<Functor>::value)> integral;
623 return detail::function::compare_not_equal(f, g, 0, integral());
626 template<typename Functor>
627 inline bool operator!=(Functor g, const function_base& f)
629 typedef mpl::bool_<(is_integral<Functor>::value)> integral;
630 return detail::function::compare_not_equal(f, g, 0, integral());
634 # if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
635 // Comparisons between boost::function objects and arbitrary function
636 // objects. GCC 3.3 and before has an obnoxious bug that prevents this
638 template<typename Functor>
639 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
640 operator==(const function_base& f, Functor g)
642 if (const Functor* fp = f.template target<Functor>())
643 return function_equal(*fp, g);
647 template<typename Functor>
648 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
649 operator==(Functor g, const function_base& f)
651 if (const Functor* fp = f.template target<Functor>())
652 return function_equal(g, *fp);
656 template<typename Functor>
657 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
658 operator!=(const function_base& f, Functor g)
660 if (const Functor* fp = f.template target<Functor>())
661 return !function_equal(*fp, g);
665 template<typename Functor>
666 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
667 operator!=(Functor g, const function_base& f)
669 if (const Functor* fp = f.template target<Functor>())
670 return !function_equal(g, *fp);
675 template<typename Functor>
676 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
677 operator==(const function_base& f, reference_wrapper<Functor> g)
679 if (const Functor* fp = f.template target<Functor>())
680 return fp == g.get_pointer();
684 template<typename Functor>
685 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
686 operator==(reference_wrapper<Functor> g, const function_base& f)
688 if (const Functor* fp = f.template target<Functor>())
689 return g.get_pointer() == fp;
693 template<typename Functor>
694 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
695 operator!=(const function_base& f, reference_wrapper<Functor> g)
697 if (const Functor* fp = f.template target<Functor>())
698 return fp != g.get_pointer();
702 template<typename Functor>
703 BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
704 operator!=(reference_wrapper<Functor> g, const function_base& f)
706 if (const Functor* fp = f.template target<Functor>())
707 return g.get_pointer() != fp;
711 #endif // Compiler supporting SFINAE
715 inline bool has_empty_target(const function_base* f)
720 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
721 inline bool has_empty_target(const void*)
726 inline bool has_empty_target(...)
731 } // end namespace function
732 } // end namespace detail
733 } // end namespace boost
735 #undef BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL
736 #undef BOOST_FUNCTION_COMPARE_TYPE_ID
738 #endif // BOOST_FUNCTION_BASE_HEADER