#pragma once #include "endian.hpp" #include "types.hpp" #include "../utils.hpp" #include #include #include namespace sibs { /** * Endian independent serializer */ class SafeSerializer { public: SafeSerializer(){} SafeSerializer(SafeSerializer &&other) { buffer = std::move(other.buffer); } template void add(const T &data) { usize offset = buffer.size(); buffer.insert(buffer.end(), (unsigned char*)&data, (unsigned char*)&data + sizeof(T)); #if BYTE_ORDER == BIG_ENDIAN std::reverse((char*)&buffer[offset], (char*)&buffer[offset] + sizeof(T)); #endif } void add(const u8 *data, usize size) { if(size > 0) { usize offset = buffer.size(); buffer.resize(buffer.size() + size); memmove(&buffer[offset], data, size); } } void add(const char *data, usize size) { add((const u8*)data, size); } std::vector& getBuffer() { return buffer; } usize getSize() const { return buffer.size(); } private: std::vector buffer; }; }