Skip to content

Instantly share code, notes, and snippets.

@pwq1989
Created August 10, 2014 15:28
Show Gist options
  • Save pwq1989/4cea96d7c05fd6406522 to your computer and use it in GitHub Desktop.
Save pwq1989/4cea96d7c05fd6406522 to your computer and use it in GitHub Desktop.
@author: vczh
#pragma once
#include <functional>
#ifndef _MSC_VER
#define __thiscall
#endif
namespace vczh {
template<typename TResult, typename ...TArgs>
class YBuilder
{
private:
std::function<TResult(std::function<TResult(TArgs...)>, TArgs...)> partialLambda;
public:
YBuilder(std::function<TResult(std::function<TResult(TArgs...)>, TArgs...)> _partialLambda)
:partialLambda(_partialLambda)
{
}
TResult operator()(TArgs ...args)const
{
return partialLambda(
[this](TArgs ...args)
{
return this->operator()(args...);
}, args...);
}
};
template<typename TMethod>
struct PartialLambdaTypeRetriver
{
typedef void FunctionType;
typedef void LambdaType;
typedef void YBuilderType;
};
template<typename TClass, typename TResult, typename ...TArgs>
struct PartialLambdaTypeRetriver<TResult(__thiscall TClass::*)(std::function<TResult(TArgs...)>, TArgs...)>
{
typedef TResult FunctionType(TArgs...);
typedef TResult LambdaType(std::function<TResult(TArgs...)>, TArgs...);
typedef YBuilder<TResult, TArgs...> YBuilderType;
};
template<typename TClass, typename TResult, typename ...TArgs>
struct PartialLambdaTypeRetriver<TResult(__thiscall TClass::*)(std::function<TResult(TArgs...)>, TArgs...)const>
{
typedef TResult FunctionType(TArgs...);
typedef TResult LambdaType(std::function<TResult(TArgs...)>, TArgs...);
typedef YBuilder<TResult, TArgs...> YBuilderType;
};
template<typename TLambda>
std::function<typename PartialLambdaTypeRetriver<decltype(&TLambda::operator())>::FunctionType> Y(TLambda partialLambda)
{
return typename PartialLambdaTypeRetriver<decltype(&TLambda::operator())>::YBuilderType(partialLambda);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment