aboutsummaryrefslogtreecommitdiff
path: root/include/odhtdb/OwnedMemory.hpp
blob: 4c6df1ca03088b4a48b190dd334881b5f1f55801 (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
45
46
47
48
49
50
51
52
53
#pragma once

#include "types.hpp"
#include "DataView.hpp"

namespace odhtdb
{
    class OwnedMemory
    {
    public:
        OwnedMemory() : data(nullptr), size(0) {}
        OwnedMemory(void *_data, usize _size) : data(_data), size(_size) {}
        OwnedMemory(OwnedMemory &&other);
        ~OwnedMemory();
        
        // Do not allow copy of this struct, forcing move when returning a OwnedMemory in a function
        OwnedMemory(OwnedMemory&) = delete;
        OwnedMemory& operator = (OwnedMemory&) = delete;
        
        const DataView getView() const { return DataView(data, size); }
        
        void *data;
        usize size;
    };
    
    class OwnedByteArray
    {
    public:
        OwnedByteArray() : data(nullptr), size(0) {}
        OwnedByteArray(u8 *_data, usize _size) : data(_data), size(_size) {}
        OwnedByteArray(OwnedByteArray &&other)
        {
            data = other.data;
            size = other.size;
            
            other.data = nullptr;
            other.size = 0;
        }
        ~OwnedByteArray()
        {
            delete[] data;
        }
        
        // Do not allow copy of this struct, forcing move when returning a OwnedByteArray in a function
        OwnedByteArray(OwnedByteArray&) = delete;
        OwnedByteArray& operator = (OwnedByteArray&) = delete;
        
        const DataView getView() const { return DataView(data, size); }
        
        u8 *data;
        usize size;
    };
}