diff options
Diffstat (limited to 'sibs/SafeSerializer.hpp')
-rw-r--r-- | sibs/SafeSerializer.hpp | 44 |
1 files changed, 44 insertions, 0 deletions
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 <vector> +#include <cstring> + +namespace sibs +{ + /** + * Endian independent serializer + */ + class SafeSerializer + { + DISABLE_COPY(SafeSerializer); + public: + SafeSerializer(){} + + template <typename T> + 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<u8>& getBuffer() + { + return buffer; + } + private: + std::vector<u8> buffer; + }; +} |