From 91418b32d7b67c335eb3baffc0bf381c04f68035 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 1 Feb 2018 20:56:03 +0100 Subject: Initial commit --- sibs/SafeSerializer.hpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 sibs/SafeSerializer.hpp (limited to 'sibs/SafeSerializer.hpp') diff --git a/sibs/SafeSerializer.hpp b/sibs/SafeSerializer.hpp new file mode 100644 index 0000000..94bcdc5 --- /dev/null +++ b/sibs/SafeSerializer.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include "../types.hpp" +#include "../utils.hpp" +#include +#include + +namespace sibs +{ + /** + * Endian independent serializer + */ + class SafeSerializer + { + DISABLE_COPY(SafeSerializer); + public: + SafeSerializer(){} + + template + void add(const T &data) + { + usize offset = buffer.size(); + buffer.resize(buffer.size() + sizeof(data)); + memcpy(&buffer[offset], &data, sizeof(data)); + } + + void add(const u8 *data, usize size) + { + if(size > 0) + { + usize offset = buffer.size(); + buffer.resize(buffer.size() + size); + memcpy(&buffer[offset], data, size); + } + } + + std::vector& getBuffer() + { + return buffer; + } + private: + std::vector buffer; + }; +} -- cgit v1.2.3