Skip to content

Instantly share code, notes, and snippets.

@lizan
Created August 25, 2019 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizan/d9fd1242c96218a0c590c7013959be9d to your computer and use it in GitHub Desktop.
Save lizan/d9fd1242c96218a0c590c7013959be9d to your computer and use it in GitHub Desktop.
Templatize parameter
#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