我有一段代码,试图自动解码给定的缓冲区中的数据类型。这些数据以元组的形式表示:
std::tuple<uint8_t, int32_t> data;
size_t bufferIndex;
IOBuffer::ConstSPtr buffer( std::make_shared<IOBuffer>(5) );
我还有元组辅助函数,可以迭代元组并对每个元组执行一个函数对象:
//-------------------------------------------------------------------------
//
template <typename Function, typename ...Tuples, typename ...Args>
void IterateOverTuple( Function& f, std::tuple<Tuples...>& t,
Args&... args )
{
impl::IterateOverTupleImpl<0, sizeof...(Tuples),
std::tuple<Tuples...>>()( f, t, args... );
}
//---------------------------------------------------------------------
//
template <int I, size_t TSize, typename Tuple>
struct IterateOverTupleImpl
: public IterateOverTupleImpl<I + 1, TSize, Tuple>
{
template <typename Function, typename ...Args>
void operator()( Function& f, Tuple& t, Args&... args )
{
f( std::get<I>(t), args... );
IterateOverTupleImpl<I + 1, TSize, Tuple>::operator()( f, t,
args... );
}
};
//---------------------------------------------------------------------
//
template <int I, typename Tuple>
struct IterateOverTupleImpl<I, I, Tuple>
{
template <typename Function, typename ...Args>
void operator()( Function& f, Tuple& t, Args&... )
{
cl::Ignore(f);
cl::Ignore(t);
}
};
这是我的解码器函数对象:
struct DecoderFunctor
{
template <typename X>
void DecodeIntegral( X& x, const IOBuffer::ConstSPtr& buffer, size_t& index )
{
if( std::is_same<X, uint8_t>::value )
{
x = buffer->At(index);
}
else if( std::is_same<X, int8_t>::value )
{
x = static_cast<int8_t>( buffer->At(index) );
}
else if( std::is_same<X, uint16_t>::value )
{
x = cl::ByteConversion::UbytesToUInt16( UByteArray<2>{{
buffer->At(index + 0),
buffer->At(index + 1) }}
);
}
else if( std::is_same<X, int16_t>::value )
{
x = cl::ByteConversion::UbytesToInt16( UByteArray<2>{{
buffer->At(index + 0),
buffer->At(index + 1) }}
);
}
else if( std::is_same<X, uint32_t>::value )
{
x = cl::ByteConversion::UbytesToUInt32( UByteArray<4>{{
buffer->At(index + 0),
buffer->At(index + 1),
buffer->At(index + 2),
buffer->At(index + 3) }}
);
}
else if( std::is_same<X, int32_t>::value )
{
x = cl::ByteConversion::UbytesToInt32( UByteArray<4>{{
buffer->At(index + 0),
buffer->At(index + 1),
buffer->At(index + 2),
buffer->At(index + 3) }}
);
}
else if( std::is_same<X, uint64_t>::value )
{
x = cl::ByteConversion::UbytesToUInt64( UByteArray<8>{{
buffer->At(index + 0),
buffer->At(index + 1),
buffer->At(index + 2),
buffer->At(index + 3),
buffer->At(index + 4),
buffer->At(index + 5),
buffer->At(index + 6),
buffer->At(index + 7) }}
);
}
else if( std::is_same<X, int64_t>::value )
{
x = cl::ByteConversion::UbytesToInt64( UByteArray<8>{{
buffer->At(index + 0),
buffer->At(index + 1),
buffer->At(index + 2),
buffer->At(index + 3),
buffer->At(index + 4),
buffer->At(index + 5),
buffer->At(index + 6),
buffer->At(index + 7) }}
);
}
// Increment the index in the buffer
index += sizeof(X);
}
template <typename X>
void operator()( X& x, const IOBuffer::ConstSPtr& buffer, size_t& index )
{
if( std::is_integral<X>::value )
{
DecodeIntegral( x, buffer, index );
}
}
};
这就是代码被调用的地方:
DecoderFunctor func;
IterateOverTuple( func, data, buffer, index );
对于整数类型,一切都很正常,它们被完美地解码。但是当我想尝试实现一个新的解码方法(用于数组)时,它无法编译:
std::tuple<std::array<uint16_t, 100>, std::array<uint8_t, 100>> data;
这里是错误信息(gcc-4.9)。因为测试
std::is_integral<X>::value
,所以数据不应该在DecodeIntegral( x, buffer, index );
中被评估。我不明白为什么会出现这个错误。请注意,这还在进行中,肯定有一些错误和改进需要做。感谢您的帮助!