Add SharedFunction to replace boost::function

This commit is contained in:
Vinnie Falco
2013-09-06 13:11:05 -07:00
parent 88af355028
commit c35843fda5
9 changed files with 140 additions and 34 deletions

View File

@@ -45,7 +45,11 @@ class SharedHandler : public SharedObject
{
protected:
typedef boost::system::error_code error_code;
#if 0
typedef boost::function <void(void)> invoked_type;
#else
typedef SharedFunction <void(void), SharedHandlerAllocator <char> > invoked_type;
#endif
SharedHandler () noexcept { }
@@ -59,19 +63,19 @@ public:
virtual void operator() (error_code const&);
virtual void operator() (error_code const&, std::size_t);
/** Dispatch the Function on our context. */
/*
template <typename Dispatcher, typename Function>
void dispatch (Dispatcher& dispatcher, BOOST_ASIO_MOVE_ARG(Function) function)
{
dispatcher.dispatch (boost::bind (
&SharedHandler::invoke <Function>, this,
BOOST_ASIO_MOVE_CAST(Function)(function)));
}
*/
template <typename Function>
void invoke (BOOST_ASIO_MOVE_ARG(Function) f);
void invoke (BOOST_ASIO_MOVE_ARG(Function) f)
#if 0
;
#else
{
// The allocator will hold a reference to the SharedHandler
// so that we can safely destroy the function object.
invoked_type invoked (f,
SharedHandlerAllocator <char> (this));
invoke (invoked);
}
#endif
virtual void invoke (invoked_type& invoked) = 0;
virtual void* allocate (std::size_t size) = 0;

View File

@@ -112,18 +112,16 @@ private:
//------------------------------------------------------------------------------
#if 0
template <typename Function>
void SharedHandler::invoke (BOOST_ASIO_MOVE_ARG(Function) f)
{
#if BEAST_USE_HANDLER_ALLOCATIONS
// The allocator will hold a reference to the SharedHandler
// so that we can safely destroy the function object.
invoked_type invoked (BOOST_ASIO_MOVE_CAST(Function)(f),
SharedHandlerAllocator <char> (this));
#else
invoked_type invoked (BOOST_ASIO_MOVE_CAST(Function)(f));
#endif
invoke (invoked);
}
#endif
#endif

View File

@@ -215,6 +215,26 @@ inline bool asio_handler_is_continuation (SharedHandlerPtr* ptr)
//
//--------------------------------------------------------------------------
// void(error_code)
template <typename Handler>
SharedHandlerPtr newErrorHandler (
BOOST_ASIO_MOVE_ARG(Handler) handler)
{
return newSharedHandlerContainer <ErrorSharedHandlerType> (
BOOST_ASIO_MOVE_CAST(Handler)(handler));
}
// void(error_code, size_t)
template <typename Handler>
SharedHandlerPtr newTransferHandler (
BOOST_ASIO_MOVE_ARG(Handler) handler)
{
return newSharedHandlerContainer <TransferSharedHandlerType> (
BOOST_ASIO_MOVE_CAST(Handler)(handler));
}
//--------------------------------------------------------------------------
// CompletionHandler
//
// http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/CompletionHandler.html

View File

@@ -87,19 +87,14 @@ protected:
//
void destroy ()
{
#if BEAST_USE_HANDLER_ALLOCATIONS
Handler local (BOOST_ASIO_MOVE_CAST(Handler)(m_handler));
std::size_t const size (m_size);
SharedHandler* const shared (static_cast <SharedHandler*>(this));
shared->~SharedHandler ();
boost_asio_handler_alloc_helpers::
deallocate <Handler> (shared, size, local);
#else
delete this;
#endif
}
#if BEAST_USE_HANDLER_ALLOCATIONS
// If these somehow get called, bad things will happen
//
void* operator new (std::size_t)
@@ -111,7 +106,6 @@ protected:
{
return pure_virtual_called (__FILE__, __LINE__);
}
#endif
protected:
std::size_t const m_size;
@@ -219,15 +213,10 @@ Container <Handler>* newSharedHandlerContainer (BOOST_ASIO_MOVE_ARG(Handler) han
{
typedef Container <Handler> ContainerType;
std::size_t const size (sizeof (ContainerType));
#if BEAST_USE_HANDLER_ALLOCATIONS
Handler local (BOOST_ASIO_MOVE_CAST(Handler)(handler));
void* const p (boost_asio_handler_alloc_helpers::
allocate <Handler> (size, local));
return ::new (p) ContainerType (size, BOOST_ASIO_MOVE_CAST(Handler)(local));
#else
void* const p = ::operator new (size);
return ::new (p) ContainerType (size, BOOST_ASIO_MOVE_CAST(Handler)(handler));
#endif
}
#endif

View File

@@ -53,13 +53,6 @@
# define BEAST_SOCKET_VIRTUAL
#endif
// A potentially dangerous but powerful feature which
// might need to be turned off to see if it fixes anything.
//
#ifndef BEAST_USE_HANDLER_ALLOCATIONS
# define BEAST_USE_HANDLER_ALLOCATIONS 1
#endif
namespace beast
{