2001-06-22 14:13:03 +00:00
|
|
|
// Boost.Function library
|
|
|
|
|
2002-08-09 14:34:23 +00:00
|
|
|
// Copyright (C) 2001, 2002 Doug Gregor (gregod@cs.rpi.edu)
|
2001-06-21 16:19:33 +00:00
|
|
|
//
|
|
|
|
// Permission to copy, use, sell and distribute this software is granted
|
|
|
|
// provided this copyright notice appears in all copies.
|
|
|
|
// Permission to modify the code and to distribute modified code is granted
|
|
|
|
// provided this copyright notice appears in all copies, and a notice
|
|
|
|
// that the code was modified is included with the copyright notice.
|
|
|
|
//
|
|
|
|
// This software is provided "as is" without express or implied warranty,
|
|
|
|
// and with no claim as to its suitability for any purpose.
|
2002-09-24 17:28:58 +00:00
|
|
|
|
2001-06-21 16:19:33 +00:00
|
|
|
// For more information, see http://www.boost.org
|
|
|
|
|
|
|
|
#ifndef BOOST_FUNCTION_BASE_HEADER
|
|
|
|
#define BOOST_FUNCTION_BASE_HEADER
|
|
|
|
|
|
|
|
#include <stdexcept>
|
2002-11-04 18:19:01 +00:00
|
|
|
#include <string>
|
2001-06-21 16:19:33 +00:00
|
|
|
#include <memory>
|
2001-10-08 13:32:24 +00:00
|
|
|
#include <new>
|
2001-06-21 16:19:33 +00:00
|
|
|
#include <boost/config.hpp>
|
2002-09-24 17:16:17 +00:00
|
|
|
#include <boost/type_traits/arithmetic_traits.hpp>
|
|
|
|
#include <boost/type_traits/composite_traits.hpp>
|
|
|
|
#include <boost/type_traits/is_stateless.hpp>
|
2001-12-03 16:28:33 +00:00
|
|
|
#include <boost/ref.hpp>
|
2002-08-05 06:19:17 +00:00
|
|
|
#include <boost/pending/ct_if.hpp>
|
2001-06-21 16:19:33 +00:00
|
|
|
|
2002-11-09 16:02:47 +00:00
|
|
|
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 || defined(__ICL) && __ICL <= 600 || defined(__MWERKS__) && __MWERKS__ < 0x2406 && !defined(BOOST_NO_CONFIG)
|
2001-12-24 17:10:47 +00:00
|
|
|
# define BOOST_FUNCTION_TARGET_FIX(x) x
|
2001-12-05 22:35:32 +00:00
|
|
|
#else
|
2001-12-24 17:10:47 +00:00
|
|
|
# define BOOST_FUNCTION_TARGET_FIX(x)
|
2001-12-05 22:35:32 +00:00
|
|
|
#endif // not MSVC
|
|
|
|
|
2002-11-09 16:02:47 +00:00
|
|
|
#if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730 && !defined(BOOST_NO_CONFIG)
|
2002-09-25 18:10:32 +00:00
|
|
|
// Work around a compiler bug.
|
2002-09-25 20:02:31 +00:00
|
|
|
// boost::python::objects::function has to be seen by the compiler before the
|
2002-09-25 18:10:32 +00:00
|
|
|
// boost::function class template.
|
2002-09-25 20:02:31 +00:00
|
|
|
namespace boost { namespace python { namespace objects {
|
2002-09-25 18:10:32 +00:00
|
|
|
class function;
|
|
|
|
}}}
|
|
|
|
#endif
|
|
|
|
|
2002-11-09 16:02:47 +00:00
|
|
|
// GCC 3.2 doesn't seem to support enable_if, so we assume that
|
|
|
|
// earlier versions have the same limitation
|
|
|
|
#if defined(__GNUC__) && __GNUC__ < 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ <= 2 ) && !(BOOST_NO_CONFIG)
|
|
|
|
# define BOOST_FUNCTION_NO_ENABLE_IF
|
|
|
|
#endif
|
2002-11-09 17:16:24 +00:00
|
|
|
|
|
|
|
// MIPSpro 7.3.1.3m doesn't support enable_if
|
2002-11-09 16:26:47 +00:00
|
|
|
#if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730 && !defined(BOOST_NO_CONFIG)
|
|
|
|
# define BOOST_FUNCTION_NO_ENABLE_IF
|
|
|
|
#endif
|
2002-11-09 16:02:47 +00:00
|
|
|
|
2002-11-09 21:05:46 +00:00
|
|
|
// MSVC 7.0 doesn't support enable_if
|
|
|
|
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 && !defined(BOOST_NO_CONFIG)
|
2002-11-09 17:16:24 +00:00
|
|
|
# define BOOST_FUNCTION_NO_ENABLE_IF
|
|
|
|
#endif
|
|
|
|
|
2002-11-15 19:26:39 +00:00
|
|
|
// Borland C++ 5.6.0 doesn't support enable_if
|
|
|
|
#if defined(__BORLANDC__) && __BORLANDC__ <= 0x560 && !defined(BOOST_NO_CONFIG)
|
2002-11-09 17:16:24 +00:00
|
|
|
# define BOOST_FUNCTION_NO_ENABLE_IF
|
|
|
|
#endif
|
|
|
|
|
2002-11-13 21:00:23 +00:00
|
|
|
// Metrowerks 7.2 doesn't support enable_if
|
|
|
|
#if defined(__MWERKS__) && __MWERKS__ <= 0x2407 && !defined(BOOST_NO_CONFIG)
|
|
|
|
# define BOOST_FUNCTION_NO_ENABLE_IF
|
|
|
|
#endif
|
|
|
|
|
2002-12-04 15:54:33 +00:00
|
|
|
#if defined(__SUNPRO_CC) && __SUNPRO_CC <= 0x540 && !defined(BOOST_STRICT_CONFIG)
|
|
|
|
# define BOOST_FUNCTION_NO_ENABLE_IF
|
|
|
|
#endif
|
|
|
|
|
2002-09-24 17:16:17 +00:00
|
|
|
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
|
|
|
|
namespace boost {
|
|
|
|
|
|
|
|
template<typename Signature, typename Allocator = std::allocator<void> >
|
|
|
|
class function;
|
|
|
|
|
|
|
|
template<typename Signature, typename Allocator>
|
2002-09-24 17:28:58 +00:00
|
|
|
inline void swap(function<Signature, Allocator>& f1,
|
2002-09-24 17:16:17 +00:00
|
|
|
function<Signature, Allocator>& f2)
|
|
|
|
{
|
|
|
|
f1.swap(f2);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace boost
|
|
|
|
#endif // have partial specialization
|
2002-08-05 03:08:08 +00:00
|
|
|
|
2001-06-21 16:19:33 +00:00
|
|
|
namespace boost {
|
|
|
|
namespace detail {
|
|
|
|
namespace function {
|
|
|
|
/**
|
|
|
|
* A union of a function pointer and a void pointer. This is necessary
|
2002-09-24 17:28:58 +00:00
|
|
|
* because 5.2.10/6 allows reinterpret_cast<> to safely cast between
|
2001-06-21 16:19:33 +00:00
|
|
|
* 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. */
|
2002-09-24 17:28:58 +00:00
|
|
|
union any_pointer
|
2001-06-21 16:19:33 +00:00
|
|
|
{
|
|
|
|
void* obj_ptr;
|
2001-10-08 13:32:24 +00:00
|
|
|
const void* const_obj_ptr;
|
2001-06-21 16:19:33 +00:00
|
|
|
void (*func_ptr)();
|
2002-09-24 17:28:58 +00:00
|
|
|
char data[1];
|
2001-06-21 16:19:33 +00:00
|
|
|
};
|
|
|
|
|
2002-09-24 19:27:51 +00:00
|
|
|
inline any_pointer make_any_pointer(void* o)
|
2002-09-24 17:16:17 +00:00
|
|
|
{
|
2002-09-24 17:28:58 +00:00
|
|
|
any_pointer p;
|
|
|
|
p.obj_ptr = o;
|
|
|
|
return p;
|
2002-09-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
2002-09-24 19:27:51 +00:00
|
|
|
inline any_pointer make_any_pointer(const void* o)
|
2002-09-24 17:16:17 +00:00
|
|
|
{
|
2002-09-24 17:28:58 +00:00
|
|
|
any_pointer p;
|
|
|
|
p.const_obj_ptr = o;
|
|
|
|
return p;
|
2002-09-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
2002-09-24 19:27:51 +00:00
|
|
|
inline any_pointer make_any_pointer(void (*f)())
|
2002-09-24 17:16:17 +00:00
|
|
|
{
|
2002-09-24 17:28:58 +00:00
|
|
|
any_pointer p;
|
|
|
|
p.func_ptr = f;
|
|
|
|
return p;
|
2002-09-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
2001-06-21 16:19:33 +00:00
|
|
|
/**
|
|
|
|
* The unusable class is a placeholder for unused function arguments
|
|
|
|
* It is also completely unusable except that it constructable from
|
|
|
|
* anything. This helps compilers without partial specialization to
|
|
|
|
* handle Boost.Function objects returning void.
|
|
|
|
*/
|
2002-09-24 17:28:58 +00:00
|
|
|
struct unusable
|
2001-06-21 16:19:33 +00:00
|
|
|
{
|
|
|
|
unusable() {}
|
|
|
|
template<typename T> unusable(const T&) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Determine the return type. This supports compilers that do not support
|
|
|
|
* void returns or partial specialization by silently changing the return
|
2002-09-24 17:28:58 +00:00
|
|
|
* type to "unusable".
|
2001-06-21 16:19:33 +00:00
|
|
|
*/
|
|
|
|
template<typename T> struct function_return_type { typedef T type; };
|
|
|
|
|
2002-09-24 17:28:58 +00:00
|
|
|
template<>
|
|
|
|
struct function_return_type<void>
|
2001-06-21 16:19:33 +00:00
|
|
|
{
|
|
|
|
typedef unusable type;
|
|
|
|
};
|
|
|
|
|
|
|
|
// The operation type to perform on the given functor/function pointer
|
2002-09-24 17:28:58 +00:00
|
|
|
enum functor_manager_operation_type {
|
|
|
|
clone_functor_tag,
|
2001-11-27 23:11:44 +00:00
|
|
|
destroy_functor_tag
|
2001-10-08 13:32:24 +00:00
|
|
|
};
|
2001-06-21 16:19:33 +00:00
|
|
|
|
2001-10-16 19:23:37 +00:00
|
|
|
// Tags used to decide between different types of functions
|
2001-06-21 16:19:33 +00:00
|
|
|
struct function_ptr_tag {};
|
|
|
|
struct function_obj_tag {};
|
2001-10-16 19:23:37 +00:00
|
|
|
struct member_ptr_tag {};
|
2001-12-03 16:28:33 +00:00
|
|
|
struct function_obj_ref_tag {};
|
2002-01-30 01:59:49 +00:00
|
|
|
struct stateless_function_obj_tag {};
|
2001-10-16 19:23:37 +00:00
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
class get_function_tag
|
|
|
|
{
|
2002-04-10 04:10:06 +00:00
|
|
|
typedef typename ct_if<(is_pointer<F>::value),
|
2001-10-16 19:23:37 +00:00
|
|
|
function_ptr_tag,
|
|
|
|
function_obj_tag>::type ptr_or_obj_tag;
|
|
|
|
|
2002-04-10 04:10:06 +00:00
|
|
|
typedef typename ct_if<(is_member_pointer<F>::value),
|
2002-01-17 15:57:13 +00:00
|
|
|
member_ptr_tag,
|
|
|
|
ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag;
|
2002-01-30 01:59:49 +00:00
|
|
|
|
2002-04-10 04:10:06 +00:00
|
|
|
typedef typename ct_if<(is_reference_wrapper<F>::value),
|
2002-01-17 15:57:13 +00:00
|
|
|
function_obj_ref_tag,
|
2002-01-30 01:59:49 +00:00
|
|
|
ptr_or_obj_or_mem_tag>::type or_ref_tag;
|
|
|
|
|
|
|
|
public:
|
2002-04-10 04:10:06 +00:00
|
|
|
typedef typename ct_if<(is_stateless<F>::value),
|
2002-01-30 01:59:49 +00:00
|
|
|
stateless_function_obj_tag,
|
|
|
|
or_ref_tag>::type type;
|
2001-10-16 19:23:37 +00:00
|
|
|
};
|
2001-06-21 16:19:33 +00:00
|
|
|
|
2001-12-03 16:28:33 +00:00
|
|
|
// The trivial manager does nothing but return the same pointer (if we
|
|
|
|
// are cloning) or return the null pointer (if we are deleting).
|
2002-09-24 17:28:58 +00:00
|
|
|
inline any_pointer trivial_manager(any_pointer f,
|
2002-05-10 17:54:40 +00:00
|
|
|
functor_manager_operation_type op)
|
2001-12-03 16:28:33 +00:00
|
|
|
{
|
2002-01-17 15:57:13 +00:00
|
|
|
if (op == clone_functor_tag)
|
|
|
|
return f;
|
|
|
|
else
|
2002-09-24 17:16:17 +00:00
|
|
|
return make_any_pointer(reinterpret_cast<void*>(0));
|
2001-12-03 16:28:33 +00:00
|
|
|
}
|
|
|
|
|
2001-06-21 16:19:33 +00:00
|
|
|
/**
|
|
|
|
* The functor_manager class contains a static function "manage" which
|
2002-09-24 17:28:58 +00:00
|
|
|
* can clone or destroy the given function/function object pointer.
|
2001-06-21 16:19:33 +00:00
|
|
|
*/
|
|
|
|
template<typename Functor, typename Allocator>
|
|
|
|
struct functor_manager
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
typedef Functor functor_type;
|
|
|
|
|
|
|
|
// For function pointers, the manager is trivial
|
|
|
|
static inline any_pointer
|
2002-09-24 17:28:58 +00:00
|
|
|
manager(any_pointer function_ptr,
|
2002-05-10 17:54:40 +00:00
|
|
|
functor_manager_operation_type op,
|
2001-06-21 16:19:33 +00:00
|
|
|
function_ptr_tag)
|
|
|
|
{
|
2001-11-27 23:11:44 +00:00
|
|
|
if (op == clone_functor_tag)
|
2001-06-21 16:19:33 +00:00
|
|
|
return function_ptr;
|
2001-10-08 13:32:24 +00:00
|
|
|
else
|
2002-09-24 17:16:17 +00:00
|
|
|
return make_any_pointer(static_cast<void (*)()>(0));
|
2001-11-19 20:30:18 +00:00
|
|
|
}
|
|
|
|
|
2002-09-24 17:28:58 +00:00
|
|
|
// For function object pointers, we clone the pointer to each
|
2001-06-21 16:19:33 +00:00
|
|
|
// function has its own version.
|
|
|
|
static inline any_pointer
|
2002-09-24 17:28:58 +00:00
|
|
|
manager(any_pointer function_obj_ptr,
|
2001-06-21 16:19:33 +00:00
|
|
|
functor_manager_operation_type op,
|
|
|
|
function_obj_tag)
|
|
|
|
{
|
2001-11-19 20:30:18 +00:00
|
|
|
#ifndef BOOST_NO_STD_ALLOCATOR
|
2002-09-24 17:28:58 +00:00
|
|
|
typedef typename Allocator::template rebind<functor_type>::other
|
2001-11-19 20:30:18 +00:00
|
|
|
allocator_type;
|
|
|
|
typedef typename allocator_type::pointer pointer_type;
|
|
|
|
#else
|
|
|
|
typedef functor_type* pointer_type;
|
|
|
|
#endif // BOOST_NO_STD_ALLOCATOR
|
|
|
|
|
2001-06-21 16:19:33 +00:00
|
|
|
# ifndef BOOST_NO_STD_ALLOCATOR
|
|
|
|
allocator_type allocator;
|
|
|
|
# endif // BOOST_NO_STD_ALLOCATOR
|
|
|
|
|
2001-11-27 23:11:44 +00:00
|
|
|
if (op == clone_functor_tag) {
|
2002-09-24 17:28:58 +00:00
|
|
|
functor_type* f =
|
2001-06-21 16:19:33 +00:00
|
|
|
static_cast<functor_type*>(function_obj_ptr.obj_ptr);
|
|
|
|
|
|
|
|
// Clone the functor
|
|
|
|
# ifndef BOOST_NO_STD_ALLOCATOR
|
|
|
|
pointer_type copy = allocator.allocate(1);
|
|
|
|
allocator.construct(copy, *f);
|
|
|
|
|
|
|
|
// Get back to the original pointer type
|
|
|
|
functor_type* new_f = static_cast<functor_type*>(copy);
|
|
|
|
# else
|
|
|
|
functor_type* new_f = new functor_type(*f);
|
|
|
|
# endif // BOOST_NO_STD_ALLOCATOR
|
2002-09-24 17:16:17 +00:00
|
|
|
return make_any_pointer(static_cast<void*>(new_f));
|
2001-06-21 16:19:33 +00:00
|
|
|
}
|
2001-11-19 20:30:18 +00:00
|
|
|
else {
|
2001-06-21 16:19:33 +00:00
|
|
|
/* Cast from the void pointer to the functor pointer type */
|
2002-09-24 17:28:58 +00:00
|
|
|
functor_type* f =
|
2001-06-29 19:33:29 +00:00
|
|
|
reinterpret_cast<functor_type*>(function_obj_ptr.obj_ptr);
|
2001-06-21 16:19:33 +00:00
|
|
|
|
|
|
|
# ifndef BOOST_NO_STD_ALLOCATOR
|
2001-06-29 19:33:29 +00:00
|
|
|
/* Cast from the functor pointer type to the allocator's pointer
|
|
|
|
type */
|
|
|
|
pointer_type victim = static_cast<pointer_type>(f);
|
2001-06-21 16:19:33 +00:00
|
|
|
|
2001-06-29 19:33:29 +00:00
|
|
|
// Destroy and deallocate the functor
|
|
|
|
allocator.destroy(victim);
|
|
|
|
allocator.deallocate(victim, 1);
|
2001-06-21 16:19:33 +00:00
|
|
|
# else
|
2001-06-29 19:33:29 +00:00
|
|
|
delete f;
|
2001-06-21 16:19:33 +00:00
|
|
|
# endif // BOOST_NO_STD_ALLOCATOR
|
|
|
|
|
2002-09-24 17:16:17 +00:00
|
|
|
return make_any_pointer(static_cast<void*>(0));
|
2001-06-21 16:19:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2001-11-19 20:30:18 +00:00
|
|
|
typedef typename get_function_tag<functor_type>::type tag_type;
|
2001-06-21 16:19:33 +00:00
|
|
|
return manager(functor_ptr, op, tag_type());
|
|
|
|
}
|
|
|
|
};
|
2002-11-09 16:02:47 +00:00
|
|
|
|
2002-12-04 15:54:33 +00:00
|
|
|
# if defined(BOOST_STRICT_CONFIG) || !defined(__HP_aCC) || __HP_aCC > 33900
|
2002-11-09 16:02:47 +00:00
|
|
|
template<bool>
|
2002-12-04 15:54:33 +00:00
|
|
|
# else
|
|
|
|
template<bool x>
|
|
|
|
# endif
|
2002-11-09 16:02:47 +00:00
|
|
|
struct enabled
|
|
|
|
{
|
|
|
|
template<typename T>
|
|
|
|
struct base
|
|
|
|
{
|
|
|
|
typedef T type;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct enabled<false>
|
|
|
|
{
|
|
|
|
template<typename T>
|
|
|
|
struct base
|
|
|
|
{
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<bool Enabled, typename T>
|
|
|
|
struct enable_if : public enabled<Enabled>::template base<T>
|
|
|
|
{
|
|
|
|
};
|
2001-06-21 16:19:33 +00:00
|
|
|
} // end namespace function
|
|
|
|
} // end namespace detail
|
|
|
|
|
2002-11-04 18:19:01 +00:00
|
|
|
/**
|
|
|
|
* The function_base class contains the basic elements needed for the
|
|
|
|
* function1, function2, function3, etc. classes. It is common to all
|
|
|
|
* functions (and as such can be used to tell if we have one of the
|
|
|
|
* functionN objects).
|
|
|
|
*/
|
|
|
|
class function_base
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
function_base() : manager(0)
|
2001-06-21 16:19:33 +00:00
|
|
|
{
|
2002-11-04 18:19:01 +00:00
|
|
|
functor.obj_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this function empty?
|
|
|
|
bool empty() const { return !manager; }
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The bad_function_call exception class is thrown when a boost::function
|
|
|
|
* object is invoked
|
|
|
|
*/
|
|
|
|
class bad_function_call : public std::runtime_error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bad_function_call() : std::runtime_error("call to empty boost::function") {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Poison comparison between Boost.Function objects (because it is
|
|
|
|
* meaningless). The comparisons would otherwise be allowed because of the
|
|
|
|
* conversion required to allow syntax such as:
|
|
|
|
* boost::function<int, int> f;
|
|
|
|
* if (f) { f(5); }
|
|
|
|
*/
|
|
|
|
void operator==(const function_base&, const function_base&);
|
|
|
|
void operator!=(const function_base&, const function_base&);
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
namespace function {
|
|
|
|
inline bool has_empty_target(const function_base* f)
|
2002-09-24 17:16:17 +00:00
|
|
|
{
|
2002-11-04 18:19:01 +00:00
|
|
|
return f->empty();
|
2002-09-24 17:16:17 +00:00
|
|
|
}
|
2002-09-24 17:28:58 +00:00
|
|
|
|
2002-11-04 18:19:01 +00:00
|
|
|
inline bool has_empty_target(...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} // end namespace function
|
|
|
|
} // end namespace detail
|
|
|
|
} // end namespace boost
|
2001-06-21 16:19:33 +00:00
|
|
|
|
|
|
|
#endif // BOOST_FUNCTION_BASE_HEADER
|