#include "../serialize/SerialBufferAdapter.h" #include "../serviceinterface/ServiceInterfaceStream.h" #include template SerialBufferAdapter::SerialBufferAdapter(const uint8_t* buffer, count_t bufferLength, bool serializeLength) : serializeLength(serializeLength), constBuffer(buffer), buffer(nullptr), bufferLength(bufferLength) {} template SerialBufferAdapter::SerialBufferAdapter(uint8_t* buffer, count_t bufferLength, bool serializeLength) : serializeLength(serializeLength), constBuffer(buffer), buffer(buffer), bufferLength(bufferLength) {} template SerialBufferAdapter::~SerialBufferAdapter() { } template ReturnValue_t SerialBufferAdapter::serialize(uint8_t** buffer, size_t* size, size_t maxSize, Endianness streamEndianness) const { uint32_t serializedLength = bufferLength; if (serializeLength) { serializedLength += SerializeAdapter::getSerializedSize( &bufferLength); } if (*size + serializedLength > maxSize) { return BUFFER_TOO_SHORT; } else { if (serializeLength) { SerializeAdapter::serialize(&bufferLength, buffer, size, maxSize, streamEndianness); } if (this->constBuffer != nullptr) { memcpy(*buffer, this->constBuffer, bufferLength); } else if (this->buffer != nullptr) { // This will propably be never reached, constBuffer should always be // set if non-const buffer is set. memcpy(*buffer, this->buffer, bufferLength); } else { return HasReturnvaluesIF::RETURN_FAILED; } *size += bufferLength; (*buffer) += bufferLength; return HasReturnvaluesIF::RETURN_OK; } } template size_t SerialBufferAdapter::getSerializedSize() const { if (serializeLength) { return bufferLength + SerializeAdapter::getSerializedSize(&bufferLength); } else { return bufferLength; } } template ReturnValue_t SerialBufferAdapter::deSerialize(const uint8_t** buffer, size_t* size, Endianness streamEndianness) { if (this->buffer == nullptr) { return HasReturnvaluesIF::RETURN_FAILED; } if(serializeLength){ count_t lengthField = 0; ReturnValue_t result = SerializeAdapter::deSerialize(&lengthField, buffer, size, streamEndianness); if(result != HasReturnvaluesIF::RETURN_OK) { return result; } if(lengthField > bufferLength) { return TOO_MANY_ELEMENTS; } bufferLength = lengthField; } if (bufferLength <= *size) { *size -= bufferLength; std::memcpy(this->buffer, *buffer, bufferLength); (*buffer) += bufferLength; return HasReturnvaluesIF::RETURN_OK; } else { return STREAM_TOO_SHORT; } } template uint8_t * SerialBufferAdapter::getBuffer() { if(buffer == nullptr) { sif::error << "Wrong access function for stored type !" " Use getConstBuffer()." << std::endl; return nullptr; } return buffer; } template const uint8_t * SerialBufferAdapter::getConstBuffer() { if(constBuffer == nullptr) { sif::error << "SerialBufferAdapter::getConstBuffer:" " Buffers are unitialized!" << std::endl; return nullptr; } return constBuffer; } template void SerialBufferAdapter::setBuffer(uint8_t* buffer, count_t bufferLength) { this->buffer = buffer; this->constBuffer = buffer; this->bufferLength = bufferLength; } //forward Template declaration for linker template class SerialBufferAdapter; template class SerialBufferAdapter; template class SerialBufferAdapter;