-
Notifications
You must be signed in to change notification settings - Fork 15
/
SimpleBuffer.h
63 lines (61 loc) · 1.14 KB
/
SimpleBuffer.h
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
54
55
56
57
58
59
60
61
62
63
#pragma once
#include <stddef.h>
#include <stdint.h>
//replace vector when you only need a buffer
template <typename T>
struct SimpleBuffer
{
SimpleBuffer() {}
SimpleBuffer(size_t size) { resize(size); }
SimpleBuffer(void* p)
{
shared = 1;
data_ = p;
}
~SimpleBuffer() { clear(); }
void resize(size_t n)
{
if (shared)
{
size_ = n;
return;
}
if (n > capacity_)
{
clear();
data_ = new T[n];
size_ = n;
capacity_ = n;
}
else
{
size_ = n;
}
}
void clear()
{
if (shared)
{
data_ = nullptr;
return;
}
delete[] data_;
data_ = nullptr;
size_ = 0;
capacity_ = 0;
}
T* data() { return data_; }
size_t size() { return size_; }
T& operator[](const size_t i) { return data_[i]; }
void set_pointer(void* p)
{
clear();
shared = 1;
data_ = p;
}
private:
T* data_{};
size_t size_{};
size_t capacity_{};
int shared{};
};