os/ossrv/ossrv_pub/boost_apis/boost/function/function_base.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Boost.Function library
sl@0
     2
sl@0
     3
//  Copyright Douglas Gregor 2001-2006. Use, modification and
sl@0
     4
//  distribution is subject to the Boost Software License, Version
sl@0
     5
//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
sl@0
     6
//  http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
sl@0
     8
// For more information, see http://www.boost.org
sl@0
     9
sl@0
    10
#ifndef BOOST_FUNCTION_BASE_HEADER
sl@0
    11
#define BOOST_FUNCTION_BASE_HEADER
sl@0
    12
sl@0
    13
#include <stdexcept>
sl@0
    14
#include <string>
sl@0
    15
#include <memory>
sl@0
    16
#include <new>
sl@0
    17
#include <typeinfo>
sl@0
    18
#include <boost/config.hpp>
sl@0
    19
#include <boost/assert.hpp>
sl@0
    20
#include <boost/type_traits/is_integral.hpp>
sl@0
    21
#include <boost/type_traits/composite_traits.hpp>
sl@0
    22
#include <boost/ref.hpp>
sl@0
    23
#include <boost/mpl/if.hpp>
sl@0
    24
#include <boost/detail/workaround.hpp>
sl@0
    25
#include <boost/type_traits/alignment_of.hpp>
sl@0
    26
#ifndef BOOST_NO_SFINAE
sl@0
    27
#  include "boost/utility/enable_if.hpp"
sl@0
    28
#else
sl@0
    29
#  include "boost/mpl/bool.hpp"
sl@0
    30
#endif
sl@0
    31
#include <boost/function_equal.hpp>
sl@0
    32
sl@0
    33
// Borrowed from Boost.Python library: determines the cases where we
sl@0
    34
// need to use std::type_info::name to compare instead of operator==.
sl@0
    35
# if (defined(__GNUC__) && __GNUC__ >= 3) \
sl@0
    36
 || defined(_AIX) \
sl@0
    37
 || (   defined(__sgi) && defined(__host_mips))
sl@0
    38
#  include <cstring>
sl@0
    39
#  define BOOST_FUNCTION_COMPARE_TYPE_ID(X,Y) \
sl@0
    40
     (std::strcmp((X).name(),(Y).name()) == 0)
sl@0
    41
# else
sl@0
    42
#  define BOOST_FUNCTION_COMPARE_TYPE_ID(X,Y) ((X)==(Y))
sl@0
    43
#endif
sl@0
    44
sl@0
    45
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 || defined(__ICL) && __ICL <= 600 || defined(__MWERKS__) && __MWERKS__ < 0x2406 && !defined(BOOST_STRICT_CONFIG)
sl@0
    46
#  define BOOST_FUNCTION_TARGET_FIX(x) x
sl@0
    47
#else
sl@0
    48
#  define BOOST_FUNCTION_TARGET_FIX(x)
sl@0
    49
#endif // not MSVC
sl@0
    50
sl@0
    51
#if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730 && !defined(BOOST_STRICT_CONFIG)
sl@0
    52
// Work around a compiler bug.
sl@0
    53
// boost::python::objects::function has to be seen by the compiler before the
sl@0
    54
// boost::function class template.
sl@0
    55
namespace boost { namespace python { namespace objects {
sl@0
    56
  class function;
sl@0
    57
}}}
sl@0
    58
#endif
sl@0
    59
sl@0
    60
#if defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)                    \
sl@0
    61
 || defined(BOOST_BCB_PARTIAL_SPECIALIZATION_BUG)                         \
sl@0
    62
 || !(BOOST_STRICT_CONFIG || !defined(__SUNPRO_CC) || __SUNPRO_CC > 0x540)
sl@0
    63
#  define BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX
sl@0
    64
#endif
sl@0
    65
sl@0
    66
#if !BOOST_WORKAROUND(__BORLANDC__, < 0x600)
sl@0
    67
#  define BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor,Type)              \
sl@0
    68
      typename ::boost::enable_if_c<(::boost::type_traits::ice_not<          \
sl@0
    69
                            (::boost::is_integral<Functor>::value)>::value), \
sl@0
    70
                           Type>::type
sl@0
    71
#else
sl@0
    72
// BCC doesn't recognize this depends on a template argument and complains
sl@0
    73
// about the use of 'typename'
sl@0
    74
#  define BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor,Type)     \
sl@0
    75
      ::boost::enable_if_c<(::boost::type_traits::ice_not<          \
sl@0
    76
                   (::boost::is_integral<Functor>::value)>::value), \
sl@0
    77
                       Type>::type
sl@0
    78
#endif
sl@0
    79
sl@0
    80
#if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX)
sl@0
    81
namespace boost {
sl@0
    82
sl@0
    83
#if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730 && !defined(BOOST_STRICT_CONFIG)
sl@0
    84
// The library shipping with MIPSpro 7.3.1.3m has a broken allocator<void>
sl@0
    85
class function_base;
sl@0
    86
sl@0
    87
template<typename Signature,
sl@0
    88
         typename Allocator = std::allocator<function_base> >
sl@0
    89
class function;
sl@0
    90
#else
sl@0
    91
template<typename Signature, typename Allocator = std::allocator<void> >
sl@0
    92
class function;
sl@0
    93
#endif
sl@0
    94
sl@0
    95
template<typename Signature, typename Allocator>
sl@0
    96
inline void swap(function<Signature, Allocator>& f1,
sl@0
    97
                 function<Signature, Allocator>& f2)
sl@0
    98
{
sl@0
    99
  f1.swap(f2);
sl@0
   100
}
sl@0
   101
sl@0
   102
} // end namespace boost
sl@0
   103
#endif // have partial specialization
sl@0
   104
sl@0
   105
namespace boost {
sl@0
   106
  namespace detail {
sl@0
   107
    namespace function {
sl@0
   108
      class X;
sl@0
   109
sl@0
   110
      /**
sl@0
   111
       * A buffer used to store small function objects in
sl@0
   112
       * boost::function. It is a union containing function pointers,
sl@0
   113
       * object pointers, and a structure that resembles a bound
sl@0
   114
       * member function pointer.
sl@0
   115
       */
sl@0
   116
      union function_buffer
sl@0
   117
      {
sl@0
   118
        // For pointers to function objects
sl@0
   119
        void* obj_ptr;
sl@0
   120
sl@0
   121
        // For pointers to std::type_info objects
sl@0
   122
        // (get_functor_type_tag, check_functor_type_tag).
sl@0
   123
        const void* const_obj_ptr;
sl@0
   124
sl@0
   125
        // For function pointers of all kinds
sl@0
   126
        mutable void (*func_ptr)();
sl@0
   127
sl@0
   128
        // For bound member pointers
sl@0
   129
        struct bound_memfunc_ptr_t {
sl@0
   130
          void (X::*memfunc_ptr)(int);
sl@0
   131
          void* obj_ptr;
sl@0
   132
        } bound_memfunc_ptr;
sl@0
   133
sl@0
   134
        // To relax aliasing constraints
sl@0
   135
        mutable char data;
sl@0
   136
      };
sl@0
   137
sl@0
   138
      /**
sl@0
   139
       * The unusable class is a placeholder for unused function arguments
sl@0
   140
       * It is also completely unusable except that it constructable from
sl@0
   141
       * anything. This helps compilers without partial specialization to
sl@0
   142
       * handle Boost.Function objects returning void.
sl@0
   143
       */
sl@0
   144
      struct unusable
sl@0
   145
      {
sl@0
   146
        unusable() {}
sl@0
   147
        template<typename T> unusable(const T&) {}
sl@0
   148
      };
sl@0
   149
sl@0
   150
      /* Determine the return type. This supports compilers that do not support
sl@0
   151
       * void returns or partial specialization by silently changing the return
sl@0
   152
       * type to "unusable".
sl@0
   153
       */
sl@0
   154
      template<typename T> struct function_return_type { typedef T type; };
sl@0
   155
sl@0
   156
      template<>
sl@0
   157
      struct function_return_type<void>
sl@0
   158
      {
sl@0
   159
        typedef unusable type;
sl@0
   160
      };
sl@0
   161
sl@0
   162
      // The operation type to perform on the given functor/function pointer
sl@0
   163
      enum functor_manager_operation_type {
sl@0
   164
        clone_functor_tag,
sl@0
   165
        destroy_functor_tag,
sl@0
   166
        check_functor_type_tag,
sl@0
   167
        get_functor_type_tag
sl@0
   168
      };
sl@0
   169
sl@0
   170
      // Tags used to decide between different types of functions
sl@0
   171
      struct function_ptr_tag {};
sl@0
   172
      struct function_obj_tag {};
sl@0
   173
      struct member_ptr_tag {};
sl@0
   174
      struct function_obj_ref_tag {};
sl@0
   175
sl@0
   176
      template<typename F>
sl@0
   177
      class get_function_tag
sl@0
   178
      {
sl@0
   179
        typedef typename mpl::if_c<(is_pointer<F>::value),
sl@0
   180
                                   function_ptr_tag,
sl@0
   181
                                   function_obj_tag>::type ptr_or_obj_tag;
sl@0
   182
sl@0
   183
        typedef typename mpl::if_c<(is_member_pointer<F>::value),
sl@0
   184
                                   member_ptr_tag,
sl@0
   185
                                   ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag;
sl@0
   186
sl@0
   187
        typedef typename mpl::if_c<(is_reference_wrapper<F>::value),
sl@0
   188
                                   function_obj_ref_tag,
sl@0
   189
                                   ptr_or_obj_or_mem_tag>::type or_ref_tag;
sl@0
   190
sl@0
   191
      public:
sl@0
   192
        typedef or_ref_tag type;
sl@0
   193
      };
sl@0
   194
sl@0
   195
      // The trivial manager does nothing but return the same pointer (if we
sl@0
   196
      // are cloning) or return the null pointer (if we are deleting).
sl@0
   197
      template<typename F>
sl@0
   198
      struct reference_manager
sl@0
   199
      {
sl@0
   200
        static inline void
sl@0
   201
        get(const function_buffer& in_buffer, function_buffer& out_buffer, 
sl@0
   202
            functor_manager_operation_type op)
sl@0
   203
        {
sl@0
   204
          switch (op) {
sl@0
   205
          case clone_functor_tag: 
sl@0
   206
            out_buffer.obj_ptr = in_buffer.obj_ptr;
sl@0
   207
            return;
sl@0
   208
sl@0
   209
          case destroy_functor_tag:
sl@0
   210
            out_buffer.obj_ptr = 0;
sl@0
   211
            return;
sl@0
   212
sl@0
   213
          case check_functor_type_tag:
sl@0
   214
            {
sl@0
   215
              // DPG TBD: Since we're only storing a pointer, it's
sl@0
   216
              // possible that the user could ask for a base class or
sl@0
   217
              // derived class. Is that okay?
sl@0
   218
              const std::type_info& check_type = 
sl@0
   219
                *static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
sl@0
   220
              if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(F)))
sl@0
   221
                out_buffer.obj_ptr = in_buffer.obj_ptr;
sl@0
   222
              else
sl@0
   223
                out_buffer.obj_ptr = 0;
sl@0
   224
            }
sl@0
   225
            return;
sl@0
   226
sl@0
   227
          case get_functor_type_tag:
sl@0
   228
            out_buffer.const_obj_ptr = &typeid(F);
sl@0
   229
            return;
sl@0
   230
          }
sl@0
   231
        }
sl@0
   232
      };
sl@0
   233
sl@0
   234
      /**
sl@0
   235
       * Determine if boost::function can use the small-object
sl@0
   236
       * optimization with the function object type F.
sl@0
   237
       */
sl@0
   238
      template<typename F>
sl@0
   239
      struct function_allows_small_object_optimization
sl@0
   240
      {
sl@0
   241
        BOOST_STATIC_CONSTANT
sl@0
   242
          (bool, 
sl@0
   243
           value = ((sizeof(F) <= sizeof(function_buffer) &&
sl@0
   244
                     (alignment_of<function_buffer>::value 
sl@0
   245
                      % alignment_of<F>::value == 0))));
sl@0
   246
      };
sl@0
   247
sl@0
   248
      /**
sl@0
   249
       * The functor_manager class contains a static function "manage" which
sl@0
   250
       * can clone or destroy the given function/function object pointer.
sl@0
   251
       */
sl@0
   252
      template<typename Functor, typename Allocator>
sl@0
   253
      struct functor_manager
sl@0
   254
      {
sl@0
   255
      private:
sl@0
   256
        typedef Functor functor_type;
sl@0
   257
sl@0
   258
        // For function pointers, the manager is trivial
sl@0
   259
        static inline void
sl@0
   260
        manager(const function_buffer& in_buffer, function_buffer& out_buffer, 
sl@0
   261
                functor_manager_operation_type op, function_ptr_tag)
sl@0
   262
        {
sl@0
   263
          if (op == clone_functor_tag)
sl@0
   264
            out_buffer.func_ptr = in_buffer.func_ptr;
sl@0
   265
          else if (op == destroy_functor_tag)
sl@0
   266
            out_buffer.func_ptr = 0;
sl@0
   267
          else /* op == check_functor_type_tag */ {
sl@0
   268
            const std::type_info& check_type = 
sl@0
   269
              *static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
sl@0
   270
            if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(Functor)))
sl@0
   271
              out_buffer.obj_ptr = &in_buffer.func_ptr;
sl@0
   272
            else
sl@0
   273
              out_buffer.obj_ptr = 0;
sl@0
   274
          }
sl@0
   275
        }
sl@0
   276
sl@0
   277
        // Function objects that fit in the small-object buffer.
sl@0
   278
        static inline void
sl@0
   279
        manager(const function_buffer& in_buffer, function_buffer& out_buffer, 
sl@0
   280
                functor_manager_operation_type op, mpl::true_)
sl@0
   281
        {
sl@0
   282
          if (op == clone_functor_tag) {
sl@0
   283
            const functor_type* in_functor = 
sl@0
   284
              reinterpret_cast<const functor_type*>(&in_buffer.data);
sl@0
   285
            new ((void*)&out_buffer.data) functor_type(*in_functor);
sl@0
   286
          } else if (op == destroy_functor_tag) {
sl@0
   287
            // Some compilers (Borland, vc6, ...) are unhappy with ~functor_type.
sl@0
   288
            reinterpret_cast<functor_type*>(&out_buffer.data)->~Functor();
sl@0
   289
          } else /* op == check_functor_type_tag */ {
sl@0
   290
            const std::type_info& check_type = 
sl@0
   291
              *static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
sl@0
   292
            if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(Functor)))
sl@0
   293
              out_buffer.obj_ptr = &in_buffer.data;
sl@0
   294
            else
sl@0
   295
              out_buffer.obj_ptr = 0;
sl@0
   296
          }
sl@0
   297
        }
sl@0
   298
        
sl@0
   299
        // Function objects that require heap allocation
sl@0
   300
        static inline void
sl@0
   301
        manager(const function_buffer& in_buffer, function_buffer& out_buffer, 
sl@0
   302
                functor_manager_operation_type op, mpl::false_)
sl@0
   303
        {
sl@0
   304
#ifndef BOOST_NO_STD_ALLOCATOR
sl@0
   305
          typedef typename Allocator::template rebind<functor_type>::other
sl@0
   306
            allocator_type;
sl@0
   307
          typedef typename allocator_type::pointer pointer_type;
sl@0
   308
#else
sl@0
   309
          typedef functor_type* pointer_type;
sl@0
   310
#endif // BOOST_NO_STD_ALLOCATOR
sl@0
   311
sl@0
   312
#  ifndef BOOST_NO_STD_ALLOCATOR
sl@0
   313
          allocator_type allocator;
sl@0
   314
#  endif // BOOST_NO_STD_ALLOCATOR
sl@0
   315
sl@0
   316
          if (op == clone_functor_tag) {
sl@0
   317
            // GCC 2.95.3 gets the CV qualifiers wrong here, so we
sl@0
   318
            // can't do the static_cast that we should do.
sl@0
   319
            const functor_type* f =
sl@0
   320
              (const functor_type*)(in_buffer.obj_ptr);
sl@0
   321
sl@0
   322
            // Clone the functor
sl@0
   323
#  ifndef BOOST_NO_STD_ALLOCATOR
sl@0
   324
            pointer_type copy = allocator.allocate(1);
sl@0
   325
            allocator.construct(copy, *f);
sl@0
   326
sl@0
   327
            // Get back to the original pointer type
sl@0
   328
            functor_type* new_f = static_cast<functor_type*>(copy);
sl@0
   329
#  else
sl@0
   330
            functor_type* new_f = new functor_type(*f);
sl@0
   331
#  endif // BOOST_NO_STD_ALLOCATOR
sl@0
   332
            out_buffer.obj_ptr = new_f;
sl@0
   333
          } else if (op == destroy_functor_tag) {
sl@0
   334
            /* Cast from the void pointer to the functor pointer type */
sl@0
   335
            functor_type* f =
sl@0
   336
              static_cast<functor_type*>(out_buffer.obj_ptr);
sl@0
   337
sl@0
   338
#  ifndef BOOST_NO_STD_ALLOCATOR
sl@0
   339
            /* Cast from the functor pointer type to the allocator's pointer
sl@0
   340
               type */
sl@0
   341
            pointer_type victim = static_cast<pointer_type>(f);
sl@0
   342
sl@0
   343
            // Destroy and deallocate the functor
sl@0
   344
            allocator.destroy(victim);
sl@0
   345
            allocator.deallocate(victim, 1);
sl@0
   346
#  else
sl@0
   347
            delete f;
sl@0
   348
#  endif // BOOST_NO_STD_ALLOCATOR
sl@0
   349
            out_buffer.obj_ptr = 0;
sl@0
   350
          } else /* op == check_functor_type_tag */ {
sl@0
   351
            const std::type_info& check_type = 
sl@0
   352
              *static_cast<const std::type_info*>(out_buffer.const_obj_ptr);
sl@0
   353
            if (BOOST_FUNCTION_COMPARE_TYPE_ID(check_type, typeid(Functor)))
sl@0
   354
              out_buffer.obj_ptr = in_buffer.obj_ptr;
sl@0
   355
            else
sl@0
   356
              out_buffer.obj_ptr = 0;
sl@0
   357
          }
sl@0
   358
        }
sl@0
   359
sl@0
   360
        // For function objects, we determine whether the function
sl@0
   361
        // object can use the small-object optimization buffer or
sl@0
   362
        // whether we need to allocate it on the heap.
sl@0
   363
        static inline void
sl@0
   364
        manager(const function_buffer& in_buffer, function_buffer& out_buffer, 
sl@0
   365
                functor_manager_operation_type op, function_obj_tag)
sl@0
   366
        {
sl@0
   367
          manager(in_buffer, out_buffer, op,
sl@0
   368
                  mpl::bool_<(function_allows_small_object_optimization<functor_type>::value)>());
sl@0
   369
        }
sl@0
   370
sl@0
   371
      public:
sl@0
   372
        /* Dispatch to an appropriate manager based on whether we have a
sl@0
   373
           function pointer or a function object pointer. */
sl@0
   374
        static inline void
sl@0
   375
        manage(const function_buffer& in_buffer, function_buffer& out_buffer, 
sl@0
   376
               functor_manager_operation_type op)
sl@0
   377
        {
sl@0
   378
          typedef typename get_function_tag<functor_type>::type tag_type;
sl@0
   379
          switch (op) {
sl@0
   380
          case get_functor_type_tag:
sl@0
   381
            out_buffer.const_obj_ptr = &typeid(functor_type);
sl@0
   382
            return;
sl@0
   383
sl@0
   384
          default:
sl@0
   385
            manager(in_buffer, out_buffer, op, tag_type());
sl@0
   386
            return;
sl@0
   387
          }
sl@0
   388
        }
sl@0
   389
      };
sl@0
   390
sl@0
   391
      // A type that is only used for comparisons against zero
sl@0
   392
      struct useless_clear_type {};
sl@0
   393
sl@0
   394
#ifdef BOOST_NO_SFINAE
sl@0
   395
      // These routines perform comparisons between a Boost.Function
sl@0
   396
      // object and an arbitrary function object (when the last
sl@0
   397
      // parameter is mpl::bool_<false>) or against zero (when the
sl@0
   398
      // last parameter is mpl::bool_<true>). They are only necessary
sl@0
   399
      // for compilers that don't support SFINAE.
sl@0
   400
      template<typename Function, typename Functor>
sl@0
   401
        bool
sl@0
   402
        compare_equal(const Function& f, const Functor&, int, mpl::bool_<true>)
sl@0
   403
        { return f.empty(); }
sl@0
   404
sl@0
   405
      template<typename Function, typename Functor>
sl@0
   406
        bool
sl@0
   407
        compare_not_equal(const Function& f, const Functor&, int,
sl@0
   408
                          mpl::bool_<true>)
sl@0
   409
        { return !f.empty(); }
sl@0
   410
sl@0
   411
      template<typename Function, typename Functor>
sl@0
   412
        bool
sl@0
   413
        compare_equal(const Function& f, const Functor& g, long,
sl@0
   414
                      mpl::bool_<false>)
sl@0
   415
        {
sl@0
   416
          if (const Functor* fp = f.template target<Functor>())
sl@0
   417
            return function_equal(*fp, g);
sl@0
   418
          else return false;
sl@0
   419
        }
sl@0
   420
sl@0
   421
      template<typename Function, typename Functor>
sl@0
   422
        bool
sl@0
   423
        compare_equal(const Function& f, const reference_wrapper<Functor>& g,
sl@0
   424
                      int, mpl::bool_<false>)
sl@0
   425
        {
sl@0
   426
          if (const Functor* fp = f.template target<Functor>())
sl@0
   427
            return fp == g.get_pointer();
sl@0
   428
          else return false;
sl@0
   429
        }
sl@0
   430
sl@0
   431
      template<typename Function, typename Functor>
sl@0
   432
        bool
sl@0
   433
        compare_not_equal(const Function& f, const Functor& g, long,
sl@0
   434
                          mpl::bool_<false>)
sl@0
   435
        {
sl@0
   436
          if (const Functor* fp = f.template target<Functor>())
sl@0
   437
            return !function_equal(*fp, g);
sl@0
   438
          else return true;
sl@0
   439
        }
sl@0
   440
sl@0
   441
      template<typename Function, typename Functor>
sl@0
   442
        bool
sl@0
   443
        compare_not_equal(const Function& f,
sl@0
   444
                          const reference_wrapper<Functor>& g, int,
sl@0
   445
                          mpl::bool_<false>)
sl@0
   446
        {
sl@0
   447
          if (const Functor* fp = f.template target<Functor>())
sl@0
   448
            return fp != g.get_pointer();
sl@0
   449
          else return true;
sl@0
   450
        }
sl@0
   451
#endif // BOOST_NO_SFINAE
sl@0
   452
sl@0
   453
      /**
sl@0
   454
       * Stores the "manager" portion of the vtable for a
sl@0
   455
       * boost::function object.
sl@0
   456
       */
sl@0
   457
      struct vtable_base
sl@0
   458
      {
sl@0
   459
        vtable_base() : manager(0) { }
sl@0
   460
        void (*manager)(const function_buffer& in_buffer, 
sl@0
   461
                        function_buffer& out_buffer, 
sl@0
   462
                        functor_manager_operation_type op);
sl@0
   463
      };
sl@0
   464
    } // end namespace function
sl@0
   465
  } // end namespace detail
sl@0
   466
sl@0
   467
/**
sl@0
   468
 * The function_base class contains the basic elements needed for the
sl@0
   469
 * function1, function2, function3, etc. classes. It is common to all
sl@0
   470
 * functions (and as such can be used to tell if we have one of the
sl@0
   471
 * functionN objects).
sl@0
   472
 */
sl@0
   473
class function_base
sl@0
   474
{
sl@0
   475
public:
sl@0
   476
  function_base() : vtable(0) { }
sl@0
   477
sl@0
   478
  /** Determine if the function is empty (i.e., has no target). */
sl@0
   479
  bool empty() const { return !vtable; }
sl@0
   480
sl@0
   481
  /** Retrieve the type of the stored function object, or typeid(void)
sl@0
   482
      if this is empty. */
sl@0
   483
  const std::type_info& target_type() const
sl@0
   484
  {
sl@0
   485
    if (!vtable) return typeid(void);
sl@0
   486
sl@0
   487
    detail::function::function_buffer type;
sl@0
   488
    vtable->manager(functor, type, detail::function::get_functor_type_tag);
sl@0
   489
    return *static_cast<const std::type_info*>(type.const_obj_ptr);
sl@0
   490
  }
sl@0
   491
sl@0
   492
  template<typename Functor>
sl@0
   493
    Functor* target()
sl@0
   494
    {
sl@0
   495
      if (!vtable) return 0;
sl@0
   496
sl@0
   497
      detail::function::function_buffer type_result;
sl@0
   498
      type_result.const_obj_ptr = &typeid(Functor);
sl@0
   499
      vtable->manager(functor, type_result, 
sl@0
   500
                      detail::function::check_functor_type_tag);
sl@0
   501
      return static_cast<Functor*>(type_result.obj_ptr);
sl@0
   502
    }
sl@0
   503
sl@0
   504
  template<typename Functor>
sl@0
   505
#if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
sl@0
   506
    const Functor* target( Functor * = 0 ) const
sl@0
   507
#else
sl@0
   508
    const Functor* target() const
sl@0
   509
#endif
sl@0
   510
    {
sl@0
   511
      if (!vtable) return 0;
sl@0
   512
sl@0
   513
      detail::function::function_buffer type_result;
sl@0
   514
      type_result.const_obj_ptr = &typeid(Functor);
sl@0
   515
      vtable->manager(functor, type_result, 
sl@0
   516
                      detail::function::check_functor_type_tag);
sl@0
   517
      // GCC 2.95.3 gets the CV qualifiers wrong here, so we
sl@0
   518
      // can't do the static_cast that we should do.
sl@0
   519
      return (const Functor*)(type_result.obj_ptr);
sl@0
   520
    }
sl@0
   521
sl@0
   522
  template<typename F>
sl@0
   523
    bool contains(const F& f) const
sl@0
   524
    {
sl@0
   525
#if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300)
sl@0
   526
      if (const F* fp = this->target( (F*)0 ))
sl@0
   527
#else
sl@0
   528
      if (const F* fp = this->template target<F>())
sl@0
   529
#endif
sl@0
   530
      {
sl@0
   531
        return function_equal(*fp, f);
sl@0
   532
      } else {
sl@0
   533
        return false;
sl@0
   534
      }
sl@0
   535
    }
sl@0
   536
sl@0
   537
#if defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3
sl@0
   538
  // GCC 3.3 and newer cannot copy with the global operator==, due to
sl@0
   539
  // problems with instantiation of function return types before it
sl@0
   540
  // has been verified that the argument types match up.
sl@0
   541
  template<typename Functor>
sl@0
   542
    BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
sl@0
   543
    operator==(Functor g) const
sl@0
   544
    {
sl@0
   545
      if (const Functor* fp = target<Functor>())
sl@0
   546
        return function_equal(*fp, g);
sl@0
   547
      else return false;
sl@0
   548
    }
sl@0
   549
sl@0
   550
  template<typename Functor>
sl@0
   551
    BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
sl@0
   552
    operator!=(Functor g) const
sl@0
   553
    {
sl@0
   554
      if (const Functor* fp = target<Functor>())
sl@0
   555
        return !function_equal(*fp, g);
sl@0
   556
      else return true;
sl@0
   557
    }
sl@0
   558
#endif
sl@0
   559
sl@0
   560
public: // should be protected, but GCC 2.95.3 will fail to allow access
sl@0
   561
  detail::function::vtable_base* vtable;
sl@0
   562
  mutable detail::function::function_buffer functor;
sl@0
   563
};
sl@0
   564
sl@0
   565
/**
sl@0
   566
 * The bad_function_call exception class is thrown when a boost::function
sl@0
   567
 * object is invoked
sl@0
   568
 */
sl@0
   569
class bad_function_call : public std::runtime_error
sl@0
   570
{
sl@0
   571
public:
sl@0
   572
  bad_function_call() : std::runtime_error("call to empty boost::function") {}
sl@0
   573
};
sl@0
   574
sl@0
   575
#ifndef BOOST_NO_SFINAE
sl@0
   576
inline bool operator==(const function_base& f,
sl@0
   577
                       detail::function::useless_clear_type*)
sl@0
   578
{
sl@0
   579
  return f.empty();
sl@0
   580
}
sl@0
   581
sl@0
   582
inline bool operator!=(const function_base& f,
sl@0
   583
                       detail::function::useless_clear_type*)
sl@0
   584
{
sl@0
   585
  return !f.empty();
sl@0
   586
}
sl@0
   587
sl@0
   588
inline bool operator==(detail::function::useless_clear_type*,
sl@0
   589
                       const function_base& f)
sl@0
   590
{
sl@0
   591
  return f.empty();
sl@0
   592
}
sl@0
   593
sl@0
   594
inline bool operator!=(detail::function::useless_clear_type*,
sl@0
   595
                       const function_base& f)
sl@0
   596
{
sl@0
   597
  return !f.empty();
sl@0
   598
}
sl@0
   599
#endif
sl@0
   600
sl@0
   601
#ifdef BOOST_NO_SFINAE
sl@0
   602
// Comparisons between boost::function objects and arbitrary function objects
sl@0
   603
template<typename Functor>
sl@0
   604
  inline bool operator==(const function_base& f, Functor g)
sl@0
   605
  {
sl@0
   606
    typedef mpl::bool_<(is_integral<Functor>::value)> integral;
sl@0
   607
    return detail::function::compare_equal(f, g, 0, integral());
sl@0
   608
  }
sl@0
   609
sl@0
   610
template<typename Functor>
sl@0
   611
  inline bool operator==(Functor g, const function_base& f)
sl@0
   612
  {
sl@0
   613
    typedef mpl::bool_<(is_integral<Functor>::value)> integral;
sl@0
   614
    return detail::function::compare_equal(f, g, 0, integral());
sl@0
   615
  }
sl@0
   616
sl@0
   617
template<typename Functor>
sl@0
   618
  inline bool operator!=(const function_base& f, Functor g)
sl@0
   619
  {
sl@0
   620
    typedef mpl::bool_<(is_integral<Functor>::value)> integral;
sl@0
   621
    return detail::function::compare_not_equal(f, g, 0, integral());
sl@0
   622
  }
sl@0
   623
sl@0
   624
template<typename Functor>
sl@0
   625
  inline bool operator!=(Functor g, const function_base& f)
sl@0
   626
  {
sl@0
   627
    typedef mpl::bool_<(is_integral<Functor>::value)> integral;
sl@0
   628
    return detail::function::compare_not_equal(f, g, 0, integral());
sl@0
   629
  }
sl@0
   630
#else
sl@0
   631
sl@0
   632
#  if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
sl@0
   633
// Comparisons between boost::function objects and arbitrary function
sl@0
   634
// objects. GCC 3.3 and before has an obnoxious bug that prevents this
sl@0
   635
// from working.
sl@0
   636
template<typename Functor>
sl@0
   637
  BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
sl@0
   638
  operator==(const function_base& f, Functor g)
sl@0
   639
  {
sl@0
   640
    if (const Functor* fp = f.template target<Functor>())
sl@0
   641
      return function_equal(*fp, g);
sl@0
   642
    else return false;
sl@0
   643
  }
sl@0
   644
sl@0
   645
template<typename Functor>
sl@0
   646
  BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
sl@0
   647
  operator==(Functor g, const function_base& f)
sl@0
   648
  {
sl@0
   649
    if (const Functor* fp = f.template target<Functor>())
sl@0
   650
      return function_equal(g, *fp);
sl@0
   651
    else return false;
sl@0
   652
  }
sl@0
   653
sl@0
   654
template<typename Functor>
sl@0
   655
  BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
sl@0
   656
  operator!=(const function_base& f, Functor g)
sl@0
   657
  {
sl@0
   658
    if (const Functor* fp = f.template target<Functor>())
sl@0
   659
      return !function_equal(*fp, g);
sl@0
   660
    else return true;
sl@0
   661
  }
sl@0
   662
sl@0
   663
template<typename Functor>
sl@0
   664
  BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
sl@0
   665
  operator!=(Functor g, const function_base& f)
sl@0
   666
  {
sl@0
   667
    if (const Functor* fp = f.template target<Functor>())
sl@0
   668
      return !function_equal(g, *fp);
sl@0
   669
    else return true;
sl@0
   670
  }
sl@0
   671
#  endif
sl@0
   672
sl@0
   673
template<typename Functor>
sl@0
   674
  BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
sl@0
   675
  operator==(const function_base& f, reference_wrapper<Functor> g)
sl@0
   676
  {
sl@0
   677
    if (const Functor* fp = f.template target<Functor>())
sl@0
   678
      return fp == g.get_pointer();
sl@0
   679
    else return false;
sl@0
   680
  }
sl@0
   681
sl@0
   682
template<typename Functor>
sl@0
   683
  BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
sl@0
   684
  operator==(reference_wrapper<Functor> g, const function_base& f)
sl@0
   685
  {
sl@0
   686
    if (const Functor* fp = f.template target<Functor>())
sl@0
   687
      return g.get_pointer() == fp;
sl@0
   688
    else return false;
sl@0
   689
  }
sl@0
   690
sl@0
   691
template<typename Functor>
sl@0
   692
  BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
sl@0
   693
  operator!=(const function_base& f, reference_wrapper<Functor> g)
sl@0
   694
  {
sl@0
   695
    if (const Functor* fp = f.template target<Functor>())
sl@0
   696
      return fp != g.get_pointer();
sl@0
   697
    else return true;
sl@0
   698
  }
sl@0
   699
sl@0
   700
template<typename Functor>
sl@0
   701
  BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
sl@0
   702
  operator!=(reference_wrapper<Functor> g, const function_base& f)
sl@0
   703
  {
sl@0
   704
    if (const Functor* fp = f.template target<Functor>())
sl@0
   705
      return g.get_pointer() != fp;
sl@0
   706
    else return true;
sl@0
   707
  }
sl@0
   708
sl@0
   709
#endif // Compiler supporting SFINAE
sl@0
   710
sl@0
   711
namespace detail {
sl@0
   712
  namespace function {
sl@0
   713
    inline bool has_empty_target(const function_base* f)
sl@0
   714
    {
sl@0
   715
      return f->empty();
sl@0
   716
    }
sl@0
   717
sl@0
   718
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
sl@0
   719
    inline bool has_empty_target(const void*)
sl@0
   720
    {
sl@0
   721
      return false;
sl@0
   722
    }
sl@0
   723
#else
sl@0
   724
    inline bool has_empty_target(...)
sl@0
   725
    {
sl@0
   726
      return false;
sl@0
   727
    }
sl@0
   728
#endif
sl@0
   729
  } // end namespace function
sl@0
   730
} // end namespace detail
sl@0
   731
} // end namespace boost
sl@0
   732
sl@0
   733
#undef BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL
sl@0
   734
#undef BOOST_FUNCTION_COMPARE_TYPE_ID
sl@0
   735
sl@0
   736
#endif // BOOST_FUNCTION_BASE_HEADER