forked from duneroadrunner/SaferCPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mselegacyhelpers.h
350 lines (311 loc) · 14.7 KB
/
mselegacyhelpers.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright (c) 2015 Noah Lopez
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#ifndef MSELEGACYHELPERS_H_
#define MSELEGACYHELPERS_H_
#include "msepoly.h"
#include <cstring>
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( disable : 4100 4456 4189 4127 )
#endif /*_MSC_VER*/
#ifdef MSE_SAFER_SUBSTITUTES_DISABLED
#define MSE_LEGACYHELPERS_DISABLED
#endif /*MSE_SAFER_SUBSTITUTES_DISABLED*/
namespace mse {
namespace lh {
#ifdef MSE_LEGACYHELPERS_DISABLED
#define MSE_LH_FIXED_ARRAY_TYPE_PREFIX(size)
#define MSE_LH_FIXED_ARRAY_TYPE_SUFFIX(size)
#define MSE_LH_FIXED_ARRAY_TYPE_POST_NAME_SUFFIX(size) [size]
#define MSE_LH_FIXED_ARRAY_DECLARATION(element_type, size, name) MSE_LH_FIXED_ARRAY_TYPE_PREFIX(size) element_type MSE_LH_FIXED_ARRAY_TYPE_SUFFIX(size) name MSE_LH_FIXED_ARRAY_TYPE_POST_NAME_SUFFIX(size)
#define MSE_LH_DYNAMIC_ARRAY_ITERATOR_TYPE(element_type) element_type *
#define MSE_LH_ALLOC(element_type, ptr, num_bytes) ptr = (element_type *)malloc(num_bytes)
#define MSE_LH_REALLOC(element_type, ptr, num_bytes) ptr = (element_type *)realloc(ptr, num_bytes)
#define MSE_LH_FREE(ptr) free(ptr)
#define MSE_LH_ALLOC_DYN_ARRAY1(iterator_type, num_bytes) (iterator_type)malloc(num_bytes)
#define MSE_LH_ARRAY_ITERATOR_TYPE(element_type) element_type *
#define MSE_LH_FREAD(ptr, size, count, stream) fread(ptr, size, count, stream)
#define MSE_LH_FWRITE(ptr, size, count, stream) fwrite(ptr, size, count, stream)
#define MSE_LH_TYPED_MEMCPY(element_type, destination, source, num_bytes) memcpy(destination, source, num_bytes)
#define MSE_LH_TYPED_MEMSET(element_type, ptr, value, num_bytes) memset(ptr, value, num_bytes)
#define MSE_LH_MEMCPY(destination, source, num_bytes) memcpy(destination, source, num_bytes)
#define MSE_LH_MEMSET(ptr, value, num_bytes) memset(ptr, value, num_bytes)
#else /*MSE_LEGACYHELPERS_DISABLED*/
#define MSE_LH_FIXED_ARRAY_TYPE_PREFIX(size) mse::lh::TNativeArrayReplacement<
#define MSE_LH_FIXED_ARRAY_TYPE_SUFFIX(size) , size >
#define MSE_LH_FIXED_ARRAY_TYPE_POST_NAME_SUFFIX(size)
#define MSE_LH_FIXED_ARRAY_DECLARATION(element_type, size, name) MSE_LH_FIXED_ARRAY_TYPE_PREFIX(size) element_type MSE_LH_FIXED_ARRAY_TYPE_SUFFIX(size) name MSE_LH_FIXED_ARRAY_TYPE_POST_NAME_SUFFIX(size)
#define MSE_LH_DYNAMIC_ARRAY_ITERATOR_TYPE(element_type) mse::lh::TIPointerWithBundledVector< element_type >
#define MSE_LH_ALLOC(element_type, ptr, num_bytes) mse::lh::allocate(ptr, num_bytes)
#define MSE_LH_REALLOC(element_type, ptr, num_bytes) mse::lh::reallocate(ptr, num_bytes)
#define MSE_LH_FREE(ptr) mse::lh::CAllocF<typename std::remove_reference<decltype(ptr)>::type>::free(ptr)
#define MSE_LH_ALLOC_DYN_ARRAY1(iterator_type, num_bytes) mse::lh::allocate_dyn_array1<iterator_type>(num_bytes)
#define MSE_LH_ARRAY_ITERATOR_TYPE(element_type) mse::TNullableAnyRandomAccessIterator< element_type >
#define MSE_LH_FREAD(ptr, size, count, stream) mse::lh::CFileF< mse::TNullableAnyRandomAccessIterator<typename std::remove_reference<decltype((ptr)[0])>::type> >::fread(ptr, size, count, stream)
#define MSE_LH_FWRITE(ptr, size, count, stream) mse::lh::CFileF< mse::TNullableAnyRandomAccessIterator<typename std::remove_reference<decltype((ptr)[0])>::type> >::fwrite(ptr, size, count, stream)
#define MSE_LH_TYPED_MEMCPY(element_type, destination, source, num_bytes) mse::lh::CMemF< mse::TNullableAnyRandomAccessIterator<element_type> >::memcpy(destination, source, num_bytes)
#define MSE_LH_TYPED_MEMSET(element_type, ptr, value, num_bytes) mse::lh::CMemF< mse::TNullableAnyRandomAccessIterator<element_type> >::memset(ptr, value, num_bytes)
#define MSE_LH_MEMCPY(destination, source, num_bytes) mse::lh::CMemF< mse::TNullableAnyRandomAccessIterator<typename std::remove_reference<decltype((destination)[0])>::type> >::memcpy(destination, source, num_bytes)
#define MSE_LH_MEMSET(ptr, value, num_bytes) mse::lh::CMemF< mse::TNullableAnyRandomAccessIterator<typename std::remove_reference<decltype((ptr)[0])>::type> >::memset(ptr, value, num_bytes)
#endif /*MSE_LEGACYHELPERS_DISABLED*/
template <typename _Ty>
class TOpaqueWrapper {
public:
TOpaqueWrapper(const _Ty& value_param) : m_value(value_param) {}
_Ty& value() { return m_value; }
const _Ty& value() const { return m_value; }
_Ty m_value;
};
template <typename _Ty> using TVectorRefcfptrWrapper = TOpaqueWrapper<mse::TRefCountingFixedPointer<mse::us::msevector<_Ty>>>;
/* This data type was motivated by the need for a direct substitute for native pointers targeting dynamically
allocated (native) arrays, which can kind of play a dual role as a reference to the array object and/or as an
iterator. I'm not sure about this implementation though. We could base it on an mse::ivector::ipointer
instead... */
template <typename _Ty>
class TIPointerWithBundledVector : private TVectorRefcfptrWrapper<_Ty>, public mse::us::msevector<_Ty>::ipointer {
public:
typedef typename mse::us::msevector<_Ty>::ipointer ipointer_base_class;
typedef typename mse::us::msevector<_Ty>::size_type size_type;
TIPointerWithBundledVector() : TVectorRefcfptrWrapper<_Ty>(mse::make_refcounting<mse::us::msevector<_Ty>>())
, ipointer_base_class(*vector_refcptr()) {}
TIPointerWithBundledVector(const std::nullptr_t& src) : TIPointerWithBundledVector() {}
TIPointerWithBundledVector(const TIPointerWithBundledVector& src) : TVectorRefcfptrWrapper<_Ty>(src.vector_refcptr())
, ipointer_base_class(*vector_refcptr()) {
ipointer_base_class::operator=(src);
}
TIPointerWithBundledVector(_XSTD initializer_list<_Ty> _Ilist) : TVectorRefcfptrWrapper<_Ty>(mse::make_refcounting<mse::us::msevector<_Ty>>(_Ilist))
, ipointer_base_class(*vector_refcptr()) {}
explicit TIPointerWithBundledVector(size_type _N) : TVectorRefcfptrWrapper<_Ty>(mse::make_refcounting<mse::us::msevector<_Ty>>(_N))
, ipointer_base_class(*vector_refcptr()) {}
explicit TIPointerWithBundledVector(size_type _N, const _Ty& _V) : TVectorRefcfptrWrapper<_Ty>(mse::make_refcounting<mse::us::msevector<_Ty>>(_N, _V))
, ipointer_base_class(*vector_refcptr()) {}
/*
template <class... Args>
TIPointerWithBundledVector(Args&&... args) : TVectorRefcfptrWrapper<_Ty>(mse::make_refcounting<mse::us::msevector<_Ty>>(std::forward<Args>(args)...))
, ipointer_base_class(*vector_refcptr()) {}
*/
size_type size() const {
return (*vector_refcptr()).size();
}
void resize(size_type _N, const _Ty& _X = _Ty()) {
auto old_size = size();
(*vector_refcptr()).resize(_N, _X);
(*vector_refcptr()).shrink_to_fit();
if (true || (0 == old_size)) {
(*this).set_to_beginning();
}
}
bool operator==(const std::nullptr_t& _Right_cref) const { return (0 == size()); }
TIPointerWithBundledVector& operator=(const std::nullptr_t& _Right_cref) {
return operator=(TIPointerWithBundledVector());
}
TIPointerWithBundledVector& operator=(const TIPointerWithBundledVector& _Right_cref) {
if (_Right_cref.vector_refcptr() == vector_refcptr()) {
ipointer_base_class::operator=(_Right_cref);
}
else {
(*this).~TIPointerWithBundledVector();
::new (this) TIPointerWithBundledVector(_Right_cref);
}
return(*this);
}
explicit operator bool() const {
return ((*this).size() != 0);
}
template <class... Args>
static TIPointerWithBundledVector make(Args&&... args) {
return TIPointerWithBundledVector(std::forward<Args>(args)...);
}
private:
mse::TRefCountingFixedPointer<mse::us::msevector<_Ty>>& vector_refcptr() { return (*this).value(); }
const mse::TRefCountingFixedPointer<mse::us::msevector<_Ty>>& vector_refcptr() const { return (*this).value(); }
//mse::TRefCountingFixedPointer<mse::us::msevector<_Ty>> m_vector_refcptr;
};
template <class X, class... Args>
TIPointerWithBundledVector<X> make_ipointer_with_bundled_vector(Args&&... args) {
return TIPointerWithBundledVector<X>::make(std::forward<Args>(args)...);
}
template <typename _Ty, size_t _Size>
class TNativeArrayReplacement : public mse::mstd::array<_Ty, _Size> {
public:
typedef mse::mstd::array<_Ty, _Size> base_class;
using base_class::base_class;
operator mse::TNullableAnyRandomAccessIterator<_Ty>() {
return base_class::begin();
}
operator mse::TAnyRandomAccessIterator<_Ty>() {
return base_class::begin();
}
operator mse::TAnyRandomAccessConstIterator<_Ty>() const {
return base_class::cbegin();
}
operator typename mse::mstd::array<_Ty, _Size>::iterator() {
return base_class::begin();
}
operator typename mse::mstd::array<_Ty, _Size>::const_iterator() {
return base_class::cbegin();
}
template <class = typename std::enable_if<(!std::is_const<_Ty>::value), void>::type>
operator mse::TNullableAnyRandomAccessIterator<const _Ty>() {
return base_class::begin();
}
typename base_class::iterator operator+(typename base_class::difference_type n) { return base_class::begin() + n; }
typename base_class::iterator operator-(typename base_class::difference_type n) { return base_class::begin() - n; }
typename base_class::difference_type operator-(const typename base_class::iterator& _Right_cref) const { return base_class::begin() - _Right_cref; }
typename base_class::const_iterator operator+(typename base_class::difference_type n) const { return base_class::cbegin() + n; }
typename base_class::const_iterator operator-(typename base_class::difference_type n) const { return base_class::cbegin() - n; }
typename base_class::difference_type operator-(const typename base_class::const_iterator& _Right_cref) const { return base_class::cbegin() - _Right_cref; }
};
template<class _Ty>
class CAllocF {
public:
static void free(_Ty& ptr);
static void allocate(_Ty& ptr, size_t num_bytes);
static void reallocate(_Ty& ptr, size_t num_bytes);
};
template<class _Ty>
class CAllocF<_Ty*> {
public:
static void free(_Ty* ptr) {
::free(ptr);
}
static void allocate(_Ty*& ptr, size_t num_bytes) {
ptr = ::malloc(num_bytes);
}
static void reallocate(_Ty*& ptr, size_t num_bytes) {
ptr = ::realloc(ptr, num_bytes);
}
};
template<class _Ty>
class CAllocF<mse::lh::TIPointerWithBundledVector<_Ty>> {
public:
static void free(mse::lh::TIPointerWithBundledVector<_Ty>& ptr) {
ptr = mse::lh::TIPointerWithBundledVector<_Ty>();
}
static void allocate(mse::lh::TIPointerWithBundledVector<_Ty>& ptr, size_t num_bytes) {
mse::lh::TIPointerWithBundledVector<_Ty> tmp(num_bytes / sizeof(_Ty));
ptr = tmp;
}
static void reallocate(mse::lh::TIPointerWithBundledVector<_Ty>& ptr, size_t num_bytes) {
ptr.resize(num_bytes / sizeof(_Ty));
}
};
template<class _TDynArrayIter>
_TDynArrayIter& allocate(_TDynArrayIter& ptr, size_t num_bytes) {
CAllocF<_TDynArrayIter>::allocate(ptr, num_bytes);
return ptr;
}
template<class _TDynArrayIter>
_TDynArrayIter& reallocate(_TDynArrayIter& ptr, size_t num_bytes) {
CAllocF<_TDynArrayIter>::reallocate(ptr, num_bytes);
return ptr;
}
template<class _TDynArrayIter>
_TDynArrayIter allocate_dyn_array1(size_t num_bytes) {
_TDynArrayIter ptr;
CAllocF<_TDynArrayIter>::allocate(ptr, num_bytes);
return ptr;
}
template<class _Ty>
class CFileF {
public:
static size_t fread(_Ty ptr, size_t size, size_t count, FILE * stream);
static size_t fwrite(_Ty ptr, size_t size, size_t count, FILE * stream);
};
template<class _Ty>
class CFileF<_Ty*> {
public:
static size_t fread(_Ty* ptr, size_t size, size_t count, FILE * stream) {
return ::fread(ptr, size, count, stream);
}
static size_t fwrite(_Ty ptr, size_t size, size_t count, FILE * stream) {
return ::fwrite(ptr, size, count, stream);
}
};
template<class _Ty>
class CFileF<mse::TNullableAnyRandomAccessIterator<_Ty>> {
public:
static size_t fread(mse::TNullableAnyRandomAccessIterator<_Ty> ptr, size_t size, size_t count, FILE * stream) {
static std::vector<unsigned char> v;
v.resize(size * count);
auto num_bytes_read = ::fread(v.data(), size, count, stream);
auto num_items_read = num_bytes_read / sizeof(_Ty);
size_t uc_index = 0;
size_t Ty_index = 0;
for (; Ty_index < num_items_read; uc_index += sizeof(_Ty), Ty_index += 1) {
unsigned char* uc_ptr = &(v[uc_index]);
_Ty* Ty_ptr = reinterpret_cast<_Ty*>(uc_ptr);
ptr[Ty_index] = (*Ty_ptr);
}
v.resize(0);
return num_bytes_read;
}
static size_t fwrite(mse::TNullableAnyRandomAccessIterator<_Ty> ptr, size_t size, size_t count, FILE * stream) {
typedef typename std::remove_const<_Ty>::type non_const_Ty;
static std::vector<unsigned char> v;
v.resize(size * count);
auto num_items_to_write = size * count / sizeof(_Ty);
size_t uc_index = 0;
size_t Ty_index = 0;
for (; Ty_index < num_items_to_write; uc_index += sizeof(_Ty), Ty_index += 1) {
unsigned char* uc_ptr = &(v[uc_index]);
non_const_Ty* Ty_ptr = reinterpret_cast<non_const_Ty*>(uc_ptr);
(*Ty_ptr) = ptr[Ty_index];
}
auto res = ::fwrite(v.data(), size, count, stream);
v.resize(0);
return res;
}
};
template<class _Ty>
class CMemF {
public:
static void memcpy(_Ty destination, _Ty source, size_t num);
static void memset(_Ty ptr, int value, size_t num);
};
template<class _Ty>
class CMemF<_Ty*> {
public:
static void memcpy(_Ty* destination, _Ty* source, size_t num) {
::memcpy(destination, source, num);
}
static void memset(_Ty ptr, int value, size_t num) {
::memset(ptr, value, num);
}
};
template<class _Ty>
class CMemF<mse::TNullableAnyRandomAccessIterator<_Ty>> {
public:
static void memcpy(mse::TNullableAnyRandomAccessIterator<_Ty> destination, mse::TNullableAnyRandomAccessIterator<_Ty> source, size_t num_bytes) {
auto num_items = num_bytes / sizeof(_Ty);
for (size_t i = 0; i < num_items; i += 1) {
destination[i] = source[i];
}
}
static void memset(mse::TNullableAnyRandomAccessIterator<_Ty> ptr, int value, size_t num_bytes) {
auto Ty_value = _Ty(value);
auto num_items = num_bytes / sizeof(_Ty);
for (size_t i = 0; i < num_items; i += 1) {
ptr[i] = Ty_value;
}
}
};
template<class _Ty>
class CMemF<mse::lh::TIPointerWithBundledVector<_Ty>> {
public:
static void memcpy(mse::TNullableAnyRandomAccessIterator<_Ty> destination, mse::TNullableAnyRandomAccessIterator<_Ty> source, size_t num_bytes) {
CMemF< mse::TNullableAnyRandomAccessIterator<_Ty> >::memcpy(destination, source, num_bytes);
}
static void memset(mse::TNullableAnyRandomAccessIterator<_Ty> ptr, int value, size_t num_bytes) {
CMemF< mse::TNullableAnyRandomAccessIterator<_Ty> >::memset(ptr, value, num_bytes);
}
};
}
}
#ifdef _MSC_VER
#pragma warning( pop )
#endif /*_MSC_VER*/
#endif // MSELEGACYHELPERS_H_