forked from ostis-ai/sc-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc_utils.hpp
258 lines (219 loc) · 5.14 KB
/
sc_utils.hpp
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
/*
* This source file is part of an OSTIS project. For the latest info, see http://ostis.net
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/
#pragma once
#include "sc_debug.hpp"
#include <cstdint>
#include <exception>
#include <memory>
#include <string>
#include <cassert>
#include <memory.h>
// Got it there: https://github.com/mapsme/omim/blob/136f12af3adde05623008f71d07bb996fe5801a5/base/macros.hpp
#define ARRAY_SIZE(X) sizeof(::my::impl::ArraySize(X))
#define SC_DISALLOW_COPY(className) \
className(className const &) = delete; \
className & operator=(className const &) = delete
#define SC_DISALLOW_MOVE(className) \
className(className &&) = delete; \
className & operator=(className &&) = delete
#define SC_DISALLOW_COPY_AND_MOVE(className) \
SC_DISALLOW_COPY(className); \
SC_DISALLOW_MOVE(className)
// ---------------- Reference counter -----------
class RefCount
{
public:
RefCount()
: m_refCount(0)
{
}
inline void Ref()
{
++m_refCount;
}
inline uint32_t Unref()
{
SC_ASSERT(m_refCount > 0, ());
--m_refCount;
return m_refCount;
}
private:
uint32_t m_refCount;
};
#define SHARED_PTR_TYPE(__type) using __type##Ptr = std::shared_ptr<__type>;
class MemoryBuffer
{
public:
MemoryBuffer(char * buff, unsigned int size)
: m_data(buff)
, m_size(size)
{
}
inline bool IsValid() const
{
return m_data != nullptr;
}
void * Data()
{
return static_cast<void *>(m_data);
}
void const * CData() const
{
return static_cast<void const *>(m_data);
}
size_t Size() const
{
return m_size;
}
size_t Read(void * buff, size_t size) const
{
size_t const read = std::min(size, m_size);
memcpy(buff, m_data, read);
return read;
}
protected:
MemoryBuffer()
: m_data(nullptr)
, m_size(0)
{
}
char * m_data;
size_t m_size;
};
class MemoryBufferSafe : public MemoryBuffer
{
public:
MemoryBufferSafe()
: MemoryBuffer()
{
}
MemoryBufferSafe(char const * buff, size_t size)
{
Reinit(buff, size);
}
~MemoryBufferSafe()
{
Clear();
}
void Reinit(size_t size)
{
Clear();
m_data = new char[size];
m_size = size;
}
void Reinit(char const * buff, size_t size)
{
m_data = new char[size];
m_size = size;
memcpy(m_data, buff, size);
}
void Clear()
{
delete[] m_data;
m_data = nullptr;
m_size = 0;
}
};
SHARED_PTR_TYPE(MemoryBuffer)
SHARED_PTR_TYPE(MemoryBufferSafe)
namespace utils
{
namespace impl
{
template <typename T>
inline T toNumber(std::string const & value);
template <>
inline int8_t toNumber<int8_t>(std::string const & value)
{
return int8_t(std::stol(value));
}
template <>
inline int16_t toNumber<int16_t>(std::string const & value)
{
return int16_t(std::stol(value));
}
template <>
inline int32_t toNumber<int32_t>(std::string const & value)
{
return int32_t(std::stol(value));
}
template <>
inline int64_t toNumber<int64_t>(std::string const & value)
{
return int64_t(std::stoll(value));
}
template <>
inline uint8_t toNumber<uint8_t>(std::string const & value)
{
return uint8_t(std::stoul(value));
}
template <>
inline uint16_t toNumber<uint16_t>(std::string const & value)
{
return uint16_t(std::stoul(value));
}
template <>
inline uint32_t toNumber<uint32_t>(std::string const & value)
{
return uint32_t(std::stoul(value));
}
template <>
inline uint64_t toNumber<uint64_t>(std::string const & value)
{
return uint64_t(std::stoull(value));
}
template <>
inline float toNumber<float>(std::string const & value)
{
return std::stof(value);
}
template <>
inline double toNumber<double>(std::string const & value)
{
return std::stod(value);
}
} // namespace impl
class StringUtils
{
public:
_SC_EXTERN static void ToLowerCase(std::string & str);
_SC_EXTERN static void ToUpperCase(std::string & str);
_SC_EXTERN static bool StartsWith(std::string const & str, std::string const & pattern, bool lowerCase = true);
_SC_EXTERN static bool EndsWith(std::string const & str, std::string const & pattern, bool lowerCase = true);
_SC_EXTERN static void SplitFilename(
std::string const & qualifiedName,
std::string & outBasename,
std::string & outPath);
_SC_EXTERN static void SplitString(std::string const & str, char delim, std::vector<std::string> & outList);
_SC_EXTERN static void TrimLeft(std::string & str);
_SC_EXTERN static void TrimRight(std::string & str);
_SC_EXTERN static void Trim(std::string & str);
_SC_EXTERN static std::string GetFileExtension(std::string const & filename);
_SC_EXTERN static std::string NormalizeFilePath(std::string const & init, bool makeLowerCase);
_SC_EXTERN static std::string ReplaceAll(
std::string const & source,
std::string const & replaceWhat,
std::string const & replaceWithWhat);
template <typename T>
static bool ParseNumber(std::string const & value, T & outValue)
{
try
{
outValue = impl::toNumber<T>(value);
return true;
}
catch (std::exception const &)
{
}
return false;
}
};
class Random
{
public:
_SC_EXTERN static int Int();
};
} // namespace utils