Add SharedFunction to replace boost::function

This commit is contained in:
Vinnie Falco committed 2013-09-07 11:53:36 -07:00
1 parent 88af355028
commit c35843fda5
9 files changed
+140 -34

No files matched your search

+16 -12
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;
@@ -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
@@ -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
@@ -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