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

@@ -163,6 +163,7 @@
<ClInclude Include="..\..\modules\beast_core\files\beast_RandomAccessFile.h" />
<ClInclude Include="..\..\modules\beast_core\files\beast_TemporaryFile.h" />
<ClInclude Include="..\..\modules\beast_core\functional\beast_Function.h" />
<ClInclude Include="..\..\modules\beast_core\functional\SharedFunction.h" />
<ClInclude Include="..\..\modules\beast_core\json\beast_JSON.h" />
<ClInclude Include="..\..\modules\beast_core\logging\beast_FileLogger.h" />
<ClInclude Include="..\..\modules\beast_core\logging\beast_Logger.h" />

View File

@@ -992,6 +992,9 @@
<ClInclude Include="..\..\modules\beast_asio\protocol\UniformResourceLocator.h">
<Filter>beast_asio\protocol</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\functional\SharedFunction.h">
<Filter>beast_core\functional</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\beast_core.cpp">

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
{

View File

@@ -408,6 +408,7 @@ extern BEAST_API void BEAST_CALLTYPE logAssertion (char const* file, int line) n
#include "diagnostic/MeasureFunctionCallTime.h"
#include "functional/beast_Function.h"
#include "functional/SharedFunction.h"
#include "thread/beast_DeadlineTimer.h"

View File

@@ -0,0 +1,97 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_CORE_SHAREDFUNCTION_H_INCLUDED
#define BEAST_CORE_SHAREDFUNCTION_H_INCLUDED
/** A reference counted, abstract function object.
*/
template <typename Signature, class Allocator = std::allocator <char> >
class SharedFunction;
// nullary function
//
template <typename R, class A>
class SharedFunction <R (void), A>
{
public:
template <typename F>
SharedFunction (F f, A a = A ())
: m_ptr (::new (
typename CallType <F>::Allocator (a)
.allocate (sizeof (CallType <F>)))
CallType <F> (BEAST_MOVE_CAST(F)(f), a))
{
}
SharedFunction (SharedFunction const& other)
: m_ptr (other.m_ptr)
{
}
SharedFunction (SharedFunction const& other, A)
: m_ptr (other.m_ptr)
{
}
SharedFunction& operator= (SharedFunction const& other)
{
m_ptr = other.m_ptr;
return *this;
}
R operator() ()
{
return (*m_ptr)();
}
private:
class Call : public SharedObject
{
public:
virtual R operator() () = 0;
};
template <typename F>
class CallType : public Call
{
public:
typedef typename A:: template rebind <CallType <F> >::other Allocator;
CallType (BEAST_MOVE_ARG(F) f, A const& a)
: m_f (BEAST_MOVE_CAST(F)(f))
, m_a (a)
{
}
R operator() ()
{
return m_f ();
}
private:
F m_f;
Allocator m_a;
};
SharedPtr <Call> m_ptr;
};
#endif