【发布时间】:2012-07-09 16:07:23
【问题描述】:
我正在使用 libffi,并且我使用与 std::function 类似的模板创建了一个类(即class Func<Ret (Args...)> { /* ... */};。我想转换返回类型(Ret)和每个参数类型(Args ) 到它们对应的 libffi 类型(参见this 以供参考)。到目前为止,我已经想出了这个:
// Member function of 'Func' class
Prepare(void)
{
// This vector holds all the type structures
std::vector<ffi_type*> argumentTypes{ GetFFIType<Args>()... };
ffi_type * returnType = GetFFIType<Ret>();
// Rest of the code below
// ....
}
GetFFIType函数实现如下:
template <typename T>
ffi_type * GetFFIType(void)
{
// We will check for any kind of pointer types
if(std::is_pointer<T>::value || std::is_array<T>::value ||
std::is_reference<T>::value || std::is_function<T>::value)
return &ffi_type_pointer;
if(std::is_enum<T>::value)
//return GetFFIType<std::underlying_type<T>::type>();
{
// Since the size of the enum may vary, we will identify the size
if(sizeof(T) == ffi_type_schar.size) return std::is_unsigned<T>::value ? &ffi_type_uchar : &ffi_type_schar;
if(sizeof(T) == ffi_type_sshort.size) return std::is_unsigned<T>::value ? &ffi_type_ushort : &ffi_type_sshort;
if(sizeof(T) == ffi_type_sint.size) return std::is_unsigned<T>::value ? &ffi_type_uint : &ffi_type_sint;
if(sizeof(T) == ffi_type_slong.size) return std::is_unsigned<T>::value ? &ffi_type_ulong : &ffi_type_slong;
}
assert(false && "cannot identify type");
}
// These are all of our specializations
template <> ffi_type * GetFFIType<void>(void) { return &ffi_type_void; }
template <> ffi_type * GetFFIType<byte>(void) { return &ffi_type_uchar; }
template <> ffi_type * GetFFIType<char>(void) { return &ffi_type_schar; }
template <> ffi_type * GetFFIType<ushort>(void) { return &ffi_type_ushort; }
template <> ffi_type * GetFFIType<short>(void) { return &ffi_type_sshort; }
template <> ffi_type * GetFFIType<uint>(void) { return &ffi_type_uint; }
template <> ffi_type * GetFFIType<int>(void) { return &ffi_type_sint; }
template <> ffi_type * GetFFIType<ulong>(void) { return &ffi_type_ulong; }
template <> ffi_type * GetFFIType<long>(void) { return &ffi_type_slong; }
template <> ffi_type * GetFFIType<float>(void) { return &ffi_type_float; }
template <> ffi_type * GetFFIType<double>(void) { return &ffi_type_double; }
template <> ffi_type * GetFFIType<long double>(void) { return &ffi_type_longdouble; }
这可行,但显然还有一些改进的余地。如果类型无效(即类或结构),则不会在编译时识别(使用assert 会发生运行时错误)。我该如何避免这种情况,并让这个函数在编译期间确定一个类型是否有效(原始类型)?
我也不喜欢在enums 的情况下识别基础类型的方式。我更喜欢使用 std::underlying_type<T> 代替(在代码中注释掉),但如果类型是 void 指针 (type_traits:1762:38: error: ‘void*’ is not an enumeration type),则会出现编译错误
我尝试使用 std::enable_if 实现此行为,但没有成功...请告诉我是否应该解释一些事情,以防听起来有点模糊!
总结:我想获取 GetFFIType 函数来确定编译期间的所有内容,并且该函数应该只支持原始类型(请参阅this 以获得更广泛的参考)
编辑:对不起标题,没有更好的想法:(
【问题讨论】: