SharedFunction improvements

This commit is contained in:
Vinnie Falco
2013-09-14 18:18:13 -07:00
parent 277e32bb1e
commit 50965ca9c0

View File

@@ -25,6 +25,8 @@
template <typename Signature, class Allocator = std::allocator <char> >
class SharedFunction;
//------------------------------------------------------------------------------
// nullary function
//
template <typename R, class A>
@@ -61,6 +63,10 @@ public:
//--------------------------------------------------------------------------
SharedFunction ()
{
}
template <typename F>
SharedFunction (F f, A a = A ())
: m_ptr (::new (
@@ -86,8 +92,15 @@ public:
return *this;
}
R operator() ()
bool empty () const
{
return m_ptr == nullptr;
}
R operator() () const
{
bassert (! empty());
return (*m_ptr)();
}
@@ -95,5 +108,88 @@ private:
SharedPtr <Call> m_ptr;
};
//------------------------------------------------------------------------------
// unary function (arity 1)
//
template <typename R, typename P1, class A>
class SharedFunction <R (P1), A>
{
public:
class Call : public SharedObject
{
public:
virtual R operator() (P1 p1) = 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 a = A ())
: m_f (BEAST_MOVE_CAST(F)(f))
, m_a (a)
{
}
R operator() (P1 p1)
{
return m_f (p1);
}
private:
F m_f;
Allocator m_a;
};
//--------------------------------------------------------------------------
SharedFunction ()
{
}
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;
}
bool empty () const
{
return m_ptr == nullptr;
}
R operator() (P1 p1) const
{
bassert (! empty());
return (*m_ptr)(p1);
}
private:
SharedPtr <Call> m_ptr;
};
#endif