-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerySimpleBuf.hpp
More file actions
192 lines (169 loc) · 4.33 KB
/
Copy pathVerySimpleBuf.hpp
File metadata and controls
192 lines (169 loc) · 4.33 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
///////////////////////////////////////////////////////////////////////////////
///
/// Written 2012, Oliver Schneider (assarbad.net) - PUBLIC DOMAIN/CC0
///
///////////////////////////////////////////////////////////////////////////////
#ifndef __VERYSIMPLEBUF_HPP_VER__
#define __VERYSIMPLEBUF_HPP_VER__ 2018030923
#if !defined(__VERYSIMPLEBUF_MULTI_INC__) && ((defined(_MSC_VER) && (_MSC_VER >= 1020)) || defined(__MCPP))
# pragma once
#endif // Check for "#pragma once" support
template <typename T> class CVerySimpleBuf
{
public:
typedef T ElemType;
CVerySimpleBuf(size_t count = 0)
: m_buf(0)
, m_count(0)
{
reAlloc(count);
}
CVerySimpleBuf(const CVerySimpleBuf& rval)
: m_buf(0)
, m_count(0)
{
operator=(rval);
}
CVerySimpleBuf(const T* buf)
: m_buf(0)
, m_count(0)
{
if (buf)
{
operator=(buf);
}
}
~CVerySimpleBuf()
{
delete[] m_buf;
}
CVerySimpleBuf& operator=(const CVerySimpleBuf& rval)
{
if (&rval != this)
{
reAlloc(0);
if (rval.getBuf() && reAlloc(rval.getCount(), true))
{
memcpy(getBuf(), rval.getBuf(), getMin_(getByteCount(), rval.getByteCount()));
}
}
return *this;
}
CVerySimpleBuf& operator=(const T* buf)
{
reAlloc(0);
if (buf)
{
const size_t len = getBufLenZ_<const T*>(buf);
if (!len)
{
reAlloc(1);
}
else if (reAlloc(len + 1, true))
{
memcpy(getBuf(), buf, len * sizeof(T));
getBuf()[len] = 0;
}
}
return *this;
}
CVerySimpleBuf& operator+=(const CVerySimpleBuf& rval)
{
if (rval.getCountZ() && reAlloc(getCountZ() + rval.getCountZ()))
{
memcpy(getBuf() + getCountZ(), rval.getBuf(), sizeof(T) * rval.getCountZ());
}
return *this;
}
CVerySimpleBuf& operator+=(const T* buf)
{
const size_t len = getBufLenZ_<const T*>(buf);
if (len && reAlloc(getCountZ() + len))
{
memcpy(getBuf() + getCountZ(), buf, sizeof(T) * len);
}
return *this;
}
inline T* getBuf() const
{
return m_buf;
};
inline operator bool() const
{
return (0 != m_buf);
}
inline bool operator!() const
{
return (0 == m_buf);
}
inline size_t elemSize() const
{
return sizeof(ElemType);
}
void clear()
{
if (m_buf)
{
memset(m_buf, 0, m_count * sizeof(T));
}
}
bool reAlloc(size_t count, bool exact = false)
{
T* tempBuf = 0;
size_t count_ = 0;
if (count)
{
count_ = (!exact) ? getCeil_(count + 1) : count;
if (count_ <= m_count)
{
memset(m_buf + count, 0, sizeof(T) * (m_count - count));
return true;
}
if (0 != (tempBuf = new T[count_])) //-V668
{
memset(tempBuf, 0, sizeof(T) * count_);
}
if (tempBuf && m_buf) //-V668
{
memcpy(tempBuf, m_buf, sizeof(T) * getMin_(count, m_count));
}
}
delete[] m_buf;
m_buf = tempBuf;
m_count = count_;
return (0 != m_buf);
}
inline size_t getCount() const
{
return m_count;
}
inline size_t getCountZ() const
{
return getBufLenZ_<const T*>(m_buf);
}
inline size_t getByteCount() const
{
return m_count * sizeof(T);
}
protected:
T* m_buf; //-V122
size_t m_count; //-V122
inline size_t getCeil_(size_t count) const
{
const size_t align = sizeof(void*);
return (((sizeof(T) * count) + (align - 1)) & (~(align - 1))) / sizeof(T);
}
inline size_t getMin_(size_t a, size_t b) const
{
return ((a < b) ? a : b);
}
template <typename T> static size_t getBufLenZ_(const char* val)
{
return (val) ? strlen(val) : 0;
}
template <typename T> static size_t getBufLenZ_(const wchar_t* val)
{
return (val) ? wcslen(val) : 0;
}
};
#endif // __VERYSIMPLEBUF_HPP_VER__