3

我有一段代码,我试图根据等待的数据类型自动解码缓冲区。数据表示为元组:

std::tuple<uint8_t, int32_t> data;
size_t bufferIndex;
IOBuffer::ConstSPtr buffer( std::make_shared<IOBuffer>(5) );

我也有元组 hepler 来迭代元组并为每个元组执行一个仿函数:

//-------------------------------------------------------------------------
//
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 );吗?

请不要这是正在进行的工作,因此肯定会有一些错误和改进。并感谢您的帮助!

4

2 回答 2

5

我承认我没有完成所有代码,但我相信您的问题在于运行时与编译时条件。您不能使用运行时条件(例如if (std::is_integral<:::>::value>)防止编译时错误。

我知道只有在确实是不可分割DecodeIntegral的时候才可以编译。X因此,您必须确保编译器永远不会看到对DecodeIntegral非整数的调用(即实例化),而不仅仅是它永远不会在运行时发生。X

看到函数DecodeIntegral可以很容易地成为静态成员而无需更改语义,您可以使用“委托到类”技巧来实现这一点。进入DecodeIntegral一个助手类:

template <bool Integral>
struct Helper;

template <>
struct Helper<true>
{
  template <class X>
  static void Decode( X& x, const IOBuffer::ConstSPtr& buffer, size_t& index )
  {
    // Old code of DecodeIntegral() goes here
  }  
};

template <>
struct Helper<false>
{
  template <class X>
  static void Decode( X& x, const IOBuffer::ConstSPtr& buffer, size_t& index )
  {
    // Code for non-integral decoding goes here
  }
};

struct DecoderFunctor
{
    template <typename X>
    void operator()( X& x, const IOBuffer::ConstSPtr& buffer, size_t& index )
    {
        Helper<std::is_integral<X>::value>::Decode(x, buffer, index);
    }
};

根据评论中的要求添加

如果您需要多个鉴别器,bool请向助手添加更多模板参数。如果您需要的鉴别器没有标准谓词,您可以编写自己的。

(下面的示例假设鉴别器是排他的 - 最多一个是真的):

// Two-discriminator helper
template <bool Integral, bool Array>
struct Helper;

template <>
struct Helper<true, false>
{
  void Decode() { /* integral decode */ }
};

template <>
struct Helper<false, true>
{
  void Decode() { /* array decode */ }
};


// Custom predicate
template <class T>
struct IsStdArray : std::false_type
{};

template <class T, size_t N>
struct IsStdArray<std::array<T, N>> : std::true_type
{};

// Usage
struct DecoderFunctor
{
    template <typename X>
    void operator()( X& x, const IOBuffer::ConstSPtr& buffer, size_t& index )
    {
        Helper<
            std::is_integral<X>::value,
            IsStdArray<X>::value
        >::Decode(x, buffer, index);
    }
};
于 2014-06-26T09:10:56.710 回答
1

计算结果std::is_integral<X>::valuefalse,但这并不意味着 ( DecodeIntegral( x, buffer, index );) 下面的代码被“跳过”。编译器也会看到这一行。所以,目前你有一个运行时条件,但你实际上想要一个编译时条件。

编辑:我只是想用一个简短的例子来编辑我的答案,但 Angew 更快。看他的回答:)

于 2014-06-26T09:14:56.597 回答