Created
August 25, 2019 01:57
-
-
Save lizan/d9fd1242c96218a0c590c7013959be9d to your computer and use it in GitHub Desktop.
Templatize parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <functional> | |
#include <type_traits> | |
class Context; | |
class Word; | |
template <size_t N, class ReturnType, class ContextType, class ParamType, | |
class FuncBase = ReturnType(ContextType)> | |
struct WasmFuncTypeHelper {}; | |
template <size_t N, class ReturnType, class ContextType, class ParamType, class... Args> | |
struct WasmFuncTypeHelper<N, ReturnType, ContextType, ParamType, ReturnType(ContextType, Args...)> { | |
using type = typename WasmFuncTypeHelper<N - 1, ReturnType, ContextType, ParamType, | |
ReturnType(ContextType, Args..., ParamType)>::type; | |
}; | |
template <class ReturnType, class ContextType, class ParamType, class... Args> | |
struct WasmFuncTypeHelper<0, ReturnType, ContextType, ParamType, ReturnType(ContextType, Args...)> { | |
using type = ReturnType(ContextType, Args...); | |
}; | |
template <size_t N, class ReturnType, class ContextType, class ParamType> | |
using WasmFuncType = typename WasmFuncTypeHelper<N, ReturnType, ContextType, ParamType>::type; | |
template <size_t N> using WasmCallVoid = std::function<WasmFuncType<N, void, Context*, Word>>; | |
template <size_t N> using WasmCallWord = std::function<WasmFuncType<N, Word, Context*, Word>>; | |
template <size_t N> using WasmCallbackVoid = WasmFuncType<N, void, void*, Word>*; | |
template <size_t N> using WasmCallbackWord = WasmFuncType<N, Word, void*, Word>*; | |
static_assert(std::is_same<WasmCallVoid<3>, std::function<void(Context*, Word, Word, Word)>>::value, | |
""); | |
static_assert(std::is_same<WasmCallWord<3>, std::function<Word(Context*, Word, Word, Word)>>::value, | |
""); | |
static_assert(std::is_same<WasmCallbackVoid<3>, void (*)(void*, Word, Word, Word)>::value, ""); | |
static_assert(std::is_same<WasmCallbackWord<3>, Word (*)(void*, Word, Word, Word)>::value, ""); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment