首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >crc模块的模板

crc模块的模板
EN

Code Review用户
提问于 2019-12-20 12:11:46
回答 1查看 93关注 0票数 5

这是我在模板方面的第一次体验。

该模块根据多项式生成crc表,并在其方法中计算结果值。

你能回顾一下吗?

代码语言:javascript
复制
    #ifndef CRC_H  // An unmultiplay file loading macros,
    #define CRC_H  // define the one.

    /* Include files: */
    #include "platform.h"  // a platform depend settings.

    #define CRC_DEBUG_COUT       ENABLE// The enable to print debug messages to a console.


    namespace CrcSpace {   // The class namespace.


        const u32 crcTableSize  = 256; // A CRC table size.

        /* CRC8 polynomials: */
        enum u8Poly  {etsiPoly  = 0xD5,    // the ETSI polynomial.
                      maximPoly = 0x31};   // the MAXIM polynomial.

        /* CRC16 polynomials: */
        enum u16Poly {ibmPoly   = 0x8005,  // the IBM polynomial.
                      ccitPoly  = 0x1021}; // the CCIT polynomial.

        /* CRC32 polynomials: */
        enum u32Poly {posixPoly = 0x04C11DB7}; // the POSIX polynomial.
    }


    /* The class definition. */
    template <typename T>
    class Crc {
    private:
        T initValue;
        T finalXor;

    protected:
       T crcTable[CrcSpace::crcTableSize] = {}; // An initialization of crc table.

    public:
        Crc(T polynomial,T initValue, T finalXor); // The class constructor declaration.
        ~Crc();                                    // The class destructor declaration.

        status calculate(void *pData, u32 dataArraySize, T &crc);   // the calkulation crc method.
    };


    //**********************************************************************
    // The class constructor definition.
    //**********************************************************************
    // Description:    The method generates a crc table depending on
    //                 A polynomial.
    //
    // polynomial:     A polynomial depending on which will be generated the
    //                 crc table.
    //
    // initValue:      A first init value.
    //
    // finalXor:       F final  XOR value.
    //
    // return:         Non.
    //
    template <typename T>
    Crc<T>::Crc(T polynomial,T initValue, T finalXor) : initValue(initValue),  finalXor(finalXor) {

       T temp = 0;

       for(u32 tabIndexCounter = 0; tabIndexCounter < CrcSpace::crcTableSize; ++tabIndexCounter) {

           temp = static_cast<T>(tabIndexCounter);

           for (u32 bitCounter = 0; bitCounter < PlatformSpace::byteSize; ++bitCounter) {

               if(temp & (1 << (sizeof(T) * PlatformSpace::byteSize - 1))) {

                   temp = (temp << 1) ^ polynomial;
               }
               else {

                   temp <<= 1;
               }
           }

           *(this->crcTable + tabIndexCounter) = temp;
       }
    }


    //**********************************************************************
    // The class destructor definition. */
    //**********************************************************************
    // Description:    non.
    //
    // parameters:     non.
    //
    template <typename T>
    Crc<T>::~Crc() {
        // It is empty yet.
    }

    //**********************************************************************
    // A definition of the template method the calculate.
    //**********************************************************************
    // Description:    The method calculates a crc result depending on
    //                 received data.
    //
    // pData:          Data fot a crc calculate.
    //
    // dataArraySize:  Size of the received data.
    //
    // crc:            The crc result.
    //
    // return:         A status of the method perform
    //                 (ok == 0 aka success and error == -1 aka failure).
    //
    template <typename T>
    status Crc<T>::calculate(void *pData, u32 dataArraySize, T& crc) {

       u8 *pByteBata = reinterpret_cast<u8*> (pData);

       if((pByteBata == nullptr) || (dataArraySize == 0)) {

           return StatusSpace::error;
       }

       crc = this->initValue;

       for(u32 dataCounter = 0; dataCounter < dataArraySize; ++dataCounter) {

           crc = (crc >> 8 ^ this->crcTable[crc ^ *(pByteBata + dataCounter)]);
       }

       crc ^= finalXor;

       return StatusSpace::ok;
    }

    #endif // CRC_H
EN

回答 1

Code Review用户

回答已采纳

发布于 2019-12-20 13:23:19

删除平凡的析构函数

析构函数没有做任何事情,所以根本不需要编写它。

明确了T允许的类型(

)

当前,您的代码允许使用任何类型的T实例化类。但是,它可能只对无符号整型有意义。此外,您可能希望将其限制为您在CrcSpace中定义的多项式,因为并非所有生成器都会产生有用的CRCs。

如果您可以将所需的多项式直接作为模板参数传递给class Crc,那就更好了。以下是一个可能的实现:

代码语言:javascript
复制
namespace CrcSpace {
    template<typename T, T value_>
    struct Polynomial {
         using value_type = T; 
         static constexpr T value = value_;
    };

    using etsi = Polynomial<u8, 0xD5>;
    ...
    using posix = Polynomial<u32, 0x04C11DB7>;
}

template <typename P>
class Crc {
    using T = typename P::value_type;

private:
    T initValue;
    ...

public:
    Crc(T initValue, T finalXor);
    ...
};

实例化一个Crc,如下所示:

代码语言:javascript
复制
Crc<CrcSpace::etsi> crc(9, 42);

在适当的

中使用数组表示法

这一行:

代码语言:javascript
复制
*(this->crcTable + tabIndexCounter) = temp;

看起来非常复杂,但它只是写到一个数组元素。它应改写为:

代码语言:javascript
复制
crcTable[tabIndexCounter] = temp;

不要编写不必要的this->

在许多没有必要使用this->variable部件的地方使用this->。与Python不同的是,您不需要到处用this->编写C++。

票数 7
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/234386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档