10 #ifndef _BIT_SERIALIZER_HEADER_
11 #define _BIT_SERIALIZER_HEADER_
44 namespace bitconverters
88 tSize nBytesToRead = 0;
94 tSize nOffsetStart = nStartBit % CHAR_BIT;
96 tSize nOffsetEnd = (CHAR_BIT - ((nStartBit + nBitLength) % CHAR_BIT)) % CHAR_BIT;
116 nResult >>= nOffsetStart;
123 tSize nBitSize =
sizeof(nResult) * CHAR_BIT;
125 nNinthByte <<= (nBitSize - nOffsetStart);
127 nResult = nResult | nNinthByte;
134 if (nBitLength % CHAR_BIT != 0)
141 nResult <<= (nOffsetEnd + nOffsetStart) % CHAR_BIT;
150 pMSByte[0] >>= (nOffsetEnd + nOffsetStart) % CHAR_BIT;
158 bithelpers::ConvertSignalEndianess(&nResult, nEndianess, nBitLength);
179 CutLeadingBits(&nResult, (
sizeof(nResult) * CHAR_BIT) - nOffsetEnd);
186 nResult >>= nOffsetStart;
195 nResult >>= (((
sizeof(nResult) * CHAR_BIT) - nBitLength - nOffsetStart) /
sizeof(nResult)) * CHAR_BIT;
208 nResult <<= nOffsetEnd;
211 if (nBitLength % CHAR_BIT != 0)
218 tUInt8 *pMSByte = (
tUInt8*)&nResult + (nBitLength / CHAR_BIT);
219 pMSByte[0] >>= (nOffsetEnd + nOffsetStart) % CHAR_BIT;
235 CutLeadingBits(&nResult, nBitLength + (nOffsetEnd + nOffsetStart) % CHAR_BIT);
267 tSize nBytesToRead = 0;
268 CopyBytesFromBuffer(pBuffer, &nBufferCopy, nStartBit, nBitLength, &nNinthByte, &nBytesToRead);
272 tSize nOffsetStart = nStartBit % CHAR_BIT;
274 tSize nOffsetEnd = (CHAR_BIT - ((nStartBit + nBitLength) % CHAR_BIT)) % CHAR_BIT;
276 if ((nBitLength + nOffsetStart) >= (
sizeof(nMaskLeft) * CHAR_BIT))
283 nMaskLeft <<= (nBitLength + nOffsetStart);
286 nMask <<= nOffsetStart;
290 nBufferCopy &= nMask;
300 tInt nShiftAmount = (
tInt)nOffsetStart;
306 bithelpers::ConvertSignalEndianess(&nSignal, nEndianess, nBitLength);
309 tInt nMSBShift = (CHAR_BIT - (nBitLength % CHAR_BIT)) % CHAR_BIT;
310 pMSByte[0] <<= nMSBShift;
311 nShiftAmount -= nMSBShift;
315 if ((nOffsetStart + nBitLength) > (
sizeof(nSignal) * CHAR_BIT))
317 tUInt64 nSignalForNinthByte = nSignal;
318 nSignalForNinthByte >>= (
sizeof(nSignal) - 1) * CHAR_BIT;
319 nSignalForNinthByte >>= (CHAR_BIT - nOffsetStart);
321 nMask <<= (CHAR_BIT - nOffsetEnd);
323 nNinthByte |= nSignalForNinthByte;
326 if (nShiftAmount < 0)
328 nSignal >>= std::abs(nShiftAmount);
332 nSignal <<= nShiftAmount;
336 nBufferCopy |= nSignal;
341 if (nBytesToRead >
sizeof(nSignal))
360 tSize nBitSize = (
sizeof(pValue) * CHAR_BIT);
361 if (nBitLength < nBitSize)
364 mask >>= (nBitSize - nBitLength);
390 tSize nStartByte = nStartBit / CHAR_BIT;
393 tSize nBitsToRead = nBitLength + (nStartBit % CHAR_BIT);
394 if ((nBitsToRead % CHAR_BIT) > 0)
396 nBitsToRead += (CHAR_BIT - (nBitsToRead % CHAR_BIT));
399 *pBytesToRead = nBitsToRead / CHAR_BIT;
402 if (*pBytesToRead > (
tSize)
sizeof(pValue))
413 if (*pBytesToRead >
sizeof(*pValue))
426 template<
typename T,
int IS_SIGNED,
int IS_FLOATING_POINT>
class Converter;
507 *pValue <<= (
sizeof(T) * CHAR_BIT) - nBitLength;
508 *pValue >>= (
sizeof(T) * CHAR_BIT) - nBitLength;
563 if (
sizeof(T) * CHAR_BIT == nBitLength)
569 return ERR_INVALID_ARG;
592 if (
sizeof(T) * CHAR_BIT == nBitLength)
598 return ERR_INVALID_ARG;
686 std::is_floating_point<T>::value>
725 std::is_floating_point<T>::value>
771 if (nSizeVariable * CHAR_BIT < nBitLength)
uint8_t tUInt8
type definition for unsigned integer values (8bit) (platform and compiler independent type).
void tVoid
The tVoid is always the definition for the void (non-type).
int tInt
type definition for signed integer value (platform and compiler dependent type).
size_t tSize
type definition for a array size values, map size values etc.
uint64_t tUInt64
type definition for unsigned integer values (64bit) (platform and compiler independent type).
#define RETURN_IF_FAILED(s)
Return if expression is failed, which requires the calling function's return type to be tResult.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
#define RETURN_IF_POINTER_NULL(_ptr)
Return ERR_POINTER if _ptr is nullptr, which requires the calling function's return type to be tResul...
tResult ConvertSignalEndianess(tUInt64 *pSignal, tInt nEndianess, tSize nBitLength)
Convert the endianess of a signal by correctly swapping the byte order if required.
tVoid PrintBits(tUInt64 nValue)
Write the bit pattern of a tUInt64 value to a string and prints it.
static tResult Read(tUInt8 *pBuffer, tSize nStartBit, tSize nBitLength, T *pValue, tInt nEndianess)
Read unsigned integer from bitfield.
static tResult Write(tUInt8 *pBuffer, tSize nStartBit, tSize nBitLength, T nValue, tInt nEndianess)
Write unsigned integer to bitfield.
static tResult Read(tUInt8 *pBuffer, tSize nStartBit, tSize nBitLength, T *pValue, tInt nEndianess)
Read signed integer from bitfield.
static tResult Write(tUInt8 *pBuffer, tSize nStartBit, tSize nBitLength, T nValue, tInt nEndianess)
Write signed integer to bitfield.
static tResult Read(tUInt8 *pBuffer, tSize nStartBit, tSize nBitLength, T *pValue, tInt nEndianess)
Read tFloat from bitfield.
static tResult Write(tUInt8 *pBuffer, tSize nStartBit, tSize nBitLength, T nValue, tInt nEndianess)
Write tFloat to bitfield.
Converter Base Contains the base methods used by all inheriting Converter classes.
static tResult ReadSignal(tUInt8 *pBuffer, tSize nStartBit, tSize nBitLength, T *pValue, tInt nEndianess=PLATFORM_BYTEORDER)
Read value from bitfield.
static tResult WriteSignal(tUInt8 *pBuffer, tSize nStartBit, tSize nBitLength, T nValue, tInt nEndianess=PLATFORM_BYTEORDER)
Write value to bitfield.
static tResult CopyBytesFromBuffer(tUInt8 *pBuffer, tUInt64 *pValue, tSize nStartBit, tSize nBitLength, tUInt64 *pNinthByte, tSize *pBytesToRead)
Copy pBytesToRead number of bytes from the buffer to pValue and pNinthByte.
static tResult CutLeadingBits(tUInt64 *pValue, tSize nBitLength)
Set the highest bits of a tUInt64 value to zero.
Template converter class to differentiate between float, signed and unsigned integer values.
cBitSerializer(cMemoryBlock *pMemoryBlock)
Constructor.
tUInt8 * m_pBuffer
internal buffer
tResult Read(tSize nStartBit, tSize nBitLength, T *pValue, tInt nEndianess=PLATFORM_BYTEORDER)
Read value from bitfield.
cBitSerializer(tVoid *pData, tSize nDataSize)
Constructor.
tSize m_nBufferBits
size of internal buffer in bits
tResult Write(tSize nStartBit, tSize nBitLength, T nValue, tInt nEndianess=PLATFORM_BYTEORDER)
Write value to bitfield.
tSize m_nBufferBytes
size of internal buffer in bytes
virtual ~cBitSerializer()
Destructor.
cBitSerializer()
Default Constructor.
tResult CheckForInvalidArguments(tSize nStartBit, tSize nBitLength, tSize nSizeVariable)
Check if the parameters for the reading and writing access are valid.
static tVoid MemCopy(tVoid *pDest, const tVoid *pSrc, tSize nCount)
Copies data from one buffer to another.
tVoid * GetPtr() const
Get a pointer to the allocated memory.
tSize GetSize() const
Get the amount of allocated memory in bytes.
A common result class usable as return value throughout.
tBool IsFailed() const
Check whether this result object contains an error != ERR_NOERROR.
#define PLATFORM_BIG_ENDIAN_8
defines the big endianess value, that will be retrieved by
#define PLATFORM_BYTEORDER
defines a link to __get_platform_byteorder.
#define PLATFORM_LITTLE_ENDIAN_8
defines the little endianess value, that will be retrieved by
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.