dynarmic/src/common/mp.h
Lioncash 9ed9f4c565 mp: Generalize function information retrieval
Generalizes MemFnInfo to be compatible with all function types.
Also adds type introspection for arguments, as well as helper templates for the common types supported by all partial specializations.
2016-08-17 10:08:40 +01:00

69 lines
2 KiB
C++

/* This file is part of the dynarmic project.
* Copyright (c) 2016 MerryMage
* This software may be used and distributed according to the terms of the GNU
* General Public License version 2 or any later version.
*/
#pragma once
#include <tuple>
#include "common/common_types.h"
namespace Dynarmic {
namespace mp {
/// Used to provide information about an arbitrary function.
template <typename Function>
struct FunctionInfo;
/**
* Partial specialization for function types.
*
* This is used as the supporting base for all other specializations.
*/
template <typename R, typename... Args>
struct FunctionInfo<R(Args...)>
{
using return_type = R;
static constexpr size_t args_count = sizeof...(Args);
template <size_t ParameterIndex>
struct Parameter
{
static_assert(args_count != 0 && ParameterIndex < args_count - 1, "Non-existent function parameter index");
using type = std::tuple_element_t<ParameterIndex, std::tuple<Args...>>;
};
};
/// Partial specialization for function pointers
template <typename R, typename... Args>
struct FunctionInfo<R(*)(Args...)> : public FunctionInfo<R(Args...)>
{
};
/// Partial specialization for member function pointers.
template <typename C, typename R, typename... Args>
struct FunctionInfo<R(C::*)(Args...)> : public FunctionInfo<R(Args...)>
{
using class_type = C;
};
/**
* Helper template for retrieving the type of a function parameter.
*
* @tparam Function An arbitrary function type.
* @tparam ParameterIndex Zero-based index indicating which parameter to get the type of.
*/
template <typename Function, size_t ParameterIndex>
using parameter_type_t = typename FunctionInfo<Function>::template Parameter<ParameterIndex>::type;
/**
* Helper template for retrieving the return type of a function.
*
* @tparam Function The function type to get the return type of.
*/
template <typename Function>
using return_type_t = typename FunctionInfo<Function>::return_type;
} // namespace mp
} // namespace Dynarmic