aboutsummaryrefslogtreecommitdiff
path: root/sibs/SafeSerializer.hpp
blob: 94bcdc55b7bfb47225fee15f9bbc3be6c2fc6751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
    };
}