Small buffer optimization for Boost.Function

[SVN r32282]
This commit is contained in:
Douglas Gregor
2006-01-10 23:52:35 +00:00
parent 93c691fbdf
commit 78f6b385d5
6 changed files with 347 additions and 283 deletions

View File

@ -5,6 +5,13 @@
<title>History &amp; Compatibility Notes</title> <title>History &amp; Compatibility Notes</title>
<itemizedlist spacing="compact"> <itemizedlist spacing="compact">
<listitem><para><bold>Version 1.34.0</bold>: </para>
<itemizedlist spacing="compact">
<listitem><para>Boost.Function now implements a small buffer optimization, which can drastically improve the performance when copying or construction Boost.Function objects storing small function objects. For instance, <code>bind(&amp;X:foo, &amp;x, _1, _2)</code> requires no heap allocation when placed into a Boost.Function object. Note that some exception-safety guarantees have changed: assignment provides the basic exception guarantee and <code>swap()</code> may throw.</para></listitem>
</itemizedlist>
</listitem>
<listitem><para><bold>Version 1.30.0</bold>: </para> <listitem><para><bold>Version 1.30.0</bold>: </para>
<itemizedlist spacing="compact"> <itemizedlist spacing="compact">
<listitem><para>All features deprecated in version 1.29.0 have <listitem><para>All features deprecated in version 1.29.0 have

View File

@ -118,6 +118,12 @@
<returns><simpara><code>true</code> if <code>this-&gt;<methodname>target</methodname>&lt;Functor&gt;()</code> is non-NULL and <code><functionname>function_equal</functionname>(*(this-&gt;target&lt;Functor&gt;()), f)</code></simpara></returns> <returns><simpara><code>true</code> if <code>this-&gt;<methodname>target</methodname>&lt;Functor&gt;()</code> is non-NULL and <code><functionname>function_equal</functionname>(*(this-&gt;target&lt;Functor&gt;()), f)</code></simpara></returns>
</method> </method>
<method name="target_type" cv="const">
<type>const std::type_info&amp;</type>
<returns><simpara><code>typeid</code> of the target function object, or <code>typeid(void)</code> if <code>this-&gt;<methodname>empty</methodname>()</code>.</simpara></returns>
<throws><simpara>Will not throw.</simpara></throws>
</method>
</method-group> </method-group>
</class> </class>
@ -182,7 +188,7 @@
</struct> </struct>
<constructor> <constructor>
<postconditions><simpara><code>this-><methodname>empty</methodname>()</code></simpara></postconditions> <postconditions><simpara><code>this-&gt;<methodname>empty</methodname>()</code></simpara></postconditions>
<throws><simpara>Will not throw.</simpara></throws> <throws><simpara>Will not throw.</simpara></throws>
</constructor> </constructor>
@ -201,11 +207,10 @@
<parameter name="f"><paramtype>F</paramtype></parameter> <parameter name="f"><paramtype>F</paramtype></parameter>
<requires><simpara>F is a function object Callable from <code>this</code>.</simpara></requires> <requires><simpara>F is a function object Callable from <code>this</code>.</simpara></requires>
<postconditions><simpara><code>*this</code> targets a copy of <code>f</code> if <code>f</code> is nonempty, or <code>this-&gt;<methodname>empty</methodname>()</code> if <code>f</code> is empty.</simpara></postconditions> <postconditions><simpara><code>*this</code> targets a copy of <code>f</code> if <code>f</code> is nonempty, or <code>this-&gt;<methodname>empty</methodname>()</code> if <code>f</code> is empty.</simpara></postconditions>
<throws><simpara>Will not throw when <code>f</code> is a stateless function object.</simpara></throws>
</constructor> </constructor>
<destructor> <destructor>
<effects><simpara>If <code>!this-><methodname>empty</methodname>()</code>, destroys the target of this.</simpara></effects> <effects><simpara>If <code>!this-&gt;<methodname>empty</methodname>()</code>, destroys the target of this.</simpara></effects>
</destructor> </destructor>
@ -213,8 +218,7 @@
<parameter name="f"> <parameter name="f">
<paramtype>const <classname>functionN</classname>&amp;</paramtype> <paramtype>const <classname>functionN</classname>&amp;</paramtype>
</parameter> </parameter>
<postconditions><simpara><code>*this</code> targets a copy of <code>f</code>'s target, if it has one, or is empty if <code>f.<methodname>empty</methodname>()</code>.</simpara></postconditions> <postconditions><simpara>If copy construction does not throw, <code>*this</code> targets a copy of <code>f</code>'s target, if it has one, or is empty if <code>f.<methodname>empty</methodname>()</code>. If copy construction does throw, <code>this-&gt;<methodname>empty</methodname>()</code>.</simpara></postconditions>
<throws><simpara>Will not throw when the target of <code>f</code> is a stateless function object or a reference to the function object.</simpara></throws>
</copy-assignment> </copy-assignment>
<method-group name="modifiers"> <method-group name="modifiers">
@ -222,13 +226,11 @@
<type>void</type> <type>void</type>
<parameter name="f"><paramtype>const <classname>functionN</classname>&amp;</paramtype></parameter> <parameter name="f"><paramtype>const <classname>functionN</classname>&amp;</paramtype></parameter>
<effects><simpara>Interchanges the targets of <code>*this</code> and <code>f</code>.</simpara></effects> <effects><simpara>Interchanges the targets of <code>*this</code> and <code>f</code>.</simpara></effects>
<throws><simpara>Will not throw.</simpara></throws>
</method> </method>
<method name="clear"> <method name="clear">
<type>void</type> <type>void</type>
<postconditions><simpara>this-&gt;<methodname>empty</methodname>()</simpara></postconditions> <postconditions><simpara>this-&gt;<methodname>empty</methodname>()</simpara></postconditions>
<throws><simpara>Will not throw.</simpara></throws>
</method> </method>
</method-group> </method-group>
@ -286,6 +288,13 @@
<returns><simpara><code>true</code> if <code>this-&gt;<methodname>target</methodname>&lt;Functor&gt;()</code> is non-NULL and <code><functionname>function_equal</functionname>(*(this-&gt;target&lt;Functor&gt;()), f)</code></simpara></returns> <returns><simpara><code>true</code> if <code>this-&gt;<methodname>target</methodname>&lt;Functor&gt;()</code> is non-NULL and <code><functionname>function_equal</functionname>(*(this-&gt;target&lt;Functor&gt;()), f)</code></simpara></returns>
</method> </method>
<method name="target_type" cv="const">
<type>const std::type_info&amp;</type>
<returns><simpara><code>typeid</code> of the target function object, or <code>typeid(void)</code> if <code>this-&gt;<methodname>empty</methodname>()</code>.</simpara></returns>
<throws><simpara>Will not throw.</simpara></throws>
</method>
</method-group> </method-group>
<method-group name="invocation"> <method-group name="invocation">
@ -314,7 +323,6 @@
<parameter name="f1"><paramtype><classname>functionN</classname>&lt;T1, T2, ..., TN, Allocator&gt;&amp;</paramtype></parameter> <parameter name="f1"><paramtype><classname>functionN</classname>&lt;T1, T2, ..., TN, Allocator&gt;&amp;</paramtype></parameter>
<parameter name="f2"><paramtype><classname>functionN</classname>&lt;T1, T2, ..., TN, Allocator&gt;&amp;</paramtype></parameter> <parameter name="f2"><paramtype><classname>functionN</classname>&lt;T1, T2, ..., TN, Allocator&gt;&amp;</paramtype></parameter>
<effects><simpara><code>f1.<methodname>swap</methodname>(f2)</code></simpara></effects> <effects><simpara><code>f1.<methodname>swap</methodname>(f2)</code></simpara></effects>
<throws><simpara>Will not throw.</simpara></throws>
</function> </function>
</free-function-group> </free-function-group>
@ -589,7 +597,7 @@
</struct> </struct>
<constructor> <constructor>
<postconditions><simpara><code>this-><methodname>empty</methodname>()</code></simpara></postconditions> <postconditions><simpara><code>this-&gt;<methodname>empty</methodname>()</code></simpara></postconditions>
<throws><simpara>Will not throw.</simpara></throws> <throws><simpara>Will not throw.</simpara></throws>
</constructor> </constructor>
@ -616,28 +624,26 @@
<parameter name="f"><paramtype>F</paramtype></parameter> <parameter name="f"><paramtype>F</paramtype></parameter>
<requires><simpara>F is a function object Callable from <code>this</code>.</simpara></requires> <requires><simpara>F is a function object Callable from <code>this</code>.</simpara></requires>
<postconditions><simpara><code>*this</code> targets a copy of <code>f</code> if <code>f</code> is nonempty, or <code>this-&gt;<methodname>empty</methodname>()</code> if <code>f</code> is empty.</simpara></postconditions> <postconditions><simpara><code>*this</code> targets a copy of <code>f</code> if <code>f</code> is nonempty, or <code>this-&gt;<methodname>empty</methodname>()</code> if <code>f</code> is empty.</simpara></postconditions>
<throws><simpara>Will not throw when <code>f</code> is a stateless function object.</simpara></throws>
</constructor> </constructor>
<destructor> <destructor>
<effects><simpara>If <code>!this-><methodname>empty</methodname>()</code>, destroys the target of <code>this</code>.</simpara></effects> <effects><simpara>If <code>!this-&gt;<methodname>empty</methodname>()</code>, destroys the target of <code>this</code>.</simpara></effects>
</destructor> </destructor>
<copy-assignment> <copy-assignment>
<parameter name="f"> <parameter name="f">
<paramtype>const <classname>functionN</classname>&amp;</paramtype> <paramtype>const <classname>function</classname>&amp;</paramtype>
</parameter> </parameter>
<postconditions><simpara><code>*this</code> targets a copy of <code>f</code>'s target, if it has one, or is empty if <code>f.<methodname>empty</methodname>()</code></simpara></postconditions> <postconditions><simpara>If copy construction does not throw, <code>*this</code> targets a copy of <code>f</code>'s target, if it has one, or is empty if <code>f.<methodname>empty</methodname>()</code>. If copy construction does throw, <code>this-&gt;<methodname>empty</methodname>()</code>.</simpara></postconditions>
<throws><simpara>Will not throw when the target of <code>f</code> is a stateless function object or a reference to the function object.</simpara></throws>
</copy-assignment> </copy-assignment>
<copy-assignment> <copy-assignment>
<parameter name="f"> <parameter name="f">
<paramtype>const <classname>function</classname>&amp;</paramtype> <paramtype>const <classname>function</classname>&amp;</paramtype>
</parameter> </parameter>
<postconditions><simpara><code>*this</code> targets a copy of <code>f</code>'s target, if it has one, or is empty if <code>f.<methodname>empty</methodname>()</code></simpara></postconditions> <postconditions><simpara>If copy construction of the target of <code>f</code> does not throw, <code>*this</code> targets a copy of <code>f</code>'s target, if it has one, or is empty if <code>f.<methodname>empty</methodname>()</code>. </simpara></postconditions>
<throws><simpara>Will not throw when the target of <code>f</code> is a stateless function object or a reference to the function object.</simpara></throws> <throws><simpara>Will not throw when the target of <code>f</code> is a stateless function object or a reference to the function object. If copy construction does throw, <code>this-&gt;<methodname>empty</methodname>()</code>.</simpara></throws>
</copy-assignment> </copy-assignment>
<method-group name="modifiers"> <method-group name="modifiers">
@ -645,7 +651,6 @@
<type>void</type> <type>void</type>
<parameter name="f"><paramtype>const <classname>function</classname>&amp;</paramtype></parameter> <parameter name="f"><paramtype>const <classname>function</classname>&amp;</paramtype></parameter>
<effects><simpara>Interchanges the targets of <code>*this</code> and <code>f</code>.</simpara></effects> <effects><simpara>Interchanges the targets of <code>*this</code> and <code>f</code>.</simpara></effects>
<throws><simpara>Will not throw.</simpara></throws>
</method> </method>
<method name="clear"> <method name="clear">
@ -708,6 +713,12 @@
<returns><simpara><code>true</code> if <code>this-&gt;<methodname>target</methodname>&lt;Functor&gt;()</code> is non-NULL and <code><functionname>function_equal</functionname>(*(this-&gt;target&lt;Functor&gt;()), f)</code></simpara></returns> <returns><simpara><code>true</code> if <code>this-&gt;<methodname>target</methodname>&lt;Functor&gt;()</code> is non-NULL and <code><functionname>function_equal</functionname>(*(this-&gt;target&lt;Functor&gt;()), f)</code></simpara></returns>
</method> </method>
<method name="target_type" cv="const">
<type>const std::type_info&amp;</type>
<returns><simpara><code>typeid</code> of the target function object, or <code>typeid(void)</code> if <code>this-&gt;<methodname>empty</methodname>()</code>.</simpara></returns>
<throws><simpara>Will not throw.</simpara></throws>
</method>
</method-group> </method-group>
<method-group name="invocation"> <method-group name="invocation">
@ -733,7 +744,6 @@
<parameter name="f1"><paramtype><classname>function</classname>&lt;Signature, Allocator&gt;&amp;</paramtype></parameter> <parameter name="f1"><paramtype><classname>function</classname>&lt;Signature, Allocator&gt;&amp;</paramtype></parameter>
<parameter name="f2"><paramtype><classname>function</classname>&lt;Signature, Allocator&gt;&amp;</paramtype></parameter> <parameter name="f2"><paramtype><classname>function</classname>&lt;Signature, Allocator&gt;&amp;</paramtype></parameter>
<effects><simpara><code>f1.<methodname>swap</methodname>(f2)</code></simpara></effects> <effects><simpara><code>f1.<methodname>swap</methodname>(f2)</code></simpara></effects>
<throws><simpara>Will not throw.</simpara></throws>
</function> </function>
</free-function-group> </free-function-group>

View File

@ -1,6 +1,6 @@
// Boost.Function library // Boost.Function library
// Copyright Douglas Gregor 2001-2004. Use, modification and // Copyright Douglas Gregor 2001-2006. Use, modification and
// distribution is subject to the Boost Software License, Version // distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
@ -19,10 +19,10 @@
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <boost/type_traits/is_integral.hpp> #include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/composite_traits.hpp> #include <boost/type_traits/composite_traits.hpp>
#include <boost/type_traits/is_stateless.hpp>
#include <boost/ref.hpp> #include <boost/ref.hpp>
#include <boost/pending/ct_if.hpp> #include <boost/mpl/if.hpp>
#include <boost/detail/workaround.hpp> #include <boost/detail/workaround.hpp>
#include <boost/type_traits/alignment_of.hpp>
#ifndef BOOST_NO_SFINAE #ifndef BOOST_NO_SFINAE
# include "boost/utility/enable_if.hpp" # include "boost/utility/enable_if.hpp"
#else #else
@ -105,42 +105,36 @@ inline void swap(function<Signature, Allocator>& f1,
namespace boost { namespace boost {
namespace detail { namespace detail {
namespace function { namespace function {
class X;
/** /**
* A union of a function pointer and a void pointer. This is necessary * A buffer used to store small function objects in
* because 5.2.10/6 allows reinterpret_cast<> to safely cast between * boost::function. It is a union containing function pointers,
* function pointer types and 5.2.9/10 allows static_cast<> to safely * object pointers, and a structure that resembles a bound
* cast between a void pointer and an object pointer. But it is not legal * member function pointer.
* to cast between a function pointer and a void* (in either direction), */
* so function requires a union of the two. */ union function_buffer
union any_pointer
{ {
// For pointers to function objects
void* obj_ptr; void* obj_ptr;
// For pointers to std::type_info objects
// (get_functor_type_tag, check_functor_type_tag).
const void* const_obj_ptr; const void* const_obj_ptr;
void (*func_ptr)();
char data[1]; // 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;
// 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 * The unusable class is a placeholder for unused function arguments
* It is also completely unusable except that it constructable from * It is also completely unusable except that it constructable from
@ -169,7 +163,8 @@ namespace boost {
enum functor_manager_operation_type { enum functor_manager_operation_type {
clone_functor_tag, clone_functor_tag,
destroy_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 // Tags used to decide between different types of functions
@ -177,57 +172,79 @@ namespace boost {
struct function_obj_tag {}; struct function_obj_tag {};
struct member_ptr_tag {}; struct member_ptr_tag {};
struct function_obj_ref_tag {}; struct function_obj_ref_tag {};
struct stateless_function_obj_tag {};
template<typename F> template<typename F>
class get_function_tag class get_function_tag
{ {
typedef typename ct_if<(is_pointer<F>::value), typedef typename mpl::if_c<(is_pointer<F>::value),
function_ptr_tag, function_ptr_tag,
function_obj_tag>::type ptr_or_obj_tag; function_obj_tag>::type ptr_or_obj_tag;
typedef typename ct_if<(is_member_pointer<F>::value), typedef typename mpl::if_c<(is_member_pointer<F>::value),
member_ptr_tag, member_ptr_tag,
ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag; ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag;
typedef typename ct_if<(is_reference_wrapper<F>::value), typedef typename mpl::if_c<(is_reference_wrapper<F>::value),
function_obj_ref_tag, function_obj_ref_tag,
ptr_or_obj_or_mem_tag>::type or_ref_tag; ptr_or_obj_or_mem_tag>::type or_ref_tag;
public: public:
typedef typename ct_if<(is_stateless<F>::value), typedef or_ref_tag type;
stateless_function_obj_tag,
or_ref_tag>::type type;
}; };
// The trivial manager does nothing but return the same pointer (if we // The trivial manager does nothing but return the same pointer (if we
// are cloning) or return the null pointer (if we are deleting). // are cloning) or return the null pointer (if we are deleting).
template<typename F> template<typename F>
struct trivial_manager struct reference_manager
{ {
static inline any_pointer static inline void
get(any_pointer f, functor_manager_operation_type op) get(const function_buffer& in_buffer, function_buffer& out_buffer,
functor_manager_operation_type op)
{ {
switch (op) { switch (op) {
case clone_functor_tag: return f; case clone_functor_tag:
out_buffer.obj_ptr = in_buffer.obj_ptr;
return;
case destroy_functor_tag: case destroy_functor_tag:
return make_any_pointer(reinterpret_cast<void*>(0)); out_buffer.obj_ptr = 0;
return;
case check_functor_type_tag: case check_functor_type_tag:
{ {
std::type_info* t = static_cast<std::type_info*>(f.obj_ptr); // DPG TBD: Since we're only storing a pointer, it's
return BOOST_FUNCTION_COMPARE_TYPE_ID(typeid(F), *t)? // possible that the user could ask for a base class or
f // derived class. Is that okay?
: make_any_pointer(reinterpret_cast<void*>(0)); const std::type_info& check_type =
*static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(F)))
out_buffer.obj_ptr = in_buffer.obj_ptr;
else
out_buffer.obj_ptr = 0;
} }
} return;
// Clears up a warning with GCC 3.2.3 case get_functor_type_tag:
return make_any_pointer(reinterpret_cast<void*>(0)); out_buffer.const_obj_ptr = &typeid(F);
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))));
};
/** /**
* The functor_manager class contains a static function "manage" which * The functor_manager class contains a static function "manage" which
* can clone or destroy the given function/function object pointer. * can clone or destroy the given function/function object pointer.
@ -239,30 +256,58 @@ namespace boost {
typedef Functor functor_type; typedef Functor functor_type;
// For function pointers, the manager is trivial // For function pointers, the manager is trivial
static inline any_pointer static inline void
manager(any_pointer function_ptr, manager(const function_buffer& in_buffer, function_buffer& out_buffer,
functor_manager_operation_type op, functor_manager_operation_type op, function_ptr_tag)
function_ptr_tag)
{ {
if (op == clone_functor_tag) if (op == clone_functor_tag)
return function_ptr; out_buffer.func_ptr = in_buffer.func_ptr;
else else if (op == destroy_functor_tag)
return make_any_pointer(static_cast<void (*)()>(0)); out_buffer.func_ptr = 0;
else /* op == check_functor_type_tag */ {
const std::type_info& check_type =
*static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(Functor)))
out_buffer.obj_ptr = &in_buffer.func_ptr;
else
out_buffer.obj_ptr = 0;
}
} }
// For function object pointers, we clone the pointer to each // Function objects that fit in the small-object buffer.
// function has its own version. static inline void
static inline any_pointer manager(const function_buffer& in_buffer, function_buffer& out_buffer,
manager(any_pointer function_obj_ptr, functor_manager_operation_type op, mpl::true_)
functor_manager_operation_type op, {
function_obj_tag) if (op == clone_functor_tag) {
const functor_type* in_functor =
reinterpret_cast<const functor_type*>(&in_buffer.data);
new ((void*)&out_buffer.data) functor_type(*in_functor);
} else if (op == destroy_functor_tag) {
functor_type* out_functor =
reinterpret_cast<functor_type*>(&out_buffer.data);
out_functor->~functor_type();
} else /* op == check_functor_type_tag */ {
const std::type_info& check_type =
*static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(Functor)))
out_buffer.obj_ptr = &in_buffer.data;
else
out_buffer.obj_ptr = 0;
}
}
// 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_)
{ {
#ifndef BOOST_NO_STD_ALLOCATOR #ifndef BOOST_NO_STD_ALLOCATOR
typedef typename Allocator::template rebind<functor_type>::other typedef typename Allocator::template rebind<functor_type>::other
allocator_type; allocator_type;
typedef typename allocator_type::pointer pointer_type; typedef typename allocator_type::pointer pointer_type;
#else #else
typedef functor_type* pointer_type; typedef functor_type* pointer_type;
#endif // BOOST_NO_STD_ALLOCATOR #endif // BOOST_NO_STD_ALLOCATOR
# ifndef BOOST_NO_STD_ALLOCATOR # ifndef BOOST_NO_STD_ALLOCATOR
@ -270,8 +315,8 @@ namespace boost {
# endif // BOOST_NO_STD_ALLOCATOR # endif // BOOST_NO_STD_ALLOCATOR
if (op == clone_functor_tag) { if (op == clone_functor_tag) {
functor_type* f = const functor_type* f =
static_cast<functor_type*>(function_obj_ptr.obj_ptr); static_cast<const functor_type*>(in_buffer.obj_ptr);
// Clone the functor // Clone the functor
# ifndef BOOST_NO_STD_ALLOCATOR # ifndef BOOST_NO_STD_ALLOCATOR
@ -283,12 +328,11 @@ namespace boost {
# else # else
functor_type* new_f = new functor_type(*f); functor_type* new_f = new functor_type(*f);
# endif // BOOST_NO_STD_ALLOCATOR # endif // BOOST_NO_STD_ALLOCATOR
return make_any_pointer(static_cast<void*>(new_f)); out_buffer.obj_ptr = new_f;
} } else if (op == destroy_functor_tag) {
else {
/* Cast from the void pointer to the functor pointer type */ /* Cast from the void pointer to the functor pointer type */
functor_type* f = functor_type* f =
reinterpret_cast<functor_type*>(function_obj_ptr.obj_ptr); static_cast<functor_type*>(out_buffer.obj_ptr);
# ifndef BOOST_NO_STD_ALLOCATOR # ifndef BOOST_NO_STD_ALLOCATOR
/* Cast from the functor pointer type to the allocator's pointer /* Cast from the functor pointer type to the allocator's pointer
@ -301,26 +345,43 @@ namespace boost {
# else # else
delete f; delete f;
# endif // BOOST_NO_STD_ALLOCATOR # endif // BOOST_NO_STD_ALLOCATOR
out_buffer.obj_ptr = 0;
return make_any_pointer(static_cast<void*>(0)); } else /* op == check_functor_type_tag */ {
const std::type_info& check_type =
*static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(Functor)))
out_buffer.obj_ptr = in_buffer.obj_ptr;
else
out_buffer.obj_ptr = 0;
} }
} }
// 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: public:
/* Dispatch to an appropriate manager based on whether we have a /* Dispatch to an appropriate manager based on whether we have a
function pointer or a function object pointer. */ function pointer or a function object pointer. */
static any_pointer static inline void
manage(any_pointer functor_ptr, functor_manager_operation_type op) manage(const function_buffer& in_buffer, function_buffer& out_buffer,
functor_manager_operation_type op)
{ {
if (op == check_functor_type_tag) { typedef typename get_function_tag<functor_type>::type tag_type;
std::type_info* type = switch (op) {
static_cast<std::type_info*>(functor_ptr.obj_ptr); case get_functor_type_tag:
return (BOOST_FUNCTION_COMPARE_TYPE_ID(typeid(Functor), *type)? out_buffer.const_obj_ptr = &typeid(functor_type);
functor_ptr return;
: make_any_pointer(reinterpret_cast<void*>(0)));
} default:
else { return manager(in_buffer, out_buffer, op, tag_type());
typedef typename get_function_tag<functor_type>::type tag_type;
return manager(functor_ptr, op, tag_type());
} }
} }
}; };
@ -394,7 +455,9 @@ namespace boost {
struct vtable_base struct vtable_base
{ {
vtable_base() : manager(0) { } vtable_base() : manager(0) { }
any_pointer (*manager)(any_pointer, functor_manager_operation_type); void (*manager)(const function_buffer& in_buffer,
function_buffer& out_buffer,
functor_manager_operation_type op);
}; };
} // end namespace function } // end namespace function
} // end namespace detail } // end namespace detail
@ -408,27 +471,32 @@ namespace boost {
class function_base class function_base
{ {
public: public:
function_base() : vtable(0) function_base() : vtable(0) { }
{
functor.obj_ptr = 0;
}
// Is this function empty? /** Determine if the function is empty (i.e., has no target). */
bool empty() const { return !vtable; } bool empty() const { return !vtable; }
/** Retrieve the type of the stored function object, or typeid(void)
if this is empty. */
const std::type_info& target_type() const
{
if (!vtable) return typeid(void);
detail::function::function_buffer type;
vtable->manager(functor, type, detail::function::get_functor_type_tag);
return *static_cast<const std::type_info*>(type.const_obj_ptr);
}
template<typename Functor> template<typename Functor>
Functor* target() Functor* target()
{ {
if (!vtable) return 0; if (!vtable) return 0;
detail::function::any_pointer result = detail::function::function_buffer type_result;
vtable->manager(detail::function::make_any_pointer(&typeid(Functor)), type_result.const_obj_ptr = &typeid(Functor);
detail::function::check_functor_type_tag); vtable->manager(functor, type_result,
if (!result.obj_ptr) return 0; detail::function::check_functor_type_tag);
else { return static_cast<Functor*>(type_result.obj_ptr);
typedef typename detail::function::get_function_tag<Functor>::type tag;
return get_functor_pointer<Functor>(tag(), 0);
}
} }
template<typename Functor> template<typename Functor>
@ -440,19 +508,11 @@ public:
{ {
if (!vtable) return 0; if (!vtable) return 0;
detail::function::any_pointer result = detail::function::function_buffer type_result;
vtable->manager(detail::function::make_any_pointer(&typeid(Functor)), type_result.const_obj_ptr = &typeid(Functor);
detail::function::check_functor_type_tag); vtable->manager(functor, type_result,
if (!result.obj_ptr) return 0; detail::function::check_functor_type_tag);
else { return static_cast<const Functor*>(type_result.obj_ptr);
typedef typename detail::function::get_function_tag<Functor>::type tag;
#if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
return get_functor_pointer(tag(), 0, (Functor*)0);
#else
return get_functor_pointer<Functor>(tag(), 0);
#endif
}
} }
template<typename F> template<typename F>
@ -495,41 +555,7 @@ public:
public: // should be protected, but GCC 2.95.3 will fail to allow access public: // should be protected, but GCC 2.95.3 will fail to allow access
detail::function::vtable_base* vtable; detail::function::vtable_base* vtable;
detail::function::any_pointer functor; mutable detail::function::function_buffer functor;
private:
template<typename Functor>
#if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
Functor* get_functor_pointer(detail::function::function_ptr_tag, int, Functor * = 0)
#else
Functor* get_functor_pointer(detail::function::function_ptr_tag, int)
#endif
{ return reinterpret_cast<Functor*>(&functor.func_ptr); }
template<typename Functor, typename Tag>
#if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
Functor* get_functor_pointer(Tag, long, Functor * = 0)
#else
Functor* get_functor_pointer(Tag, long)
#endif
{ return static_cast<Functor*>(functor.obj_ptr); }
template<typename Functor>
const Functor*
#if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
get_functor_pointer(detail::function::function_ptr_tag, int, Functor * = 0) const
#else
get_functor_pointer(detail::function::function_ptr_tag, int) const
#endif
{ return reinterpret_cast<const Functor*>(&functor.func_ptr); }
template<typename Functor, typename Tag>
#if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
const Functor* get_functor_pointer(Tag, long, Functor * = 0) const
#else
const Functor* get_functor_pointer(Tag, long) const
#endif
{ return static_cast<const Functor*>(functor.const_obj_ptr); }
}; };
/** /**

View File

@ -1,6 +1,6 @@
// Boost.Function library // Boost.Function library
// Copyright Douglas Gregor 2001-2003. Use, modification and // Copyright Douglas Gregor 2001-2006. Use, modification and
// distribution is subject to the Boost Software License, Version // distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
@ -50,16 +50,16 @@
BOOST_JOIN(function_obj_invoker,BOOST_FUNCTION_NUM_ARGS) BOOST_JOIN(function_obj_invoker,BOOST_FUNCTION_NUM_ARGS)
#define BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER \ #define BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER \
BOOST_JOIN(void_function_obj_invoker,BOOST_FUNCTION_NUM_ARGS) BOOST_JOIN(void_function_obj_invoker,BOOST_FUNCTION_NUM_ARGS)
#define BOOST_FUNCTION_STATELESS_FUNCTION_OBJ_INVOKER \ #define BOOST_FUNCTION_FUNCTION_REF_INVOKER \
BOOST_JOIN(stateless_function_obj_invoker,BOOST_FUNCTION_NUM_ARGS) BOOST_JOIN(function_ref_invoker,BOOST_FUNCTION_NUM_ARGS)
#define BOOST_FUNCTION_STATELESS_VOID_FUNCTION_OBJ_INVOKER \ #define BOOST_FUNCTION_VOID_FUNCTION_REF_INVOKER \
BOOST_JOIN(stateless_void_function_obj_invoker,BOOST_FUNCTION_NUM_ARGS) BOOST_JOIN(void_function_ref_invoker,BOOST_FUNCTION_NUM_ARGS)
#define BOOST_FUNCTION_GET_FUNCTION_INVOKER \ #define BOOST_FUNCTION_GET_FUNCTION_INVOKER \
BOOST_JOIN(get_function_invoker,BOOST_FUNCTION_NUM_ARGS) BOOST_JOIN(get_function_invoker,BOOST_FUNCTION_NUM_ARGS)
#define BOOST_FUNCTION_GET_FUNCTION_OBJ_INVOKER \ #define BOOST_FUNCTION_GET_FUNCTION_OBJ_INVOKER \
BOOST_JOIN(get_function_obj_invoker,BOOST_FUNCTION_NUM_ARGS) BOOST_JOIN(get_function_obj_invoker,BOOST_FUNCTION_NUM_ARGS)
#define BOOST_FUNCTION_GET_STATELESS_FUNCTION_OBJ_INVOKER \ #define BOOST_FUNCTION_GET_FUNCTION_REF_INVOKER \
BOOST_JOIN(get_stateless_function_obj_invoker,BOOST_FUNCTION_NUM_ARGS) BOOST_JOIN(get_function_ref_invoker,BOOST_FUNCTION_NUM_ARGS)
#define BOOST_FUNCTION_VTABLE BOOST_JOIN(basic_vtable,BOOST_FUNCTION_NUM_ARGS) #define BOOST_FUNCTION_VTABLE BOOST_JOIN(basic_vtable,BOOST_FUNCTION_NUM_ARGS)
#ifndef BOOST_NO_VOID_RETURNS #ifndef BOOST_NO_VOID_RETURNS
@ -80,7 +80,7 @@ namespace boost {
> >
struct BOOST_FUNCTION_FUNCTION_INVOKER struct BOOST_FUNCTION_FUNCTION_INVOKER
{ {
static R invoke(any_pointer function_ptr BOOST_FUNCTION_COMMA static R invoke(function_buffer& function_ptr BOOST_FUNCTION_COMMA
BOOST_FUNCTION_PARMS) BOOST_FUNCTION_PARMS)
{ {
FunctionPtr f = reinterpret_cast<FunctionPtr>(function_ptr.func_ptr); FunctionPtr f = reinterpret_cast<FunctionPtr>(function_ptr.func_ptr);
@ -96,7 +96,7 @@ namespace boost {
struct BOOST_FUNCTION_VOID_FUNCTION_INVOKER struct BOOST_FUNCTION_VOID_FUNCTION_INVOKER
{ {
static BOOST_FUNCTION_VOID_RETURN_TYPE static BOOST_FUNCTION_VOID_RETURN_TYPE
invoke(any_pointer function_ptr BOOST_FUNCTION_COMMA invoke(function_buffer& function_ptr BOOST_FUNCTION_COMMA
BOOST_FUNCTION_PARMS) BOOST_FUNCTION_PARMS)
{ {
@ -112,11 +112,15 @@ namespace boost {
> >
struct BOOST_FUNCTION_FUNCTION_OBJ_INVOKER struct BOOST_FUNCTION_FUNCTION_OBJ_INVOKER
{ {
static R invoke(any_pointer function_obj_ptr BOOST_FUNCTION_COMMA static R invoke(function_buffer& function_obj_ptr BOOST_FUNCTION_COMMA
BOOST_FUNCTION_PARMS) BOOST_FUNCTION_PARMS)
{ {
FunctionObj* f = (FunctionObj*)(function_obj_ptr.obj_ptr); FunctionObj* f;
if (function_allows_small_object_optimization<FunctionObj>::value)
f = reinterpret_cast<FunctionObj*>(&function_obj_ptr.data);
else
f = reinterpret_cast<FunctionObj*>(function_obj_ptr.obj_ptr);
return (*f)(BOOST_FUNCTION_ARGS); return (*f)(BOOST_FUNCTION_ARGS);
} }
}; };
@ -129,11 +133,15 @@ namespace boost {
struct BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER struct BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER
{ {
static BOOST_FUNCTION_VOID_RETURN_TYPE static BOOST_FUNCTION_VOID_RETURN_TYPE
invoke(any_pointer function_obj_ptr BOOST_FUNCTION_COMMA invoke(function_buffer& function_obj_ptr BOOST_FUNCTION_COMMA
BOOST_FUNCTION_PARMS) BOOST_FUNCTION_PARMS)
{ {
FunctionObj* f = (FunctionObj*)(function_obj_ptr.obj_ptr); FunctionObj* f;
if (function_allows_small_object_optimization<FunctionObj>::value)
f = reinterpret_cast<FunctionObj*>(&function_obj_ptr.data);
else
f = reinterpret_cast<FunctionObj*>(function_obj_ptr.obj_ptr);
BOOST_FUNCTION_RETURN((*f)(BOOST_FUNCTION_ARGS)); BOOST_FUNCTION_RETURN((*f)(BOOST_FUNCTION_ARGS));
} }
}; };
@ -143,12 +151,15 @@ namespace boost {
typename R BOOST_FUNCTION_COMMA typename R BOOST_FUNCTION_COMMA
BOOST_FUNCTION_TEMPLATE_PARMS BOOST_FUNCTION_TEMPLATE_PARMS
> >
struct BOOST_FUNCTION_STATELESS_FUNCTION_OBJ_INVOKER struct BOOST_FUNCTION_FUNCTION_REF_INVOKER
{ {
static R invoke(any_pointer BOOST_FUNCTION_COMMA BOOST_FUNCTION_PARMS) static R invoke(function_buffer& function_obj_ptr BOOST_FUNCTION_COMMA
BOOST_FUNCTION_PARMS)
{ {
FunctionObj f = FunctionObj(); FunctionObj* f =
return f(BOOST_FUNCTION_ARGS); reinterpret_cast<FunctionObj*>(function_obj_ptr.obj_ptr);
return (*f)(BOOST_FUNCTION_ARGS);
} }
}; };
@ -157,14 +168,16 @@ namespace boost {
typename R BOOST_FUNCTION_COMMA typename R BOOST_FUNCTION_COMMA
BOOST_FUNCTION_TEMPLATE_PARMS BOOST_FUNCTION_TEMPLATE_PARMS
> >
struct BOOST_FUNCTION_STATELESS_VOID_FUNCTION_OBJ_INVOKER struct BOOST_FUNCTION_VOID_FUNCTION_REF_INVOKER
{ {
static BOOST_FUNCTION_VOID_RETURN_TYPE static BOOST_FUNCTION_VOID_RETURN_TYPE
invoke(any_pointer BOOST_FUNCTION_COMMA BOOST_FUNCTION_PARMS) invoke(function_buffer& function_obj_ptr BOOST_FUNCTION_COMMA
BOOST_FUNCTION_PARMS)
{ {
FunctionObj f = FunctionObj(); FunctionObj* f =
BOOST_FUNCTION_RETURN(f(BOOST_FUNCTION_ARGS)); reinterpret_cast<FunctionObj*>(function_obj_ptr.obj_ptr);
BOOST_FUNCTION_RETURN((*f)(BOOST_FUNCTION_ARGS));
} }
}; };
@ -175,7 +188,7 @@ namespace boost {
> >
struct BOOST_FUNCTION_GET_FUNCTION_INVOKER struct BOOST_FUNCTION_GET_FUNCTION_INVOKER
{ {
typedef typename ct_if<(is_void<R>::value), typedef typename mpl::if_c<(is_void<R>::value),
BOOST_FUNCTION_VOID_FUNCTION_INVOKER< BOOST_FUNCTION_VOID_FUNCTION_INVOKER<
FunctionPtr, FunctionPtr,
R BOOST_FUNCTION_COMMA R BOOST_FUNCTION_COMMA
@ -196,7 +209,7 @@ namespace boost {
> >
struct BOOST_FUNCTION_GET_FUNCTION_OBJ_INVOKER struct BOOST_FUNCTION_GET_FUNCTION_OBJ_INVOKER
{ {
typedef typename ct_if<(is_void<R>::value), typedef typename mpl::if_c<(is_void<R>::value),
BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER< BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER<
FunctionObj, FunctionObj,
R BOOST_FUNCTION_COMMA R BOOST_FUNCTION_COMMA
@ -215,15 +228,15 @@ namespace boost {
typename R BOOST_FUNCTION_COMMA typename R BOOST_FUNCTION_COMMA
BOOST_FUNCTION_TEMPLATE_PARMS BOOST_FUNCTION_TEMPLATE_PARMS
> >
struct BOOST_FUNCTION_GET_STATELESS_FUNCTION_OBJ_INVOKER struct BOOST_FUNCTION_GET_FUNCTION_REF_INVOKER
{ {
typedef typename ct_if<(is_void<R>::value), typedef typename mpl::if_c<(is_void<R>::value),
BOOST_FUNCTION_STATELESS_VOID_FUNCTION_OBJ_INVOKER< BOOST_FUNCTION_VOID_FUNCTION_REF_INVOKER<
FunctionObj, FunctionObj,
R BOOST_FUNCTION_COMMA R BOOST_FUNCTION_COMMA
BOOST_FUNCTION_TEMPLATE_ARGS BOOST_FUNCTION_TEMPLATE_ARGS
>, >,
BOOST_FUNCTION_STATELESS_FUNCTION_OBJ_INVOKER< BOOST_FUNCTION_FUNCTION_REF_INVOKER<
FunctionObj, FunctionObj,
R BOOST_FUNCTION_COMMA R BOOST_FUNCTION_COMMA
BOOST_FUNCTION_TEMPLATE_ARGS BOOST_FUNCTION_TEMPLATE_ARGS
@ -244,7 +257,7 @@ namespace boost {
typedef typename function_return_type<R>::type result_type; typedef typename function_return_type<R>::type result_type;
#endif // BOOST_NO_VOID_RETURNS #endif // BOOST_NO_VOID_RETURNS
typedef result_type (*invoker_type)(any_pointer typedef result_type (*invoker_type)(function_buffer&
BOOST_FUNCTION_COMMA BOOST_FUNCTION_COMMA
BOOST_FUNCTION_TEMPLATE_ARGS); BOOST_FUNCTION_TEMPLATE_ARGS);
@ -255,16 +268,16 @@ namespace boost {
} }
template<typename F> template<typename F>
bool assign_to(F f, any_pointer& functor) bool assign_to(F f, function_buffer& functor)
{ {
typedef typename get_function_tag<F>::type tag; typedef typename get_function_tag<F>::type tag;
return assign_to(f, functor, tag()); return assign_to(f, functor, tag());
} }
void clear(any_pointer& functor) void clear(function_buffer& functor)
{ {
if (manager) if (manager)
functor = manager(functor, destroy_functor_tag); manager(functor, functor, destroy_functor_tag);
} }
private: private:
@ -291,14 +304,14 @@ namespace boost {
} }
template<typename FunctionPtr> template<typename FunctionPtr>
bool assign_to(FunctionPtr f, any_pointer& functor, function_ptr_tag) bool
assign_to(FunctionPtr f, function_buffer& functor, function_ptr_tag)
{ {
this->clear(functor); this->clear(functor);
if (f) { if (f) {
// should be a reinterpret cast, but some compilers insist // should be a reinterpret cast, but some compilers insist
// on giving cv-qualifiers to free functions // on giving cv-qualifiers to free functions
functor = manager(make_any_pointer((void (*)())(f)), functor.func_ptr = (void (*)())(f);
clone_functor_tag);
return true; return true;
} else { } else {
return false; return false;
@ -310,12 +323,18 @@ namespace boost {
template<typename MemberPtr> template<typename MemberPtr>
void init(MemberPtr f, member_ptr_tag) void init(MemberPtr f, member_ptr_tag)
{ {
// DPG TBD: Add explicit support for member function
// objects, so we invoke through mem_fn() but we retain the
// right target_type() values.
this->init(mem_fn(f)); this->init(mem_fn(f));
} }
template<typename MemberPtr> template<typename MemberPtr>
bool assign_to(MemberPtr f, any_pointer& functor, member_ptr_tag) bool assign_to(MemberPtr f, function_buffer& functor, member_ptr_tag)
{ {
// DPG TBD: Add explicit support for member function
// objects, so we invoke through mem_fn() but we retain the
// right target_type() values.
if (f) { if (f) {
this->assign_to(mem_fn(f), functor); this->assign_to(mem_fn(f), functor);
return true; return true;
@ -325,7 +344,7 @@ namespace boost {
} }
#endif // BOOST_FUNCTION_NUM_ARGS > 0 #endif // BOOST_FUNCTION_NUM_ARGS > 0
// Stateful function objects // Function objects
template<typename FunctionObj> template<typename FunctionObj>
void init(FunctionObj f, function_obj_tag) void init(FunctionObj f, function_obj_tag)
{ {
@ -340,24 +359,42 @@ namespace boost {
manager = &functor_manager<FunctionObj, Allocator>::manage; manager = &functor_manager<FunctionObj, Allocator>::manage;
} }
// Assign to a function object using the small object optimization
template<typename FunctionObj> template<typename FunctionObj>
bool assign_to(FunctionObj f, any_pointer& functor, function_obj_tag) void
assign_functor(FunctionObj f, function_buffer& functor, mpl::true_)
{
new ((void*)&functor.data) FunctionObj(f);
}
// Assign to a function object allocated on the heap.
template<typename FunctionObj>
void
assign_functor(FunctionObj f, function_buffer& functor, mpl::false_)
{
#ifndef BOOST_NO_STD_ALLOCATOR
typedef typename Allocator::template rebind<FunctionObj>::other
allocator_type;
typedef typename allocator_type::pointer pointer_type;
allocator_type allocator;
pointer_type copy = allocator.allocate(1);
allocator.construct(copy, f);
// Get back to the original pointer type
functor.obj_ptr = static_cast<FunctionObj*>(copy);
# else
functor.obj_ptr = new FunctionObj(f);
# endif // BOOST_NO_STD_ALLOCATOR
}
template<typename FunctionObj>
bool
assign_to(FunctionObj f, function_buffer& functor, function_obj_tag)
{ {
if (!boost::detail::function::has_empty_target(boost::addressof(f))) { if (!boost::detail::function::has_empty_target(boost::addressof(f))) {
#ifndef BOOST_NO_STD_ALLOCATOR assign_functor(f, functor,
typedef typename Allocator::template rebind<FunctionObj>::other mpl::bool_<(function_allows_small_object_optimization<FunctionObj>::value)>());
rebound_allocator_type;
typedef typename rebound_allocator_type::pointer pointer_type;
rebound_allocator_type allocator;
pointer_type copy = allocator.allocate(1);
allocator.construct(copy, f);
// Get back to the original pointer type
FunctionObj* new_f = static_cast<FunctionObj*>(copy);
#else
FunctionObj* new_f = new FunctionObj(f);
#endif // BOOST_NO_STD_ALLOCATOR
functor = make_any_pointer(static_cast<void*>(new_f));
return true; return true;
} else { } else {
return false; return false;
@ -369,7 +406,7 @@ namespace boost {
void void
init(const reference_wrapper<FunctionObj>& f, function_obj_ref_tag) init(const reference_wrapper<FunctionObj>& f, function_obj_ref_tag)
{ {
typedef typename BOOST_FUNCTION_GET_FUNCTION_OBJ_INVOKER< typedef typename BOOST_FUNCTION_GET_FUNCTION_REF_INVOKER<
FunctionObj, FunctionObj,
R BOOST_FUNCTION_COMMA R BOOST_FUNCTION_COMMA
BOOST_FUNCTION_TEMPLATE_ARGS BOOST_FUNCTION_TEMPLATE_ARGS
@ -377,46 +414,20 @@ namespace boost {
actual_invoker_type; actual_invoker_type;
invoker = &actual_invoker_type::invoke; invoker = &actual_invoker_type::invoke;
manager = &trivial_manager<FunctionObj>::get; manager = &reference_manager<FunctionObj>::get;
} }
template<typename FunctionObj> template<typename FunctionObj>
bool bool
assign_to(const reference_wrapper<FunctionObj>& f, any_pointer& functor, assign_to(const reference_wrapper<FunctionObj>& f,
function_obj_ref_tag) function_buffer& functor, function_obj_ref_tag)
{ {
if (!boost::detail::function::has_empty_target(f.get_pointer())) { if (!boost::detail::function::has_empty_target(f.get_pointer())) {
functor = manager(make_any_pointer( // DPG TBD: We might need to detect constness of
const_cast<FunctionObj*>(f.get_pointer())), // FunctionObj to assign into obj_ptr or const_obj_ptr to
clone_functor_tag); // be truly legit, but no platform in existence makes
return true; // const void* different from void*.
} else { functor.const_obj_ptr = f.get_pointer();
return false;
}
}
// Stateless function object
template<typename FunctionObj>
void
init(FunctionObj, stateless_function_obj_tag)
{
typedef
typename BOOST_FUNCTION_GET_STATELESS_FUNCTION_OBJ_INVOKER<
FunctionObj,
R BOOST_FUNCTION_COMMA
BOOST_FUNCTION_TEMPLATE_ARGS
>::type actual_invoker_type;
invoker = &actual_invoker_type::invoke;
manager = &trivial_manager<FunctionObj>::get;
}
template<typename FunctionObj>
bool
assign_to(FunctionObj f, any_pointer& functor,
stateless_function_obj_tag)
{
if (!boost::detail::function::has_empty_target(boost::addressof(f))) {
functor = make_any_pointer(this);
return true; return true;
} else { } else {
return false; return false;
@ -539,7 +550,13 @@ namespace boost {
#endif #endif
operator=(Functor BOOST_FUNCTION_TARGET_FIX(const &) f) operator=(Functor BOOST_FUNCTION_TARGET_FIX(const &) f)
{ {
self_type(f).swap(*this); this->clear();
try {
this->assign_to(f);
} catch (...) {
vtable = 0;
throw;
}
return *this; return *this;
} }
@ -564,7 +581,13 @@ namespace boost {
if (&f == this) if (&f == this)
return *this; return *this;
self_type(f).swap(*this); this->clear();
try {
this->assign_to_own(f);
} catch (...) {
vtable = 0;
throw;
}
return *this; return *this;
} }
@ -573,8 +596,9 @@ namespace boost {
if (&other == this) if (&other == this)
return; return;
std::swap(this->functor, other.functor); BOOST_FUNCTION_FUNCTION tmp = *this;
std::swap(this->vtable, other.vtable); *this = other;
other = tmp;
} }
// Clear out a target, if there is one // Clear out a target, if there is one
@ -610,9 +634,8 @@ namespace boost {
{ {
if (!f.empty()) { if (!f.empty()) {
this->vtable = f.vtable; this->vtable = f.vtable;
this->functor = f.vtable->manager(f.functor, this->functor,
f.vtable->manager(f.functor, boost::detail::function::clone_functor_tag);
boost::detail::function::clone_functor_tag);
} }
} }
@ -779,11 +802,11 @@ public:
#undef BOOST_FUNCTION_VOID_FUNCTION_INVOKER #undef BOOST_FUNCTION_VOID_FUNCTION_INVOKER
#undef BOOST_FUNCTION_FUNCTION_OBJ_INVOKER #undef BOOST_FUNCTION_FUNCTION_OBJ_INVOKER
#undef BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER #undef BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER
#undef BOOST_FUNCTION_STATELESS_FUNCTION_OBJ_INVOKER #undef BOOST_FUNCTION_FUNCTION_REF_INVOKER
#undef BOOST_FUNCTION_STATELESS_VOID_FUNCTION_OBJ_INVOKER #undef BOOST_FUNCTION_VOID_FUNCTION_REF_INVOKER
#undef BOOST_FUNCTION_GET_FUNCTION_INVOKER #undef BOOST_FUNCTION_GET_FUNCTION_INVOKER
#undef BOOST_FUNCTION_GET_FUNCTION_OBJ_INVOKER #undef BOOST_FUNCTION_GET_FUNCTION_OBJ_INVOKER
#undef BOOST_FUNCTION_GET_STATELESS_FUNCTION_OBJ_INVOKER #undef BOOST_FUNCTION_GET_FUNCTION_REF_INVOKER
#undef BOOST_FUNCTION_GET_MEM_FUNCTION_INVOKER #undef BOOST_FUNCTION_GET_MEM_FUNCTION_INVOKER
#undef BOOST_FUNCTION_TEMPLATE_PARMS #undef BOOST_FUNCTION_TEMPLATE_PARMS
#undef BOOST_FUNCTION_TEMPLATE_ARGS #undef BOOST_FUNCTION_TEMPLATE_ARGS

View File

@ -45,7 +45,7 @@ struct plus_int
{ {
int operator()(int x, int y) const { return x + y; } int operator()(int x, int y) const { return x + y; }
int unused_state_data; int unused_state_data[32];
}; };
static int do_minus(int x, int y) { return x-y; } static int do_minus(int x, int y) { return x-y; }
@ -54,7 +54,7 @@ struct DoNothing
{ {
void operator()() const {} void operator()() const {}
int unused_state_data; int unused_state_data[32];
}; };
static void do_nothing() {} static void do_nothing() {}

View File

@ -14,23 +14,21 @@
struct stateless_integer_add { struct stateless_integer_add {
int operator()(int x, int y) const { return x+y; } int operator()(int x, int y) const { return x+y; }
void* operator new(std::size_t, stateless_integer_add*) void* operator new(std::size_t)
{ {
throw std::runtime_error("Cannot allocate a stateless_integer_add"); throw std::runtime_error("Cannot allocate a stateless_integer_add");
} }
void operator delete(void*, stateless_integer_add*) throw() void* operator new(std::size_t, void* p)
{
return p;
}
void operator delete(void*) throw()
{ {
} }
}; };
namespace boost {
template<>
struct is_stateless<stateless_integer_add> {
BOOST_STATIC_CONSTANT(bool, value = true);
};
}
int test_main(int, char*[]) int test_main(int, char*[])
{ {
boost::function2<int, int, int> f; boost::function2<int, int, int> f;