LDMX Software
Public Member Functions | Private Attributes | List of all members
packing::utility::CRC Class Reference

The HGC ROC and FPGA use a CRC checksum to double check that the data transfer has been done correctly. More...

#include <CRC.h>

Public Member Functions

template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
CRCoperator<< (const WordType &w)
 Stream an integral type into the calculator.
 
template<typename ObjectType , std::enable_if_t< std::is_class< ObjectType >::value, bool > = true>
CRCoperator<< (const ObjectType &o)
 Stream an instance of a class into the calculator.
 
template<typename ContentType >
CRCoperator<< (const std::vector< ContentType > &vec)
 Stream a vector of objects into the calculator.
 
uint32_t get ()
 Get the calculate checksum from the calculator.
 

Private Attributes

boost::crc_32_type crc
 the object from Boost doing the summing
 

Detailed Description

The HGC ROC and FPGA use a CRC checksum to double check that the data transfer has been done correctly.

Boost has a CRC checksum library that we can use to do this checking here as well.

Idea for this helper struct was found on StackOverflow https://stackoverflow.com/a/63237679 I've actually simplified it by limiting its use-case to our type of data which is integral types, classes with the 'add' method defined, and vectors of those types.

This is a very light class and is meant to be used as a calculator. It involves some fancy-shamncy template specialization using std::enable_if in order for the streaming operator to function.

Example

class MyObject { public: CRC& add(CRC& c) { c << my_member_int_; } private: uint64_t my_member_int_; } my_object;

CRC c; c << 0xffff << std::vector<uint16_t>({0xffff, 0x00ff}) << my_object; uint32_t the_sum = c.get();

Limitations

This design limits the inputs to the calculator to two main categories.

  1. Integral types (e.g. bool, int, unsigned int, long, char, ...)
  2. Classes with the 'CRC& add(CRC&)' method defined
  3. A std::vector of objects in (1) or (2) This means you will get a compiler error if you attempt to stream an object not fitting into one of these categories.

Definition at line 50 of file CRC.h.

Member Function Documentation

◆ get()

uint32_t packing::utility::CRC::get ( )
inline

Get the calculate checksum from the calculator.

Returns
uint32_t checksum

Definition at line 110 of file CRC.h.

110{ return crc.checksum(); }
boost::crc_32_type crc
the object from Boost doing the summing
Definition CRC.h:114

References crc.

Referenced by packing::rawdatafile::File::close(), packing::rawdatafile::EventPacket::EventPacket(), packing::rawdatafile::File::File(), ecal::EcalRawEncoder::produce(), hcal::HcalRawDecoder::read(), and packing::rawdatafile::SubsystemPacket::SubsystemPacket().

◆ operator<<() [1/3]

template<typename ObjectType , std::enable_if_t< std::is_class< ObjectType >::value, bool > = true>
CRC & packing::utility::CRC::operator<< ( const ObjectType &  o)
inline

Stream an instance of a class into the calculator.

Here, we assume that the class we are streaming into the calculator has a specific public method defined.

CRC& add(CRC& c)

and the user can insert the subcomponents to the calculator within this method.

Parameters
oinstance of an arbitrary class
Returns
CRC modified calculator

Definition at line 86 of file CRC.h.

86 {
87 return o.add(*this);
88 }

◆ operator<<() [2/3]

template<typename ContentType >
CRC & packing::utility::CRC::operator<< ( const std::vector< ContentType > &  vec)
inline

Stream a vector of objects into the calculator.

When the compiler deduces that the input to the stream is a vector, we simply call the stream operator on all members of the vector in sequence.

Parameters
[in]vecvector of objects to insert into calculator
Returns
CRC modified calculator

Definition at line 101 of file CRC.h.

101 {
102 for (auto const& w : vec) *this << w;
103 return *this;
104 }

◆ operator<<() [3/3]

template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
CRC & packing::utility::CRC::operator<< ( const WordType &  w)
inline

Stream an integral type into the calculator.

This is only enabled for integral types i.e. it looks like this definition doesn't exist for types that are non-integral. Look at the C++ reference for std::is_integral to see a full list of integral types.

Parameters
wintegral-type word to insert into calculator
Returns
CRC modified calculator

Definition at line 65 of file CRC.h.

65 {
66 crc.process_bytes(&w, sizeof(WordType));
67 return *this;
68 }

References crc.

Member Data Documentation

◆ crc

boost::crc_32_type packing::utility::CRC::crc
private

the object from Boost doing the summing

Definition at line 114 of file CRC.h.

Referenced by get(), and operator<<().


The documentation for this class was generated from the following file: