forked from jadesnake/baselib
-
Notifications
You must be signed in to change notification settings - Fork 6
/
baseX.h
403 lines (397 loc) · 8.69 KB
/
baseX.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#pragma once
namespace DuiLib
{
class CControlUI;
class CContainerUI;
class CPaintManagerUI;
}
#include "MacroX.h"
namespace base{
//引用计数器接口
class IRef
{
public:
virtual ~IRef() { };
virtual unsigned int AddRef() = 0;
virtual unsigned int RelRef() = 0;
};
//引用计数器实现
template<typename theIBase >
class Ref : public theIBase
{
public:
Ref(bool bDel=true)
:m_bDeleteThis(bDel)
{
m_unRef = 1;
}
virtual ~Ref()
{
}
inline unsigned int AddRef()
{
return InterlockedIncrement(&m_unRef);
}
inline unsigned int RelRef()
{
if( 0==InterlockedDecrement(&m_unRef) )
{
if( m_bDeleteThis )
{
delete this;
}
return 0;
}
return m_unRef;
}
inline unsigned int GetRef()
{ return m_unRef; }
private:
LONG volatile m_unRef;
bool m_bDeleteThis;
};
//CAutoRefPtr provides the basis for all other smart pointers
template <class T>
class AutoRefPtr
{
public:
AutoRefPtr() throw()
{
p = NULL;
}
AutoRefPtr(int nNull) throw()
{
(void)nNull;
p = NULL;
}
AutoRefPtr(T* lp) throw()
{
p = lp;
if (p != NULL)
{
p->AddRef();
}
}
AutoRefPtr(const AutoRefPtr & src) throw()
{
p=src.p;
if(p)
{
p->AddRef();
}
}
~AutoRefPtr() throw()
{
if (p)
{
p->RelRef();
}
}
T* operator->() const throw()
{
return p;
}
operator T*() const throw()
{
return p;
}
T& operator*() const
{
return *p;
}
//The assert on operator& usually indicates a bug. If this is really
//what is needed, however, take the address of the p member explicitly.
T** operator&() throw()
{
ASSERT(p==NULL);
return &p;
}
bool operator!() const throw()
{
return (p == NULL);
}
bool operator<(T* pT) const throw()
{
return p < pT;
}
bool operator!=(T* pT) const
{
return !operator==(pT);
}
bool operator==(T* pT) const throw()
{
return p == pT;
}
T* operator=(T* lp) throw()
{
if(*this!=lp)
{
if(p)
{
p->RelRef();
}
p=lp;
if(p)
{
p->AddRef();
}
}
return *this;
}
T* operator=(const AutoRefPtr<T>& lp) throw()
{
if(*this!=lp)
{
if(p)
{
p->RelRef();
}
p=lp;
if(p)
{
p->AddRef();
}
}
return *this;
}
// Release the interface and set to NULL
void RelRef() throw()
{
T* pTemp = p;
if (pTemp)
{
if(0==pTemp->RelRef())
{
p = NULL;
}
}
}
// Attach to an existing interface (does not AddRef)
void Attach(T* p2) throw()
{
if (p)
{
p->RelRef();
}
p = p2;
}
// Detach the interface (does not Release)
T* Detach() throw()
{
T* pt = p;
p = NULL;
return pt;
}
HRESULT CopyTo(T** ppT) throw()
{
if (ppT == NULL)
return E_POINTER;
*ppT = p;
if (p)
{
p->AddRef();
}
return S_OK;
}
protected:
T* p;
};
//应用常用初始化和释放
class AppInitialize
{
public:
AppInitialize()
{
WSAStartup(MAKEWORD(2,2),&wsaData);
::CoInitialize(NULL);
::OleInitialize(NULL);
}
virtual ~AppInitialize()
{
::OleUninitialize();
::CoUninitialize();
WSACleanup();
}
const WSADATA& GetSockStartData() const
{
return wsaData;
}
protected:
WSADATA wsaData;
};
//Dll工具 dll在加载时需要重设置工作目录因此需要传完整路径
class DLL
{
public:
DLL() : m_dwError(0)
{
}
//需要传完整路径
DLL(LPCTSTR pDll) : m_dwError(0)
{
Load(pDll);
}
void Load(LPCTSTR pDll)
{
TCHAR chOld[MAX_PATH+1];
TCHAR chMax[MAX_PATH+1];
memset(chMax,0,sizeof(chMax));
_tcscpy_s(chMax,MAX_PATH,pDll);
::PathRemoveFileSpec(chMax); //生成dll路径
::GetCurrentDirectory(MAX_PATH,chOld);
::SetCurrentDirectory(chMax);
m_hModule = ::LoadLibrary(pDll);
if( m_hModule == NULL )
{
m_dwError = ::GetLastError();
}
::SetCurrentDirectory(chOld);
}
virtual ~DLL()
{
if( m_hModule )
{
FreeLibrary(m_hModule);
m_hModule = NULL;
}
}
template<typename F>
F FindFunction(const char *fun)
{
F ret;
if( m_hModule == NULL ) return (F)0;
ret = (F)(::GetProcAddress(m_hModule,fun));
if( ret == 0 )
m_dwError = ::GetLastError();
return ret;
}
DWORD GetLastError(bool bShow)
{
TCHAR chMsg[512];
_stprintf_s(chMsg,512,_T("DLL LastError %d"),m_dwError);
if( bShow )
{
::MessageBox(NULL,chMsg,_T("tip"),0);
}
return m_dwError;
}
DISALLOW_COPY_AND_ASSIGN(DLL);
protected:
HMODULE m_hModule;
DWORD m_dwError;
};
//duilib根据类型进行接口转换
template<typename theX>
theX* SafeConvert(DuiLib::CControlUI *pCtrl,LPCTSTR lpClass)
{
if( (pCtrl==NULL) || (lpClass==NULL) )
return NULL;
return static_cast<theX*>(pCtrl->GetInterface(lpClass));
}
template<typename theX>
theX* SafeConvert(DuiLib::CContainerUI *pCtrl,LPCTSTR lpName,LPCTSTR lpClass)
{
DuiLib::CControlUI *pRet = NULL;
if( (pCtrl==NULL) || (lpClass==NULL) || (lpName==NULL) )
return NULL;
pRet = pCtrl->FindSubControl(lpName);
if( pRet == NULL ) return NULL;
return static_cast<theX*>(pRet->GetInterface(lpClass));
}
template<typename theX>
theX* SafeConvert(DuiLib::CPaintManagerUI *pm,LPCTSTR lpName,LPCTSTR lpClass)
{
DuiLib::CControlUI *pRet = NULL;
if( (pm==NULL) || (lpName==0) || (lpClass==0) )
return NULL;
pRet = pm->FindControl(lpName);
if( pRet == NULL ) return NULL;
return static_cast<theX*>(pRet->GetInterface(lpClass));
}
/*---------------------------------------------------------------------*\
函数功能:枚举指定路径下的所有文件
函数名称:EnumFolderFiles
参 数:pFolder 文件夹路径
返 回 值:
\*---------------------------------------------------------------------*/
template<class C>
DWORD EnumFolderFiles(LPCTSTR pFolder,C *pC,bool (C::* pOne)(const ATL::CAtlString&,WIN32_FIND_DATA*))
{
WIN32_FIND_DATA pData = { 0 };
HANDLE hFind = INVALID_HANDLE_VALUE;
ATL::CAtlString strSpec,strPath(pFolder);
strSpec.Format(_T("%s\\*.*"),pFolder);
hFind = FindFirstFile(strSpec,&pData);
if( hFind == INVALID_HANDLE_VALUE )
return GetLastError();
do
{
if( false == (pC->*pOne)(strPath,&pData) )
break;
} while ( FindNextFile(hFind, &pData) );
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
return 0;
}
// bit_cast<Dest,Source> is a template function that implements the
// equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in
// very low-level functions like the protobuf library and fast math
// support.
//
// float f = 3.14159265358979;
// int i = bit_cast<int32>(f);
// // i = 0x40490fdb
//
// The classical address-casting method is:
//
// // WRONG
// float f = 3.14159265358979; // WRONG
// int i = * reinterpret_cast<int*>(&f); // WRONG
//
// The address-casting method actually produces undefined behavior
// according to ISO C++ specification section 3.10 -15 -. Roughly, this
// section says: if an object in memory has one type, and a program
// accesses it with a different type, then the result is undefined
// behavior for most values of "different type".
//
// This is true for any cast syntax, either *(int*)&f or
// *reinterpret_cast<int*>(&f). And it is particularly true for
// conversions betweeen integral lvalues and floating-point lvalues.
//
// The purpose of 3.10 -15- is to allow optimizing compilers to assume
// that expressions with different types refer to different memory. gcc
// 4.0.1 has an optimizer that takes advantage of this. So a
// non-conforming program quietly produces wildly incorrect output.
//
// The problem is not the use of reinterpret_cast. The problem is type
// punning: holding an object in memory of one type and reading its bits
// back using a different type.
//
// The C++ standard is more subtle and complex than this, but that
// is the basic idea.
//
// Anyways ...
//
// bit_cast<> calls memcpy() which is blessed by the standard,
// especially by the example in section 3.9 . Also, of course,
// bit_cast<> wraps up the nasty logic in one place.
//
// Fortunately memcpy() is very fast. In optimized mode, with a
// constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline
// code with the minimal amount of data movement. On a 32-bit system,
// memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8)
// compiles to two loads and two stores.
//
// WARNING: if Dest or Source is a non-POD type, the result of the memcpy
// is likely to surprise you.
template <class Dest, class Source>
inline Dest bit_cast(const Source& source)
{
// Compile time assertion: sizeof(Dest) == sizeof(Source)
// A compile error here means your Dest and Source have different sizes.
typedef char VerifySizesAreEqual [sizeof(Dest) == sizeof(Source) ? 1 : -1];
Dest dest;
memcpy(&dest, &source, sizeof(dest));
return dest;
}
}