#ifndef ScopedLambda_h
#define ScopedLambda_h
namespace WTF {
template<typename FunctionType> class ScopedLambda;
template<typename ResultType, typename... ArgumentTypes>
class ScopedLambda<ResultType (ArgumentTypes...)> {
public:
ScopedLambda(ResultType (*impl)(void* arg, ArgumentTypes...) = nullptr, void* arg = nullptr)
: m_impl(impl)
, m_arg(arg)
{
}
template<typename... PassedArgumentTypes>
ResultType operator()(PassedArgumentTypes&&... arguments) const
{
return m_impl(m_arg, std::forward<PassedArgumentTypes>(arguments)...);
}
private:
ResultType (*m_impl)(void* arg, ArgumentTypes...);
void *m_arg;
};
template<typename FunctionType, typename Functor> class ScopedLambdaFunctor;
template<typename ResultType, typename... ArgumentTypes, typename Functor>
class ScopedLambdaFunctor<ResultType (ArgumentTypes...), Functor> : public ScopedLambda<ResultType (ArgumentTypes...)> {
public:
template<typename PassedFunctor>
ScopedLambdaFunctor(PassedFunctor&& functor)
: ScopedLambda<ResultType (ArgumentTypes...)>(implFunction, this)
, m_functor(std::forward<PassedFunctor>(functor))
{
}
ScopedLambdaFunctor(const ScopedLambdaFunctor& other)
: ScopedLambda<ResultType (ArgumentTypes...)>(implFunction, this)
, m_functor(other.m_functor)
{
}
ScopedLambdaFunctor(ScopedLambdaFunctor&& other)
: ScopedLambda<ResultType (ArgumentTypes...)>(implFunction, this)
, m_functor(WTFMove(other.m_functor))
{
}
ScopedLambdaFunctor& operator=(const ScopedLambdaFunctor& other)
{
m_functor = other.m_functor;
return *this;
}
ScopedLambdaFunctor& operator=(ScopedLambdaFunctor&& other)
{
m_functor = WTFMove(other.m_functor);
return *this;
}
private:
static ResultType implFunction(void* argument, ArgumentTypes... arguments)
{
return static_cast<ScopedLambdaFunctor*>(argument)->m_functor(arguments...);
}
Functor m_functor;
};
template<typename FunctionType, typename Functor>
ScopedLambdaFunctor<FunctionType, Functor> scopedLambda(const Functor& functor)
{
return ScopedLambdaFunctor<FunctionType, Functor>(functor);
}
template<typename FunctionType, typename Functor>
ScopedLambdaFunctor<FunctionType, Functor> scopedLambda(Functor&& functor)
{
return ScopedLambdaFunctor<FunctionType, Functor>(WTFMove(functor));
}
template<typename FunctionType, typename Functor> class ScopedLambdaRefFunctor;
template<typename ResultType, typename... ArgumentTypes, typename Functor>
class ScopedLambdaRefFunctor<ResultType (ArgumentTypes...), Functor> : public ScopedLambda<ResultType (ArgumentTypes...)> {
public:
ScopedLambdaRefFunctor(const Functor& functor)
: ScopedLambda<ResultType (ArgumentTypes...)>(implFunction, this)
, m_functor(&functor)
{
}
ScopedLambdaRefFunctor(const ScopedLambdaRefFunctor& other)
: ScopedLambda<ResultType (ArgumentTypes...)>(implFunction, this)
, m_functor(other.m_functor)
{
}
ScopedLambdaRefFunctor(ScopedLambdaRefFunctor&& other)
: ScopedLambda<ResultType (ArgumentTypes...)>(implFunction, this)
, m_functor(other.m_functor)
{
}
ScopedLambdaRefFunctor& operator=(const ScopedLambdaRefFunctor& other)
{
m_functor = other.m_functor;
return *this;
}
ScopedLambdaRefFunctor& operator=(ScopedLambdaRefFunctor&& other)
{
m_functor = other.m_functor;
return *this;
}
private:
static ResultType implFunction(void* argument, ArgumentTypes... arguments)
{
return (*static_cast<ScopedLambdaRefFunctor*>(argument)->m_functor)(arguments...);
}
const Functor* m_functor;
};
template<typename FunctionType, typename Functor>
ScopedLambdaRefFunctor<FunctionType, Functor> scopedLambdaRef(const Functor& functor)
{
return ScopedLambdaRefFunctor<FunctionType, Functor>(functor);
}
}
using WTF::ScopedLambda;
using WTF::scopedLambda;
using WTF::scopedLambdaRef;
#endif // ScopedLambda_h