-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData.h
96 lines (82 loc) · 2.21 KB
/
Data.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef _DATA_H_
#define _DATA_H_
class Data {
public:
class Type {
private:
static uint16_t _nextID;
uint16_t _id;
public:
explicit Type() : _id(_nextID++) {}
virtual ~Type() {}
uint16_t id() const { return _id; }
void setId(const uint16_t id) { _id = id; }
bool operator==(const Type* t) const { return (_id == t->id()); }
bool operator==(const Type& t) const { return (_id == t.id()); }
bool operator==(const uint16_t id) const { return (_id == id); }
};
public:
static const Type _binDataType;
static const Type _strDataType;
private:
uint8_t* _buffer;
size_t _size; // buffer size
size_t _length; // data length
bool _isHeap;
Type _type;
public:
explicit Data() : _buffer(nullptr), _size(0), _length(0), _isHeap(false) {}
explicit Data(size_t size) : _size(size), _length(0), _isHeap(true) {
_buffer = new uint8_t[size];
memset(_buffer, 0, size);
}
virtual ~Data() { if (_isHeap) delete[] _buffer; }
inline void alloc(size_t size) {
free();
_buffer = new uint8_t[size];
if (_buffer == nullptr) {
//traceln("[Data] memory alloc failed.");
return;
}
memset(_buffer, 0, size);
_size = size;
_isHeap = true;
}
inline void alloc(uint8_t* buffer) {
free();
_buffer = buffer;
_size = sizeof(buffer);
_isHeap = false;
}
inline void free() {
if ( (_buffer != nullptr) && (_isHeap == true) ) {
delete[] _buffer;
_buffer = nullptr;
_isHeap = false;
}
}
inline void clear() {
if (_buffer != nullptr) {
//memset(_buffer, 0, _size);
_length = 0;
}
}
inline void copyTo(uint8_t* dest, size_t size) const {
if (_buffer != nullptr) {
memcpy(dest, _buffer, _length);
}
}
inline void copyFrom(uint8_t* src, size_t length) {
if (_buffer != nullptr) {
memcpy(_buffer, src, length);
_length = length;
}
}
inline uint8_t* buffer() { return _buffer; }
inline size_t size() const { return _size; }
inline size_t length() const { return _length; }
inline Type type() const { return _type; }
inline void setType(Type type) { _type.setId(type.id()); }
inline void setLength(size_t length) { _length = length; }
};
#endif