C++ 在不同类型的样本类型之间转换
我想使用一个模板在不同的样本类型之间进行转换C++ 在不同类型的样本类型之间转换,c++,signal-processing,template-meta-programming,typeconverter,C++,Signal Processing,Template Meta Programming,Typeconverter,我想使用一个模板在不同的样本类型之间进行转换 template<class T,class U> void convert(const T* source, U* dest, size_t n) { do { double G=double(max(*dest))/max(*source); T diff=max(*source) - min(*source); *dest=U(makeUnsigned(
template<class T,class U>
void convert(const T* source, U* dest, size_t n)
{
do
{
double G=double(max(*dest))/max(*source);
T diff=max(*source) - min(*source);
*dest=U(makeUnsigned(*source - min(*source))*G/makeUnsigned(diff)
+makeUnsigned(max(*source) - *source)*double(min(*dest))/makeUnsigned(diff));
++dest;
++source;
--n;
}
while(n!=0);
}
其中每个元素都参照到正确的版本。我知道我必须在这里强制转换函数指针(每个元素都需要是指向函数的指针,函数采用const void*,void*size\u t,这是等价的)
我可以这样做吗?首先,写下以下内容:
template<typename T, typename U>
U convert( T const& src );
template<typename T, typename U>
void convert_buffer( T const* src, U* dest, size_t n );
typedef void(*blind_converter)(void const*, void*, size_t);
template<typename T, typename U>
void convert_blind_buffer( void const* src, void* dest, size_t n ) {
return convert_buffer( reinterpret_cast<T const*>(src), reinterpret_cast<U*>(dest), n );
}
它封装了转换,并且不需要执行理论上无效的指针类型转换。它是“convert blind”缓冲区——blind,因为它需要void*
s
接下来,我们不想手动维护您的NxN阵列。用于存储已排序类型列表的类型:
template<typename... Ts>
struct type_list {};
这使您可以执行以下操作:
static_assert( index_of< int, my_list >::value == 0, "all is well!" );
自然地,实际的用例将
类型化数组的创建与其使用分离开来。我不明白<代码>转换
是一个函数;转换[从][到]
应该做什么?或者你是说convert
?@sftrabbit是一个自动生成的可调用项矩阵,这样我就不需要在嵌套的switch case中使用MxN case了
template<typename Src, typename DestList>
struct make_convert_one_way;
template<typename Src, typename... Ds>
struct make_convert_one_way< Src, type_list<Ds...> > {
std::array< blind_converter, sizeof...(Ds) > operator()() const {
return { convert_blind_buffer< Src, Ds >... };
}
};
template<typename list>
struct make_convert_array;
template<typename... Ts>
struct make_convert_array< type_list<Ts...> > {
std::array< std::array<blind_converter, sizeof...(Ts) >, sizeof...(Ts) > operator()() const {
return { make_convert_one_way< Ts, type_list<Ts...> >... };
}
};
typedef type_list< int, char, double > my_list;
auto convert_array = make_convert_array<my_list>()();
template<typename T, typename List, typename=void>
struct index_of;
template<typename T, typename T0, typename... Ts>
struct index_of<T, type_list<T0, Ts...>, typename std::enable_if<
std::is_same<T, T0>::value
>::type >: std::integral_constant< std::size_t, 0 > {};
template<typename T, typename T0, typename... Ts>
struct index_of<T, type_list<T0, Ts...>, typename std::enable_if<
!std::is_same<T, T0>::value
>::type >: std::integral_constant< std::size_t, index_of<T, type_list<Ts...>::value+1 > {};
static_assert( index_of< int, my_list >::value == 0, "all is well!" );
template<typename List>
struct EnumDressing;
template<typename... Ts>
struct EnumDressing<type_list<Ts...>> {
enum type {
e_begin = 0,
e_end = sizeof...(Ts),
};
template<typename T>
static constexpr type value() {
return static_cast<type>( index_of<T, type_list<Ts...> >::value );
}
};
typedef EnumDressing<my_list> Types;
typedef Types::type eType;
struct typed_array {
eType type;
void* buff;
size_t n;
};
void do_convert( typed_array src, typed_array dst) {
Assert(src.n == dst.n);
convert_array[ src.type ][ dst.type ]( src.buff, dst.buff, std::min( src.n, dst.n ) );
}
template<typename T, size_t N>
typed_array make_typed_array( T (&arr)[N] ) {
return { Types::value<T>(), reinterpret_cast<void*>( &arr[0] ), N };
}
int main() {
double d[100];
int i[100];
do_convert( make_typed_array( d ), make_typed_array( i ) );
}