]> git.lyx.org Git - lyx.git/blobdiff - boost/boost/function/function_base.hpp
Update in-tree boost to latest from boost 1.34 cvs.
[lyx.git] / boost / boost / function / function_base.hpp
index 23f5f201c7d28ff7358123b5f6c965ef7517b72b..ae83343c794382edbad1e843fb45308338d22bd7 100644 (file)
@@ -1,6 +1,6 @@
 // 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
 //  1.0. (See accompanying file LICENSE_1_0.txt or copy at
 //  http://www.boost.org/LICENSE_1_0.txt)
 #include <boost/assert.hpp>
 #include <boost/type_traits/is_integral.hpp>
 #include <boost/type_traits/composite_traits.hpp>
-#include <boost/type_traits/is_stateless.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
@@ -63,11 +63,19 @@ namespace boost { namespace python { namespace objects {
 #  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), \
+#if !BOOST_WORKAROUND(__BORLANDC__, < 0x600)
+#  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
+// 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
 
 #if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX)
 namespace boost {
@@ -97,41 +105,35 @@ inline void swap(function<Signature, Allocator>& f1,
 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
       {
+        // For pointers to function objects
         void* obj_ptr;
+
+        // For pointers to std::type_info objects
+        // (get_functor_type_tag, check_functor_type_tag).
         const void* const_obj_ptr;
-        void (*func_ptr)();
-        char data[1];
-      };
 
-      inline any_pointer make_any_pointer(void* o)
-      {
-        any_pointer p;
-        p.obj_ptr = o;
-        return p;
-      }
+        // For function pointers of all kinds
+        mutable void (*func_ptr)();
 
-      inline any_pointer make_any_pointer(const void* o)
-      {
-        any_pointer p;
-        p.const_obj_ptr = o;
-        return p;
-      }
+        // For bound member pointers
+        struct bound_memfunc_ptr_t {
+          void (X::*memfunc_ptr)(int);
+          void* obj_ptr;
+        } bound_memfunc_ptr;
 
-      inline any_pointer make_any_pointer(void (*f)())
-      {
-        any_pointer p;
-        p.func_ptr = f;
-        return p;
-      }
+        // To relax aliasing constraints
+        mutable char data;
+      };
 
       /**
        * The unusable class is a placeholder for unused function arguments
@@ -161,7 +163,8 @@ namespace boost {
       enum functor_manager_operation_type {
         clone_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
@@ -169,57 +172,79 @@ namespace boost {
       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
+        get(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_ptr = in_buffer.obj_ptr;
+            return;
 
           case destroy_functor_tag:
-            return make_any_pointer(reinterpret_cast<void*>(0));
+            out_buffer.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));
+              // DPG TBD: Since we're only storing a pointer, it's
+              // possible that the user could ask for a base class or
+              // derived class. Is that okay?
+              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
-          return make_any_pointer(reinterpret_cast<void*>(0));
+          case get_functor_type_tag:
+            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
        * can clone or destroy the given function/function object pointer.
@@ -231,30 +256,59 @@ namespace boost {
         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)
+        static inline void
+        manager(const function_buffer& in_buffer, function_buffer& out_buffer, 
+                functor_manager_operation_type op, function_ptr_tag)
         {
           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 == destroy_functor_tag)
+            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 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
+        manager(const function_buffer& in_buffer, function_buffer& out_buffer, 
+                functor_manager_operation_type op, mpl::true_)
+        {
+          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);
+            // Some compilers (Borland, vc6, ...) are unhappy with ~functor_type.
+            out_functor->~Functor();
+          } 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
-        typedef typename Allocator::template rebind<functor_type>::other
-          allocator_type;
-        typedef typename allocator_type::pointer pointer_type;
+          typedef typename Allocator::template rebind<functor_type>::other
+            allocator_type;
+          typedef typename allocator_type::pointer pointer_type;
 #else
-        typedef functor_type* pointer_type;
+          typedef functor_type* pointer_type;
 #endif // BOOST_NO_STD_ALLOCATOR
 
 #  ifndef BOOST_NO_STD_ALLOCATOR
@@ -262,8 +316,10 @@ namespace boost {
 #  endif // BOOST_NO_STD_ALLOCATOR
 
           if (op == clone_functor_tag) {
-            functor_type* f =
-              static_cast<functor_type*>(function_obj_ptr.obj_ptr);
+            // GCC 2.95.3 gets the CV qualifiers wrong here, so we
+            // can't do the static_cast that we should do.
+            const functor_type* f =
+              (const functor_type*)(in_buffer.obj_ptr);
 
             // Clone the functor
 #  ifndef BOOST_NO_STD_ALLOCATOR
@@ -275,12 +331,11 @@ namespace boost {
 #  else
             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 == 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);
 
 #  ifndef BOOST_NO_STD_ALLOCATOR
             /* Cast from the functor pointer type to the allocator's pointer
@@ -293,26 +348,44 @@ namespace boost {
 #  else
             delete f;
 #  endif // BOOST_NO_STD_ALLOCATOR
-
-            return make_any_pointer(static_cast<void*>(0));
+            out_buffer.obj_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.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:
         /* 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.const_obj_ptr = &typeid(functor_type);
+            return;
+
+          default:
+            manager(in_buffer, out_buffer, op, tag_type());
+            return;
           }
         }
       };
@@ -378,6 +451,18 @@ namespace boost {
           else return true;
         }
 #endif // BOOST_NO_SFINAE
+
+      /**
+       * Stores the "manager" portion of the vtable for a
+       * boost::function object.
+       */
+      struct vtable_base
+      {
+        vtable_base() : manager(0) { }
+        void (*manager)(const function_buffer& in_buffer, 
+                        function_buffer& out_buffer, 
+                        functor_manager_operation_type op);
+      };
     } // end namespace function
   } // end namespace detail
 
@@ -390,48 +475,61 @@ namespace boost {
 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 typeid(void)
+      if this is empty. */
+  const std::type_info& target_type() const
   {
-    functor.obj_ptr = 0;
-  }
+    if (!vtable) return typeid(void);
 
-  // Is this function empty?
-  bool empty() const { return !manager; }
+    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>
     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.const_obj_ptr = &typeid(Functor);
+      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.const_obj_ptr = &typeid(Functor);
+      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 (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;
@@ -462,28 +560,8 @@ public:
 #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); }
-
-  template<typename Functor>
-    const Functor*
-    get_functor_pointer(detail::function::function_ptr_tag, int) const
-    { return reinterpret_cast<const Functor*>(&functor.func_ptr); }
-
-  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;
 };
 
 /**