// Boost.Function library
-// Copyright Douglas Gregor 2001-2004. Use, modification and
-// distribution is subject to the Boost Software License, Version
-// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+// Copyright Douglas Gregor 2001-2006
+// Copyright Emil Dotchevski 2007
+// Use, modification and distribution is subject to the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// For more information, see http://www.boost.org
#include <string>
#include <memory>
#include <new>
-#include <typeinfo>
#include <boost/config.hpp>
+#include <boost/detail/sp_typeinfo.hpp>
#include <boost/assert.hpp>
+#include <boost/integer.hpp>
+#include <boost/type_traits/has_trivial_copy.hpp>
+#include <boost/type_traits/has_trivial_destructor.hpp>
+#include <boost/type_traits/is_const.hpp>
#include <boost/type_traits/is_integral.hpp>
+#include <boost/type_traits/is_volatile.hpp>
#include <boost/type_traits/composite_traits.hpp>
-#include <boost/type_traits/is_stateless.hpp>
+#include <boost/type_traits/ice.hpp>
#include <boost/ref.hpp>
-#include <boost/pending/ct_if.hpp>
+#include <boost/mpl/if.hpp>
#include <boost/detail/workaround.hpp>
+#include <boost/type_traits/alignment_of.hpp>
#ifndef BOOST_NO_SFINAE
# include "boost/utility/enable_if.hpp"
#else
# include "boost/mpl/bool.hpp"
#endif
#include <boost/function_equal.hpp>
+#include <boost/function/function_fwd.hpp>
+
+#if defined(BOOST_MSVC)
+# pragma warning( push )
+# pragma warning( disable : 4793 ) // complaint about native code generation
+# pragma warning( disable : 4127 ) // "conditional expression is constant"
+#endif
+
+// Define BOOST_FUNCTION_STD_NS to the namespace that contains type_info.
+#ifdef BOOST_NO_STD_TYPEINFO
+// Embedded VC++ does not have type_info in namespace std
+# define BOOST_FUNCTION_STD_NS
+#else
+# define BOOST_FUNCTION_STD_NS std
+#endif
// Borrowed from Boost.Python library: determines the cases where we
// need to use std::type_info::name to compare instead of operator==.
-# if (defined(__GNUC__) && __GNUC__ >= 3) \
+#if defined( BOOST_NO_TYPEID )
+# define BOOST_FUNCTION_COMPARE_TYPE_ID(X,Y) ((X)==(Y))
+#elif (defined(__GNUC__) && __GNUC__ >= 3) \
|| defined(_AIX) \
|| ( defined(__sgi) && defined(__host_mips))
# include <cstring>
# define BOOST_FUNCTION_TARGET_FIX(x)
#endif // not MSVC
-#if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730 && !defined(BOOST_STRICT_CONFIG)
-// Work around a compiler bug.
-// boost::python::objects::function has to be seen by the compiler before the
-// boost::function class template.
-namespace boost { namespace python { namespace objects {
- class function;
-}}}
-#endif
-
-#if defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \
- || defined(BOOST_BCB_PARTIAL_SPECIALIZATION_BUG) \
- || !(BOOST_STRICT_CONFIG || !defined(__SUNPRO_CC) || __SUNPRO_CC > 0x540)
-# define BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX
-#endif
-
-#define BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor,Type) \
- typename ::boost::enable_if_c<(::boost::type_traits::ice_not< \
- (::boost::is_integral<Functor>::value)>::value), \
- Type>::type
-
-
-#if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX)
-namespace boost {
-
-#if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730 && !defined(BOOST_STRICT_CONFIG)
-// The library shipping with MIPSpro 7.3.1.3m has a broken allocator<void>
-class function_base;
-
-template<typename Signature,
- typename Allocator = std::allocator<function_base> >
-class function;
+#if !BOOST_WORKAROUND(__BORLANDC__, < 0x5A0)
+# define BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor,Type) \
+ typename ::boost::enable_if_c<(::boost::type_traits::ice_not< \
+ (::boost::is_integral<Functor>::value)>::value), \
+ Type>::type
#else
-template<typename Signature, typename Allocator = std::allocator<void> >
-class function;
+// BCC doesn't recognize this depends on a template argument and complains
+// about the use of 'typename'
+# define BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor,Type) \
+ ::boost::enable_if_c<(::boost::type_traits::ice_not< \
+ (::boost::is_integral<Functor>::value)>::value), \
+ Type>::type
#endif
-template<typename Signature, typename Allocator>
-inline void swap(function<Signature, Allocator>& f1,
- function<Signature, Allocator>& f2)
-{
- f1.swap(f2);
-}
-
-} // end namespace boost
-#endif // have partial specialization
-
namespace boost {
namespace detail {
namespace function {
+ class X;
+
/**
- * A union of a function pointer and a void pointer. This is necessary
- * because 5.2.10/6 allows reinterpret_cast<> to safely cast between
- * function pointer types and 5.2.9/10 allows static_cast<> to safely
- * cast between a void pointer and an object pointer. But it is not legal
- * to cast between a function pointer and a void* (in either direction),
- * so function requires a union of the two. */
- union any_pointer
+ * A buffer used to store small function objects in
+ * boost::function. It is a union containing function pointers,
+ * object pointers, and a structure that resembles a bound
+ * member function pointer.
+ */
+ union function_buffer
{
- void* obj_ptr;
- const void* const_obj_ptr;
- void (*func_ptr)();
- char data[1];
+ // For pointers to function objects
+ mutable void* obj_ptr;
+
+ // For pointers to std::type_info objects
+ struct type_t {
+ // (get_functor_type_tag, check_functor_type_tag).
+ const detail::sp_typeinfo* type;
+
+ // Whether the type is const-qualified.
+ bool const_qualified;
+ // Whether the type is volatile-qualified.
+ bool volatile_qualified;
+ } type;
+
+ // For function pointers of all kinds
+ mutable void (*func_ptr)();
+
+ // For bound member pointers
+ struct bound_memfunc_ptr_t {
+ void (X::*memfunc_ptr)(int);
+ void* obj_ptr;
+ } bound_memfunc_ptr;
+
+ // For references to function objects. We explicitly keep
+ // track of the cv-qualifiers on the object referenced.
+ struct obj_ref_t {
+ mutable void* obj_ptr;
+ bool is_const_qualified;
+ bool is_volatile_qualified;
+ } obj_ref;
+
+ // To relax aliasing constraints
+ mutable char data;
};
- inline any_pointer make_any_pointer(void* o)
- {
- any_pointer p;
- p.obj_ptr = o;
- return p;
- }
-
- inline any_pointer make_any_pointer(const void* o)
- {
- any_pointer p;
- p.const_obj_ptr = o;
- return p;
- }
-
- inline any_pointer make_any_pointer(void (*f)())
- {
- any_pointer p;
- p.func_ptr = f;
- return p;
- }
-
/**
* The unusable class is a placeholder for unused function arguments
* It is also completely unusable except that it constructable from
// The operation type to perform on the given functor/function pointer
enum functor_manager_operation_type {
clone_functor_tag,
+ move_functor_tag,
destroy_functor_tag,
- check_functor_type_tag
+ check_functor_type_tag,
+ get_functor_type_tag
};
// Tags used to decide between different types of functions
struct function_obj_tag {};
struct member_ptr_tag {};
struct function_obj_ref_tag {};
- struct stateless_function_obj_tag {};
template<typename F>
class get_function_tag
{
- typedef typename ct_if<(is_pointer<F>::value),
- function_ptr_tag,
- function_obj_tag>::type ptr_or_obj_tag;
+ typedef typename mpl::if_c<(is_pointer<F>::value),
+ function_ptr_tag,
+ function_obj_tag>::type ptr_or_obj_tag;
- typedef typename ct_if<(is_member_pointer<F>::value),
- member_ptr_tag,
- ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag;
+ typedef typename mpl::if_c<(is_member_pointer<F>::value),
+ member_ptr_tag,
+ ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag;
- typedef typename ct_if<(is_reference_wrapper<F>::value),
- function_obj_ref_tag,
- ptr_or_obj_or_mem_tag>::type or_ref_tag;
+ typedef typename mpl::if_c<(is_reference_wrapper<F>::value),
+ function_obj_ref_tag,
+ ptr_or_obj_or_mem_tag>::type or_ref_tag;
public:
- typedef typename ct_if<(is_stateless<F>::value),
- stateless_function_obj_tag,
- or_ref_tag>::type type;
+ typedef or_ref_tag type;
};
// The trivial manager does nothing but return the same pointer (if we
// are cloning) or return the null pointer (if we are deleting).
template<typename F>
- struct trivial_manager
+ struct reference_manager
{
- static inline any_pointer
- get(any_pointer f, functor_manager_operation_type op)
+ static inline void
+ manage(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op)
{
switch (op) {
- case clone_functor_tag: return f;
+ case clone_functor_tag:
+ out_buffer.obj_ref = in_buffer.obj_ref;
+ return;
+
+ case move_functor_tag:
+ out_buffer.obj_ref = in_buffer.obj_ref;
+ in_buffer.obj_ref.obj_ptr = 0;
+ return;
case destroy_functor_tag:
- return make_any_pointer(reinterpret_cast<void*>(0));
+ out_buffer.obj_ref.obj_ptr = 0;
+ return;
case check_functor_type_tag:
{
- std::type_info* t = static_cast<std::type_info*>(f.obj_ptr);
- return BOOST_FUNCTION_COMPARE_TYPE_ID(typeid(F), *t)?
- f
- : make_any_pointer(reinterpret_cast<void*>(0));
+ const detail::sp_typeinfo& check_type
+ = *out_buffer.type.type;
+
+ // Check whether we have the same type. We can add
+ // cv-qualifiers, but we can't take them away.
+ if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, BOOST_SP_TYPEID(F))
+ && (!in_buffer.obj_ref.is_const_qualified
+ || out_buffer.type.const_qualified)
+ && (!in_buffer.obj_ref.is_volatile_qualified
+ || out_buffer.type.volatile_qualified))
+ out_buffer.obj_ptr = in_buffer.obj_ref.obj_ptr;
+ else
+ out_buffer.obj_ptr = 0;
}
+ return;
+
+ case get_functor_type_tag:
+ out_buffer.type.type = &BOOST_SP_TYPEID(F);
+ out_buffer.type.const_qualified = in_buffer.obj_ref.is_const_qualified;
+ out_buffer.type.volatile_qualified = in_buffer.obj_ref.is_volatile_qualified;
+ return;
}
+ }
+ };
+
+ /**
+ * Determine if boost::function can use the small-object
+ * optimization with the function object type F.
+ */
+ template<typename F>
+ struct function_allows_small_object_optimization
+ {
+ BOOST_STATIC_CONSTANT
+ (bool,
+ value = ((sizeof(F) <= sizeof(function_buffer) &&
+ (alignment_of<function_buffer>::value
+ % alignment_of<F>::value == 0))));
+ };
- // Clears up a warning with GCC 3.2.3
- return make_any_pointer(reinterpret_cast<void*>(0));
+ template <typename F,typename A>
+ struct functor_wrapper: public F, public A
+ {
+ functor_wrapper( F f, A a ):
+ F(f),
+ A(a)
+ {
+ }
+
+ functor_wrapper(const functor_wrapper& f) :
+ F(static_cast<const F&>(f)),
+ A(static_cast<const A&>(f))
+ {
}
};
* The functor_manager class contains a static function "manage" which
* can clone or destroy the given function/function object pointer.
*/
- template<typename Functor, typename Allocator>
- struct functor_manager
+ template<typename Functor>
+ struct functor_manager_common
{
- private:
typedef Functor functor_type;
- // For function pointers, the manager is trivial
- static inline any_pointer
- manager(any_pointer function_ptr,
- functor_manager_operation_type op,
- function_ptr_tag)
+ // Function pointers
+ static inline void
+ manage_ptr(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op)
{
if (op == clone_functor_tag)
- return function_ptr;
- else
- return make_any_pointer(static_cast<void (*)()>(0));
+ out_buffer.func_ptr = in_buffer.func_ptr;
+ else if (op == move_functor_tag) {
+ out_buffer.func_ptr = in_buffer.func_ptr;
+ in_buffer.func_ptr = 0;
+ } else if (op == destroy_functor_tag)
+ out_buffer.func_ptr = 0;
+ else if (op == check_functor_type_tag) {
+ const detail::sp_typeinfo& check_type
+ = *out_buffer.type.type;
+ if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, BOOST_SP_TYPEID(Functor)))
+ out_buffer.obj_ptr = &in_buffer.func_ptr;
+ else
+ out_buffer.obj_ptr = 0;
+ } else /* op == get_functor_type_tag */ {
+ out_buffer.type.type = &BOOST_SP_TYPEID(Functor);
+ out_buffer.type.const_qualified = false;
+ out_buffer.type.volatile_qualified = false;
+ }
}
- // For function object pointers, we clone the pointer to each
- // function has its own version.
- static inline any_pointer
- manager(any_pointer function_obj_ptr,
- functor_manager_operation_type op,
- function_obj_tag)
+ // Function objects that fit in the small-object buffer.
+ static inline void
+ manage_small(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op)
{
-#ifndef BOOST_NO_STD_ALLOCATOR
- typedef typename Allocator::template rebind<functor_type>::other
- allocator_type;
- typedef typename allocator_type::pointer pointer_type;
-#else
- typedef functor_type* pointer_type;
-#endif // BOOST_NO_STD_ALLOCATOR
+ if (op == clone_functor_tag || op == move_functor_tag) {
+ const functor_type* in_functor =
+ reinterpret_cast<const functor_type*>(&in_buffer.data);
+ new (reinterpret_cast<void*>(&out_buffer.data)) functor_type(*in_functor);
+
+ if (op == move_functor_tag) {
+ functor_type* f = reinterpret_cast<functor_type*>(&in_buffer.data);
+ (void)f; // suppress warning about the value of f not being used (MSVC)
+ f->~Functor();
+ }
+ } else if (op == destroy_functor_tag) {
+ // Some compilers (Borland, vc6, ...) are unhappy with ~functor_type.
+ functor_type* f = reinterpret_cast<functor_type*>(&out_buffer.data);
+ (void)f; // suppress warning about the value of f not being used (MSVC)
+ f->~Functor();
+ } else if (op == check_functor_type_tag) {
+ const detail::sp_typeinfo& check_type
+ = *out_buffer.type.type;
+ if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, BOOST_SP_TYPEID(Functor)))
+ out_buffer.obj_ptr = &in_buffer.data;
+ else
+ out_buffer.obj_ptr = 0;
+ } else /* op == get_functor_type_tag */ {
+ out_buffer.type.type = &BOOST_SP_TYPEID(Functor);
+ out_buffer.type.const_qualified = false;
+ out_buffer.type.volatile_qualified = false;
+ }
+ }
+ };
+
+ template<typename Functor>
+ struct functor_manager
+ {
+ private:
+ typedef Functor functor_type;
-# ifndef BOOST_NO_STD_ALLOCATOR
- allocator_type allocator;
-# endif // BOOST_NO_STD_ALLOCATOR
+ // Function pointers
+ static inline void
+ manager(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op, function_ptr_tag)
+ {
+ functor_manager_common<Functor>::manage_ptr(in_buffer,out_buffer,op);
+ }
+ // Function objects that fit in the small-object buffer.
+ static inline void
+ manager(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op, mpl::true_)
+ {
+ functor_manager_common<Functor>::manage_small(in_buffer,out_buffer,op);
+ }
+
+ // Function objects that require heap allocation
+ static inline void
+ manager(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op, mpl::false_)
+ {
if (op == clone_functor_tag) {
- functor_type* f =
- static_cast<functor_type*>(function_obj_ptr.obj_ptr);
-
// Clone the functor
-# ifndef BOOST_NO_STD_ALLOCATOR
- pointer_type copy = allocator.allocate(1);
- allocator.construct(copy, *f);
-
- // Get back to the original pointer type
- functor_type* new_f = static_cast<functor_type*>(copy);
-# else
+ // GCC 2.95.3 gets the CV qualifiers wrong here, so we
+ // can't do the static_cast that we should do.
+ // jewillco: Changing this to static_cast because GCC 2.95.3 is
+ // obsolete.
+ const functor_type* f =
+ static_cast<const functor_type*>(in_buffer.obj_ptr);
functor_type* new_f = new functor_type(*f);
-# endif // BOOST_NO_STD_ALLOCATOR
- return make_any_pointer(static_cast<void*>(new_f));
- }
- else {
+ out_buffer.obj_ptr = new_f;
+ } else if (op == move_functor_tag) {
+ out_buffer.obj_ptr = in_buffer.obj_ptr;
+ in_buffer.obj_ptr = 0;
+ } else if (op == destroy_functor_tag) {
/* Cast from the void pointer to the functor pointer type */
functor_type* f =
- reinterpret_cast<functor_type*>(function_obj_ptr.obj_ptr);
+ static_cast<functor_type*>(out_buffer.obj_ptr);
+ delete f;
+ out_buffer.obj_ptr = 0;
+ } else if (op == check_functor_type_tag) {
+ const detail::sp_typeinfo& check_type
+ = *out_buffer.type.type;
+ if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, BOOST_SP_TYPEID(Functor)))
+ out_buffer.obj_ptr = in_buffer.obj_ptr;
+ else
+ out_buffer.obj_ptr = 0;
+ } else /* op == get_functor_type_tag */ {
+ out_buffer.type.type = &BOOST_SP_TYPEID(Functor);
+ out_buffer.type.const_qualified = false;
+ out_buffer.type.volatile_qualified = false;
+ }
+ }
-# ifndef BOOST_NO_STD_ALLOCATOR
- /* Cast from the functor pointer type to the allocator's pointer
- type */
- pointer_type victim = static_cast<pointer_type>(f);
+ // For function objects, we determine whether the function
+ // object can use the small-object optimization buffer or
+ // whether we need to allocate it on the heap.
+ static inline void
+ manager(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op, function_obj_tag)
+ {
+ manager(in_buffer, out_buffer, op,
+ mpl::bool_<(function_allows_small_object_optimization<functor_type>::value)>());
+ }
- // Destroy and deallocate the functor
- allocator.destroy(victim);
- allocator.deallocate(victim, 1);
-# else
- delete f;
-# endif // BOOST_NO_STD_ALLOCATOR
+ // For member pointers, we use the small-object optimization buffer.
+ static inline void
+ manager(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op, member_ptr_tag)
+ {
+ manager(in_buffer, out_buffer, op, mpl::true_());
+ }
- return make_any_pointer(static_cast<void*>(0));
+ public:
+ /* Dispatch to an appropriate manager based on whether we have a
+ function pointer or a function object pointer. */
+ static inline void
+ manage(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op)
+ {
+ typedef typename get_function_tag<functor_type>::type tag_type;
+ switch (op) {
+ case get_functor_type_tag:
+ out_buffer.type.type = &BOOST_SP_TYPEID(functor_type);
+ out_buffer.type.const_qualified = false;
+ out_buffer.type.volatile_qualified = false;
+ return;
+
+ default:
+ manager(in_buffer, out_buffer, op, tag_type());
+ return;
}
}
+ };
+
+ template<typename Functor, typename Allocator>
+ struct functor_manager_a
+ {
+ private:
+ typedef Functor functor_type;
+
+ // Function pointers
+ static inline void
+ manager(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op, function_ptr_tag)
+ {
+ functor_manager_common<Functor>::manage_ptr(in_buffer,out_buffer,op);
+ }
+
+ // Function objects that fit in the small-object buffer.
+ static inline void
+ manager(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op, mpl::true_)
+ {
+ functor_manager_common<Functor>::manage_small(in_buffer,out_buffer,op);
+ }
+
+ // Function objects that require heap allocation
+ static inline void
+ manager(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op, mpl::false_)
+ {
+ typedef functor_wrapper<Functor,Allocator> functor_wrapper_type;
+ typedef typename Allocator::template rebind<functor_wrapper_type>::other
+ wrapper_allocator_type;
+ typedef typename wrapper_allocator_type::pointer wrapper_allocator_pointer_type;
+
+ if (op == clone_functor_tag) {
+ // Clone the functor
+ // GCC 2.95.3 gets the CV qualifiers wrong here, so we
+ // can't do the static_cast that we should do.
+ const functor_wrapper_type* f =
+ static_cast<const functor_wrapper_type*>(in_buffer.obj_ptr);
+ wrapper_allocator_type wrapper_allocator(static_cast<Allocator const &>(*f));
+ wrapper_allocator_pointer_type copy = wrapper_allocator.allocate(1);
+ wrapper_allocator.construct(copy, *f);
+
+ // Get back to the original pointer type
+ functor_wrapper_type* new_f = static_cast<functor_wrapper_type*>(copy);
+ out_buffer.obj_ptr = new_f;
+ } else if (op == move_functor_tag) {
+ out_buffer.obj_ptr = in_buffer.obj_ptr;
+ in_buffer.obj_ptr = 0;
+ } else if (op == destroy_functor_tag) {
+ /* Cast from the void pointer to the functor_wrapper_type */
+ functor_wrapper_type* victim =
+ static_cast<functor_wrapper_type*>(in_buffer.obj_ptr);
+ wrapper_allocator_type wrapper_allocator(static_cast<Allocator const &>(*victim));
+ wrapper_allocator.destroy(victim);
+ wrapper_allocator.deallocate(victim,1);
+ out_buffer.obj_ptr = 0;
+ } else if (op == check_functor_type_tag) {
+ const detail::sp_typeinfo& check_type
+ = *out_buffer.type.type;
+ if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, BOOST_SP_TYPEID(Functor)))
+ out_buffer.obj_ptr = in_buffer.obj_ptr;
+ else
+ out_buffer.obj_ptr = 0;
+ } else /* op == get_functor_type_tag */ {
+ out_buffer.type.type = &BOOST_SP_TYPEID(Functor);
+ out_buffer.type.const_qualified = false;
+ out_buffer.type.volatile_qualified = false;
+ }
+ }
+
+ // For function objects, we determine whether the function
+ // object can use the small-object optimization buffer or
+ // whether we need to allocate it on the heap.
+ static inline void
+ manager(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op, function_obj_tag)
+ {
+ manager(in_buffer, out_buffer, op,
+ mpl::bool_<(function_allows_small_object_optimization<functor_type>::value)>());
+ }
+
public:
/* Dispatch to an appropriate manager based on whether we have a
function pointer or a function object pointer. */
- static any_pointer
- manage(any_pointer functor_ptr, functor_manager_operation_type op)
+ static inline void
+ manage(const function_buffer& in_buffer, function_buffer& out_buffer,
+ functor_manager_operation_type op)
{
- if (op == check_functor_type_tag) {
- std::type_info* type =
- static_cast<std::type_info*>(functor_ptr.obj_ptr);
- return (BOOST_FUNCTION_COMPARE_TYPE_ID(typeid(Functor), *type)?
- functor_ptr
- : make_any_pointer(reinterpret_cast<void*>(0)));
- }
- else {
- typedef typename get_function_tag<functor_type>::type tag_type;
- return manager(functor_ptr, op, tag_type());
+ typedef typename get_function_tag<functor_type>::type tag_type;
+ switch (op) {
+ case get_functor_type_tag:
+ out_buffer.type.type = &BOOST_SP_TYPEID(functor_type);
+ out_buffer.type.const_qualified = false;
+ out_buffer.type.volatile_qualified = false;
+ return;
+
+ default:
+ manager(in_buffer, out_buffer, op, tag_type());
+ return;
}
}
};
else return true;
}
#endif // BOOST_NO_SFINAE
+
+ /**
+ * Stores the "manager" portion of the vtable for a
+ * boost::function object.
+ */
+ struct vtable_base
+ {
+ void (*manager)(const function_buffer& in_buffer,
+ function_buffer& out_buffer,
+ functor_manager_operation_type op);
+ };
} // end namespace function
} // end namespace detail
class function_base
{
public:
- function_base() : manager(0)
+ function_base() : vtable(0) { }
+
+ /** Determine if the function is empty (i.e., has no target). */
+ bool empty() const { return !vtable; }
+
+ /** Retrieve the type of the stored function object, or BOOST_SP_TYPEID(void)
+ if this is empty. */
+ const detail::sp_typeinfo& target_type() const
{
- functor.obj_ptr = 0;
- }
+ if (!vtable) return BOOST_SP_TYPEID(void);
- // Is this function empty?
- bool empty() const { return !manager; }
+ detail::function::function_buffer type;
+ get_vtable()->manager(functor, type, detail::function::get_functor_type_tag);
+ return *type.type.type;
+ }
template<typename Functor>
Functor* target()
{
- if (!manager) return 0;
-
- detail::function::any_pointer result =
- manager(detail::function::make_any_pointer(&typeid(Functor)),
- detail::function::check_functor_type_tag);
- if (!result.obj_ptr) return 0;
- else {
- typedef typename detail::function::get_function_tag<Functor>::type tag;
- return get_functor_pointer<Functor>(tag(), 0);
- }
+ if (!vtable) return 0;
+
+ detail::function::function_buffer type_result;
+ type_result.type.type = &BOOST_SP_TYPEID(Functor);
+ type_result.type.const_qualified = is_const<Functor>::value;
+ type_result.type.volatile_qualified = is_volatile<Functor>::value;
+ get_vtable()->manager(functor, type_result,
+ detail::function::check_functor_type_tag);
+ return static_cast<Functor*>(type_result.obj_ptr);
}
template<typename Functor>
+#if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
+ const Functor* target( Functor * = 0 ) const
+#else
const Functor* target() const
+#endif
{
- if (!manager) return 0;
-
- detail::function::any_pointer result =
- manager(detail::function::make_any_pointer(&typeid(Functor)),
- detail::function::check_functor_type_tag);
- if (!result.obj_ptr) return 0;
- else {
- typedef typename detail::function::get_function_tag<Functor>::type tag;
- return get_functor_pointer<Functor>(tag(), 0);
- }
+ if (!vtable) return 0;
+
+ detail::function::function_buffer type_result;
+ type_result.type.type = &BOOST_SP_TYPEID(Functor);
+ type_result.type.const_qualified = true;
+ type_result.type.volatile_qualified = is_volatile<Functor>::value;
+ get_vtable()->manager(functor, type_result,
+ detail::function::check_functor_type_tag);
+ // GCC 2.95.3 gets the CV qualifiers wrong here, so we
+ // can't do the static_cast that we should do.
+ return static_cast<const Functor*>(type_result.obj_ptr);
}
template<typename F>
bool contains(const F& f) const
{
- if (const F* fp = this->template target<F>()) {
+#if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
+ if (const F* fp = this->target( (F*)0 ))
+#else
+ if (const F* fp = this->template target<F>())
+#endif
+ {
return function_equal(*fp, f);
} else {
return false;
#endif
public: // should be protected, but GCC 2.95.3 will fail to allow access
- detail::function::any_pointer (*manager)(
- detail::function::any_pointer,
- detail::function::functor_manager_operation_type);
- detail::function::any_pointer functor;
-
-private:
- template<typename Functor>
- Functor* get_functor_pointer(detail::function::function_ptr_tag, int)
- { return reinterpret_cast<Functor*>(&functor.func_ptr); }
-
- template<typename Functor, typename Tag>
- Functor* get_functor_pointer(Tag, long)
- { return static_cast<Functor*>(functor.obj_ptr); }
+ detail::function::vtable_base* get_vtable() const {
+ return reinterpret_cast<detail::function::vtable_base*>(
+ reinterpret_cast<std::size_t>(vtable) & ~static_cast<std::size_t>(0x01));
+ }
- template<typename Functor>
- const Functor*
- get_functor_pointer(detail::function::function_ptr_tag, int) const
- { return reinterpret_cast<const Functor*>(&functor.func_ptr); }
+ bool has_trivial_copy_and_destroy() const {
+ return reinterpret_cast<std::size_t>(vtable) & 0x01;
+ }
- template<typename Functor, typename Tag>
- const Functor* get_functor_pointer(Tag, long) const
- { return static_cast<const Functor*>(functor.const_obj_ptr); }
+ detail::function::vtable_base* vtable;
+ mutable detail::function::function_buffer functor;
};
/**
#undef BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL
#undef BOOST_FUNCTION_COMPARE_TYPE_ID
+#if defined(BOOST_MSVC)
+# pragma warning( pop )
+#endif
+
#endif // BOOST_FUNCTION_BASE_HEADER