-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUtility.h
307 lines (272 loc) · 6.35 KB
/
Utility.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
#pragma once
#include <boost/function.hpp>
#include "Types.h"
namespace Scan
{
class OutStream
{
public:
OStringStream& getStream();
OutStream& format(const char *fmt, ...);
private:
// OutStream(const OutStream&);
// OutStream& operator = (const OutStream&);
private:
OStringStream m_os;
};
template<typename T>
inline void safe_delete(T* &p)
{
delete p;
p = NULL;
}
template<typename T>
inline void safe_release(T* &p)
{
if (p != NULL)
{
p->release();
p = NULL;
}
}
template<typename T>
inline void safe_Release(T* &p)
{
if (p != NULL)
{
p->Release();
p = NULL;
}
}
/**
@brief 编译期断言
*/
#define SCAN_STATIC_ASSERT(b) { char a[(b) ? 1 : 0] = {0}; }
/**
@brief 强制类型转换
相当于(Type)v, 但是不会有warning.
只有在来源和目标类型具有相同尺寸的时候才可用转化.
*/
template<typename DesType, typename SrcType>
inline DesType force_cast(SrcType src)
{
SCAN_STATIC_ASSERT(sizeof(SrcType) == sizeof(DesType));
union { SrcType src; DesType des; }temp = {src};
return temp.des;
}
/**
@brief 返回某类型的静态名称
*/
template<typename T>
inline const char * getStaticClassName()
{
static const char *ls_className = typeid(T).name();
return ls_className;
}
/**
@brief 返回某类型的动态名称
*/
template<typename T>
inline const char * getDynamicClassName(T& v)
{
return typeid(v).name();
}
/**
@brief 返回数组的元素个数
*/
template<typename T, uint32 len>
inline uint32 countOf(T(&)[len])
{
return len;
}
/**
@brief 返回一个空字符串
*/
inline const String& getEmptyString()
{
static String ls_empty;
return ls_empty;
}
/**
@brief 将指定类型转化为字符串
*/
template<typename Type>
inline const String toString(const Type &i)
{
return static_cast<OStringStream&>(OStringStream() << i).str();
}
/**
@brief 准特化String, 提高效率
*/
inline const String& toString(const String& i)
{
return i;
}
/**
@brief 准特化char
由于ms的ostringstream存在bug, 所以对char特殊处理
*/
inline const String toString(char c)
{
return String(1, c);
}
/**
@brief 准特化bool
使用true/false
*/
inline const String toString(bool b)
{
return static_cast<OStringStream&>(
OStringStream() << std::boolalpha << b).str();
}
/**
@brief 从字符串中提取特定类型的数据
@return true表示转化成功
*/
template<typename Type>
inline bool fromString(Type &obj, const String& szSrc)
{
IStringStream si(szSrc);
si >> obj;
return si.eof() && !si.fail();
}
/**
@brief 特化处理指针
*/
template<typename Type>
inline bool fromString(Type* &obj, const String& szSrc)
{
IStringStream si(szSrc);
si >> std::hex >> reinterpret_cast<uint32&>(obj);
return si.eof() && !si.fail();
}
/**
@brief 针对String类型准特化fromString函数
为了保证正确性.避免被空格或换行隔断
*/
inline bool fromString(String& obj, const String& szSrc)
{
obj = szSrc;
return true;
}
/**
@brief 准特化bool
使用true/false
*/
inline bool fromString(bool &b, const String& szSrc)
{
IStringStream si(szSrc);
si >> std::boolalpha >> b;
return si.eof() && !si.fail();
}
/**
@brief 将任意类型变量置0
*/
template<typename T>
inline void toZero(T &v)
{
memset(&v, 0, sizeof(v));
}
/**
@brief 资源管理类, raii
*/
class ResGuard
{
public:
/**
@brief 构造函数
@param f 要调用的函数; 可以是类成员函数
@param p 调用函数的参数; 可以是类指针
*/
template<typename T, typename FunType>
ResGuard(FunType f, T *p):
m_f(boost::bind(f, p)){}
~ResGuard()
{
m_f();
}
private:
/**
@brief 禁止拷贝
*/
ResGuard(const ResGuard&);
ResGuard& operator = (const ResGuard&);
private:
boost::function<void(void)> m_f;
};
/**
@brief 类型分配器
*/
template<typename T>
struct ClassAllocator
{
static T* alloc()
{
return new T;
}
static void unalloc(T *p)
{
delete p;
}
};
/**
@brife 极小的浮点数
*/
extern const float FLOAT_EPSILON;
/**
@brief 极大的浮点数
*/
extern const float FLOAT_INFINITY;
/**
@brief 一个本地化locale
在它的生命期中, locale会变为ansi
*/
class NativeLocale
{
public:
NativeLocale();
~NativeLocale();
private:
NativeLocale(const NativeLocale&);
NativeLocale& operator = (const NativeLocale&);
private:
char *m_lastLocale;
};
/**
@brief 对std::type_info的包装
*/
class TypeInfoWrapper
{
public:
TypeInfoWrapper(const std::type_info& info):
m_info(info){}
friend bool operator == (const TypeInfoWrapper& l, const TypeInfoWrapper& r);
friend bool operator < (const TypeInfoWrapper& l, const TypeInfoWrapper& r);
private:
const std::type_info& m_info;
};
inline bool operator == (const TypeInfoWrapper& l, const TypeInfoWrapper& r)
{
return l.m_info == r.m_info;
}
inline bool operator != (const TypeInfoWrapper& l, const TypeInfoWrapper& r)
{
return !(l == r);
}
inline bool operator < (const TypeInfoWrapper& l, const TypeInfoWrapper& r)
{
return l.m_info.before(r.m_info) == 1 ? true : false;
}
inline bool operator >= (const TypeInfoWrapper& l, const TypeInfoWrapper& r)
{
return !(l < r);
}
inline bool operator > (const TypeInfoWrapper& l, const TypeInfoWrapper& r)
{
return r < l;
}
inline bool operator <= (const TypeInfoWrapper& l, const TypeInfoWrapper& r)
{
return !(r < l);
}
}