TNB Library
TnbDef.h
[詳解]
1#pragma once
44#ifndef _TnbDOXYGEN //Document作成用シンボル
45
46#pragma warning( disable : 4239 )
47#pragma warning( disable : 4786 )
48#pragma warning( disable : 4097 )
49#pragma warning( disable : 4100 )
50#pragma warning( disable : 4127 )
51#pragma warning( disable : 4702 )
52#pragma warning( disable : 4456 )
53#pragma warning( disable : 4840 )
54#pragma warning( disable : 4458 )
55#pragma warning( disable : 4457 )
56
57#ifdef _TnbPGRELIEF //Document作成用シンボル
58 #include <afxwin.h>
59 #include <afxext.h>
60#endif
61
62#include <windows.h>
63#include <winnt.h>
64#include <tchar.h>
65#include <stdio.h>
66#include <limits.h>
67#ifndef _WIN32_WCE
68 #if _MSC_VER < 1923
69 //====VC6.0以下
70 #include <typeinfo.h> // typeid(this).name();
71 #else
72 #include <vcruntime_typeinfo.h>
73 #endif
74 #define GetProcAddressA GetProcAddress
75#endif
76
77#ifdef _TnbDEBUG_ON
78 #define TTRACE0(XX) TRACE0(XX)
79 #define TTRACE1(XX,P1) TRACE1(XX,P1)
80 #define TTRACE2(XX,P1,P2) TRACE2(XX,P1,P2)
81 #define TTRACE3(XX,P1,P2,P3) TRACE3(XX,P1,P2,P3)
82 #define TTRACE1A(XX,P1) TRACE1A(XX,P1)
83 #define TTRACE2A(XX,P1,P2) TRACE2A(XX,P1,P2)
84 #define TTRACE3A(XX,P1,P2,P3) TRACE3A(XX,P1,P2,P3)
85#else
86 #define TTRACE0(XX)
87 #define TTRACE1(XX,P1)
88 #define TTRACE2(XX,P1,P2)
89 #define TTRACE3(XX,P1,P2,P3)
90 #define TTRACE1A(XX,P1)
91 #define TTRACE2A(XX,P1,P2)
92 #define TTRACE3A(XX,P1,P2,P3)
93#endif
94
95#ifndef __AFX_H__
96 #ifndef _DEBUG
97 #define DEBUG_ONLY(XX)
98 #define TRACE0(XX)
99 #define TRACE1(XX,P1)
100 #define TRACE2(XX,P1,P2)
101 #define TRACE3(XX,P1,P2,P3)
102 #else
103 #define DEBUG_ONLY(XX) (XX)
104 #define TRACE0(XX) ::OutputDebugString(_T(XX))
105 #define TRACE1(XX,P1) do { TCHAR* _b=new TCHAR[1024]; TNB::STRLIB::PrintF(_b,1024,_T(XX),P1); ::OutputDebugString(_b); delete[] _b; } while ( false )
106 #define TRACE2(XX,P1,P2) do { TCHAR* _b=new TCHAR[1024]; TNB::STRLIB::PrintF(_b,1024,_T(XX),P1,P2); ::OutputDebugString(_b); delete[] _b; } while ( false )
107 #define TRACE3(XX,P1,P2,P3) do { TCHAR* _b=new TCHAR[1024]; TNB::STRLIB::PrintF(_b,1024,_T(XX),P1,P2,P3); ::OutputDebugString(_b); delete[] _b; } while ( false )
108 #endif
109 #ifndef ASSERT
110 #define ASSERT(XX)
111 #endif
112 #ifndef VERIFY
113 #define VERIFY(XX) (XX)
114 #endif
115#endif
116
117
118#if defined(_DEBUG)
119 #if !defined(_WIN32_WCE)
120 #include <crtdbg.h>
121 #define ASSERT_REP(func,msg) _CrtDbgReport(_CRT_ASSERT,__FILE__,__LINE__,func,msg)
122 #define ASSERT_CHK(func,msg) \
123 do { if ( ASSERT_REP(func,msg)==1 ) { DebugBreak(); } } while ( false )
124 #define ASSERT0(expr,func,msg) \
125 do { if ( !(expr) ) { ASSERT_CHK(func,msg); } } while ( false )
126 #define ASSERT1(expr,func,msg,p1) \
127 do { if ( !(expr) ) { char* _b=new char[1024]; STRLIB::PrintF(_b,1024,msg,p1); ASSERT_CHK(func,_b); delete[] _b; } } while ( false )
128 #define ASSERT2(expr,func,msg,p1,p2) \
129 do { if ( !(expr) ) { char* _b=new char[1024]; STRLIB::PrintF(_b,1024,msg,p1,p2); ASSERT_CHK(func,_b); delete[] _b; } } while ( false )
130 #define ASSERT3(expr,func,msg,p1,p2,p3) \
131 do { if ( !(expr) ) { char* _b=new char[1024]; STRLIB::PrintF(_b,1024,msg,p1,p2,p3); ASSERT_CHK(func,_b); delete[] _b; } } while ( false )
132 #else
133 #ifdef __AFX_H__
134 #include <altcecrt.h>
135 #define ASSERT_REP(func,msg) (OutputDebugString(msg), OutputDebugString(_T("\r\n")), ::AfxAssertFailedLine(__FILE__,__LINE__))
136 #else
137 #define ASSERT_REP(func ,msg) (OutputDebugString(msg), OutputDebugString(_T("\r\n")), DBGCHK(func, 0), 1)
138 #endif
139 #define ASSERT_CHK(func,msg) \
140 do { if ( ASSERT_REP(func,msg)==1 ) { DebugBreak(); } } while ( false )
141 #define ASSERT0(expr,func,msg) \
142 do { if ( !(expr) ) { ASSERT_CHK(func,_T(msg)); } } while ( false )
143 #define ASSERT1(expr,func,msg,p1) \
144 do { if ( !(expr) ) { TCHAR* _b=new TCHAR[1024]; STRLIB::PrintF(_b,1024,_T(msg),p1); ASSERT_CHK(func,_b); delete[] _b; } } while ( false )
145 #define ASSERT2(expr,func,msg,p1,p2) \
146 do { if ( !(expr) ) { TCHAR* _b=new TCHAR[1024]; STRLIB::PrintF(_b,1024,_T(msg),p1,p2); ASSERT_CHK(func,_b); delete[] _b; } } while ( false )
147 #define ASSERT3(expr,func,msg,p1,p2,p3) \
148 do { if ( !(expr) ) { TCHAR* _b=new TCHAR[1024]; STRLIB::PrintF(_b,1024,_T(msg),p1,p2,p3); ASSERT_CHK(func,_b); delete[] _b; } } while ( false )
149 #endif
150
151 #define ASSERTLIB(XX) ASSERT0(XX, "TNB Library", "TNB Library のバグの可能性があります。")
152 #define VERIFYLIB(XX) ASSERTLIB(XX)
153 #undef ASSERT
154 #define ASSERT(XX) ASSERT1(XX, NULL, "[%s]\n\n上記箇所でアサートしました。", _T(#XX))
155 #if !defined(_WIN32_WCE)
156 #define TRACE1A(XX,P1) do { char* _b=new char[1024]; TNB::STRLIB::PrintF(_b,1024,XX,P1); ::OutputDebugStringA(_b); delete[] _b; } while ( false )
157 #define TRACE2A(XX,P1,P2) do { char* _b=new char[1024]; TNB::STRLIB::PrintF(_b,1024,XX,P1,P2); ::OutputDebugStringA(_b); delete[] _b; } while ( false )
158 #define TRACE3A(XX,P1,P2,P3) do { char* _b=new char[1024]; TNB::STRLIB::PrintF(_b,1024,XX,P1,P2,P3); ::OutputDebugStringA(_b); delete[] _b; } while ( false )
159 #else
160 #define TRACE1A(XX,P1)
161 #define TRACE2A(XX,P1,P2)
162 #define TRACE3A(XX,P1,P2,P3)
163 #endif
164#else
165 #define ASSERT0(expr,func,msg)
166 #define ASSERT1(expr,func,msg,p1)
167 #define ASSERT2(expr,func,msg,p1,p2)
168 #define ASSERT3(expr,func,msg,p1,p2,p3)
169 #define ASSERTLIB(XX)
170 #define VERIFYLIB(XX) (XX)
171 #define TRACE1A(XX,P1)
172 #define TRACE2A(XX,P1,P2)
173 #define TRACE3A(XX,P1,P2,P3)
174#endif
175
176#if defined(_DEBUG)
177 #define _GetLastError(XX) TNB::_LastErrorTrace(_T(__FILE__),__LINE__,_T(XX))
178#else
179 #define _GetLastError(XX) ::GetLastError()
180#endif
181
182#define _BDMSG0(x) #x
183#define _BDMSG(x) _BDMSG0(x)
184
185// プロセスで唯一宣言
186#define _SELECTANY _declspec(selectany)
187
188#ifndef countof
189 #define countof(X) (sizeof(X) / sizeof((X)[0]))
190#endif
191
192/*
193VC6.0 : 1200
194VC7.0 : 1300
195VC7.1 : 1310
196VC8.0 : 1400
197*/
198#if _MSC_VER < 1300
199 //====VC6.0以下
200 #define __MSVCPP6__
201 #define __noop (void)0
202 #define for if ( false ); else for // for内の変数宣言を標準にする
203 #define DEFSUPER(XX) typedef XX _super; typedef XX __super
204 #define _deprecated
205 #define DEFPARENTLISTENER(PARENT, INST) typedef IListener INST
206 #if defined(UNICODE) || defined(_UNICODE)
207 #ifndef _USRDLL
208 #pragma comment(linker, "/entry:\"wWinMainCRTStartup\"")
209 #endif
210 #endif
211#else
212 //====VC7.0以上
213 #define DEFSUPER(XX) typedef XX _super
214 #define _deprecated __declspec(deprecated)
215 #define DEFPARENTLISTENER(PARENT, INST) typedef PARENT::IListener INST
216 #if defined(__AFX_H__) && defined(_UNICODE) && defined(_DEBUG)
217 //== MFC & UNICODE & DEBUG
218 #include <wchar.h>
219 #undef TRACE0
220 #undef TRACE1
221 #undef TRACE2
222 #undef TRACE3
223 #define TRACE0(XX) ::OutputDebugStringW(_T(XX))
224 #define TRACE1(XX,P1) do { WCHAR* _b=new WCHAR[1024]; TNB::STRLIB::PrintF(_b,1024,_T(XX),P1); ::OutputDebugStringW(_b); delete[] _b; } while ( false )
225 #define TRACE2(XX,P1,P2) do { WCHAR* _b=new WCHAR[1024]; TNB::STRLIB::PrintF(_b,1024,_T(XX),P1,P2); ::OutputDebugStringW(_b); delete[] _b; } while ( false )
226 #define TRACE3(XX,P1,P2,P3) do { WCHAR* _b=new WCHAR[1024]; TNB::STRLIB::PrintF(_b,1024,_T(XX),P1,P2,P3); ::OutputDebugStringW(_b); delete[] _b; } while ( false )
227 #endif
228 #if _MSC_VER >= 1400
229 //===VC8.0以上
230 #pragma warning ( disable : 4996 )
231 #endif
232#endif
233
234#ifdef _MANAGED
235 #pragma warning ( disable : 4793 )
236 #pragma comment(lib,"user32.lib")
237#endif
238
239#ifdef _WIN32_WCE
240 #ifndef WM_PRINTCLIENT
241 #define WM_PRINTCLIENT -1
242 #endif
243#endif
244
245#ifndef WM_THEMECHANGED
246 #define WM_THEMECHANGED 0x031A
247#endif
248
249#ifndef DWORD_MAX
250 #define DWORD_MAX 0xffffffffUL
251#endif
252
253#ifndef DWORD_PTR
254 #define DWORD_PTR ULONG_PTR
255#endif
256
257#ifndef INVALID_SET_FILE_POINTER
258 #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
259#endif
260
261#ifndef _WIN64
262 typedef unsigned long ULONG_PTR, *PULONG_PTR;
263#endif
264
265#ifndef _BASETSD_H_
266 typedef int INT_PTR, *PINT_PTR;
267#endif
268
269#ifndef WS_EX_LAYERED
270 #define WS_EX_LAYERED 0x80000
271#endif
272
273typedef size_t INDEX;
274#define INVALID_SIZE ((size_t)(-1))
275#define INVALID_INDEX INVALID_SIZE
276inline bool IsValid(size_t sz) { return sz != INVALID_SIZE; }
277inline bool IsValid(HANDLE h) { return h != INVALID_HANDLE_VALUE; }
278template<typename TYP> inline bool IsInvalid(TYP t) { return ! IsValid(t); }
279#ifndef CLR_INVALID
280 #define CLR_INVALID 0xFFFFFFFF
281#endif
282#define CLR_AUTOSELECT 0xFEFFFFFF
283#define IS_RGBVALUE(RGB) (((RGB)&0xFF000000)==0)
284
285#endif //_TnbDOXYGEN
286
287
288
297#define MESSAGE(d) message(__FILE__"("_BDMSG(__LINE__)") : message:" d)
298
299
300
307#define _BIT(X) (1ui64<<(X))
308
309
310
321#define _BIN(X) (TBinaryNotation<0##X>::VALUE)
322
323
324
343#define loop(VAR, CNT) for ( size_t VAR = 0, VAR##_size = CNT; VAR < VAR##_size; VAR++ )
344
355#define loop_dn(VAR, CNT) for ( size_t VAR##_r = 0, VAR##_size = CNT, VAR = CNT - 1; VAR##_r < VAR##_size; VAR--, VAR##_r++ )
356
357
358
360namespace TNB
361{
362
363
364
375template<typename T>
376inline void MemCopy(T* _pDst, const void* pSrc, size_t len)
377{
378 if ( len != 0 )
379 {
380 ASSERT( ! ::IsBadReadPtr(pSrc, sizeof(T) * len) );
381 ASSERT( ! ::IsBadWritePtr(_pDst, sizeof(T) * len) );
382 const T* P = static_cast<const T*>(pSrc);
383 while ( (len--) > 0 )
384 {
385 *_pDst++ = *P++;
386 }
387 }
388}
389
390
391
398template<typename V>
399inline void Zero(V& value)
400{
401 BYTE* B = reinterpret_cast<BYTE*>(&value);
402 size_t l = sizeof(V);
403 loop ( i, l )
404 {
405 *B++ = 0;
406 }
407}
408
409
410
421inline bool IsInRange(INDEX value, size_t size)
422{
423 return value < size;
424}
425
426
427
434template<typename T> inline void IgnoreUnusedValue(const T& value)
435{
436}
437
438
439
440#ifndef _TnbDOXYGEN //Document作成用シンボル
441
442// オーバーフローチェック付き、ダウンキャスト
443template<typename TYP>
444class down_cast
445{
446public:
447 template<typename SRC>
448 down_cast(SRC src) : m_typ(static_cast<TYP>(src))
449 {
450 ASSERT1( (SRC)(m_typ) == src, "down_cast<>", "オーバーフローしました。\nsource = %d", src );
451 }
452 down_cast(float src) : m_typ(static_cast<TYP>(src))
453 {
454 ASSERT1( (LONGLONG)(m_typ) == LONGLONG(src), "down_cast<>", "オーバーフローしました。\nsource = %f", src );
455 }
456 down_cast(double src) : m_typ(static_cast<TYP>(src))
457 {
458 ASSERT1( (LONGLONG)(m_typ) == LONGLONG(src), "down_cast<>", "オーバーフローしました。\nsource = %f", src );
459 }
460 operator TYP(void) const
461 {
462 return m_typ;
463 }
464private:
465 TYP m_typ;
466};
467
469template<typename T> inline const T* Const(T* P)
470{
471 return const_cast<const T*>(P);
472}
473
475template<typename T> inline int ToInt(T t)
476{
477 return down_cast<int>(t);
478}
479
481template<> inline int ToInt(LONGLONG ll)
482{
483 return down_cast<int>(ll);
484}
485
487template<typename T> inline WORD ToWord(T t)
488{
489 return down_cast<WORD>(t);
490}
491
493template<typename T> inline DWORD ToDword(T t)
494{
495 return down_cast<DWORD>(t);
496}
497
499template<typename T> inline INDEX ToIndex(T t)
500{
501 return down_cast<INDEX>(t);
502}
503
505template<typename T1, typename T2> inline void _deprecated ForceSet(T1& t1, const T2& t2)
506{
507 t1 = (const T1)(t2);
508}
509
510#ifdef __MSVCPP6__
512inline LONG InterlockedCompareExchange(LONG* _plDes, LONG lExchange, LONG lComperand)
513{
514 typedef LONG (WINAPI* P_CompExc)(LONG*, LONG, LONG);
515 P_CompExc pCompExc = reinterpret_cast<P_CompExc>(&::InterlockedCompareExchange);
516 return pCompExc(_plDes, lExchange, lComperand);
517}
518#endif
519
521template<UINT n> struct TBinaryNotation {
522 enum {
523 BIT = n % 8u,
524 VALUE = BIT + (TBinaryNotation<n / 8u>::VALUE << 1u)
525 };
526};
527template<> struct TBinaryNotation<0> {
528 enum {BIT = 0, VALUE = 0};
529};
530
531#ifdef __DEVICERESOLUTIONAWARE_H__
533inline int CalcScaleX(int x) { return DRA::SCALEX(x); }
534inline int CalcScaleX(int x, HDC dc) { return DRA::SCALEX(x); }
536inline int CalcScaleY(int y) { return DRA::SCALEY(y); }
537inline int CalcScaleY(int y, HDC dc) { return DRA::SCALEY(y); }
538#else
540inline int CalcScaleX(int x, HDC dc)
541{
542 return static_cast<int>(x * ::GetDeviceCaps(dc, LOGPIXELSX) / 96.0);
543}
545inline int CalcScaleX(int x)
546{
547 HDC dc = ::GetDC(NULL);
548 int r = CalcScaleX(x, dc);
549 ::ReleaseDC(NULL, dc);
550 return r;
551}
553inline int CalcScaleY(int y, HDC dc)
554{
555 return static_cast<int>(y * ::GetDeviceCaps(dc, LOGPIXELSY) / 96.0);
556}
558inline int CalcScaleY(int y)
559{
560 HDC dc = ::GetDC(NULL);
561 int r = CalcScaleY(y, dc);
562 ::ReleaseDC(NULL, dc);
563 return r;
564}
565#endif
567inline void CalcScale(RECT& _rc)
568{
569 _rc.left = CalcScaleX(_rc.left);
570 _rc.right = CalcScaleX(_rc.right);
571 _rc.top = CalcScaleY(_rc.top);
572 _rc.bottom = CalcScaleY(_rc.bottom);
573}
575inline void CalcScale(SIZE& _sz)
576{
577 _sz.cx = CalcScaleX(_sz.cx);
578 _sz.cy = CalcScaleY(_sz.cy);
579}
581inline void CalcScale(POINT& _po)
582{
583 _po.x = CalcScaleX(_po.x);
584 _po.y = CalcScaleY(_po.y);
585}
586
587#endif //_TnbDOXYGEN
588
589
590
599{
609 void operator=(const CCopyImpossible& T);
610public:
615};
616
617
618
631template<typename TYP>
633{
634 size_t m_size;
635 TYP* m_lpBuf;
636public:
641 CWorkMemT(void) : m_size(0), m_lpBuf(NULL) {}
646 CWorkMemT(size_t l) : m_size(l), m_lpBuf(new TYP[(l == 0) ? 1 : l])
647 {}
649 ~CWorkMemT(void) { Free(); }
652 void Free(void)
653 {
654 if ( m_lpBuf != NULL )
655 {
656 delete[] m_lpBuf;
657 m_lpBuf = NULL;
658 m_size = 0;
659 }
660 }
665 size_t GetSize(void) const { return m_size; }
672 void Resize(size_t l)
673 {
674 if ( l != m_size )
675 {
676 Free();
677 m_size = l;
678 if ( l > 0 )
679 {
680 m_lpBuf = new TYP[l];
681 }
682 }
683 }
690 void Reset(size_t l, const TYP* P)
691 {
692 Resize(l);
693 MemCopy(m_lpBuf, P, l);
694 }
700 operator const TYP*(void) const { return m_lpBuf; }
706 operator TYP*(void) { return m_lpBuf; }
712 const TYP* Ref(void) const { return m_lpBuf; }
718 TYP* Ref(void) { return m_lpBuf; }
724 void Fill(const TYP& other)
725 {
726 loop ( i ,m_size )
727 {
728 m_lpBuf[i] = other;
729 }
730 }
736 void Swap(CWorkMemT& _other)
737 {
738 TNB::Swap(m_size, _other.m_size);
739 TNB::Swap(m_lpBuf, _other.m_lpBuf);
740 }
741};
742
743
744
757
758
759
772{
773 CWorkMemT<TCHAR> m_str;
774public:
779 CSimpleStr(LPCTSTR lpsz = NULL)
780 {
781 operator=(lpsz);
782 }
787 CSimpleStr(const CSimpleStr& other)
788 {
789 operator=(other.m_str.Ref());
790 }
798 {
799 return operator=(other.m_str.Ref());
800 }
807 CSimpleStr& operator=(LPCTSTR lpsz)
808 {
809 if ( lpsz == NULL )
810 {
811 m_str.Free();
812 }
813 else
814 {
815 size_t l = _tcslen(lpsz) + 1;
816 m_str.Resize(l);
817 LPTSTR P = m_str;
818 MemCopy(P, lpsz, l);
819 }
820 return *this;
821 }
827 bool IsEmpty(void) const
828 {
829 LPCTSTR P = m_str;
830 return P == NULL || *P == 0;
831 }
835 void Empty(void) { m_str.Free(); }
840 LPCTSTR Ref(void) const { return m_str; }
845 operator LPCTSTR(void) const { return m_str; }
850 operator LPTSTR(void) { return m_str; }
851};
852
853
854
869{
870 CWorkMemT<char> m_str;
871public:
876 CSimpleAscii(LPCSTR lpsz = NULL)
877 {
878 if ( lpsz != NULL ) { operator=(lpsz); }
879 }
885 {
886 operator=(other.m_str.Ref());
887 }
895 {
896 return operator=(other.m_str.Ref());
897 }
905 {
906 if ( lpsz == NULL )
907 {
908 m_str.Free();
909 }
910 else
911 {
912 size_t l = strlen(lpsz) + 1;
913 m_str.Resize(l);
914 LPSTR P = m_str;
915 MemCopy(P, lpsz, l);
916 }
917 return *this;
918 }
924 bool IsEmpty(void) const
925 {
926 LPCSTR P = m_str;
927 return P == NULL || *P == 0;
928 }
932 void Empty(void) { m_str.Free(); }
937 LPCSTR Ref(void) const { return m_str; }
942 operator LPCSTR(void) const { return m_str; }
947 operator LPSTR(void) { return m_str; }
948};
949
950
951
963template<typename T> void Swap(T& t1, T& t2)
964{
965 T x = t1;
966 t1 = t2;
967 t2 = x;
968}
969
970
971
981inline void SystemErrorToMessageText(CSimpleStr& _str, DWORD dwError)
982{
983 LPTSTR lpMsgBuf = NULL;
984 LPTSTR lpBuff = reinterpret_cast<LPTSTR>(&lpMsgBuf);
985 DWORD flag = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
986 DWORD r = ::FormatMessage(flag, NULL, dwError, LANG_USER_DEFAULT, lpBuff, 0, NULL);
987 if ( r == 0 )
988 {
989 _str = _T("Unknown");
990 }
991 else
992 {
993 _str = lpMsgBuf;
994 LPTSTR P = _str;
995 size_t l = _tcslen(P);
996 loop ( i, l )
997 {
998 if ( *P == '\n' || *P == '\r' )
999 {
1000 *P = ' ';
1001 }
1002 P++;
1003 }
1004 ::LocalFree(lpMsgBuf);
1005 }
1006}
1007
1008
1009
1018inline WORD SwapEndian(WORD val)
1019{
1020 WORD ret;
1021 *(reinterpret_cast<BYTE*>(&ret) + 0) = *(reinterpret_cast<BYTE*>(&val) + 1);
1022 *(reinterpret_cast<BYTE*>(&ret) + 1) = *(reinterpret_cast<BYTE*>(&val) + 0);
1023 return ret;
1024}
1025
1026
1027
1036inline DWORD SwapEndian(DWORD val)
1037{
1038 DWORD ret;
1039 *(reinterpret_cast<BYTE*>(&ret) + 0) = *(reinterpret_cast<BYTE*>(&val) + 3);
1040 *(reinterpret_cast<BYTE*>(&ret) + 1) = *(reinterpret_cast<BYTE*>(&val) + 2);
1041 *(reinterpret_cast<BYTE*>(&ret) + 2) = *(reinterpret_cast<BYTE*>(&val) + 1);
1042 *(reinterpret_cast<BYTE*>(&ret) + 3) = *(reinterpret_cast<BYTE*>(&val) + 0);
1043 return ret;
1044}
1045
1046
1047
1056inline ULONGLONG SwapEndian(ULONGLONG val)
1057{
1058 ULONGLONG ret = 0;
1059 ret = (ULONGLONG) (SwapEndian((DWORD)(val >> 32))) << 32;
1060 ret |= (SwapEndian((DWORD)(val & 0x00000000FFFFFFFF)));
1061 return ret;
1062}
1063
1064
1065
1067namespace STRLIB
1068{
1080 inline bool VPrintF(LPSTR _pWork, size_t iLen, LPCSTR lpFmt, va_list V)
1081 {
1082 *_pWork = 0;
1083 #if _MSC_VER < 1400 || defined(_WIN32_WCE)
1084 return vsprintf(_pWork, lpFmt, V) >= 0;
1085 #else
1086 return vsprintf_s(_pWork, iLen, lpFmt, V) >= 0;
1087 #endif
1088 }
1089
1101 inline bool PrintF(LPSTR _pWork, size_t iLen, LPCSTR lpFmt, ...)
1102 {
1103 va_list args;
1104 va_start(args, lpFmt);
1105 bool r = VPrintF(_pWork, iLen, lpFmt, args);
1106 va_end(args);
1107 return r;
1108 }
1109
1121 inline bool VPrintF(LPWSTR _pWork, size_t iLen, LPCWSTR lpFmt, va_list V)
1122 {
1123 *_pWork = 0;
1124 #if _MSC_VER < 1400 || defined(_WIN32_WCE)
1125 return vswprintf(_pWork, lpFmt, V) >= 0;
1126 #else
1127 return vswprintf_s(_pWork, iLen, lpFmt, V) >= 0;
1128 #endif
1129 }
1130
1142 inline bool PrintF(LPWSTR _pWork, size_t iLen, LPCWSTR lpFmt, ...)
1143 {
1144 va_list args;
1145 va_start(args, lpFmt);
1146 bool r = VPrintF(_pWork, iLen, lpFmt, args);
1147 va_end(args);
1148 return r;
1149 }
1150
1159 inline void IdToString(LPSTR _pWork, DWORD dw)
1160 {
1161 while( dw != 0 )
1162 {
1163 *_pWork++ = static_cast<char>((dw % 26) + 'A');
1164 dw /= 26;
1165 }
1166 *_pWork = 0;
1167 }
1168
1177 inline void IdToString(LPWSTR _pWork, DWORD dw)
1178 {
1179 while( dw != 0 )
1180 {
1181 *_pWork++ = static_cast<WCHAR>((dw % 26) + 'A');
1182 dw /= 26;
1183 }
1184 *_pWork = 0;
1185 }
1186
1187}; // STRLIB
1188
1189
1190
1191#ifndef _TnbDOXYGEN //Document作成用シンボル
1192
1194inline DWORD _LastErrorTrace(LPCTSTR lpszFile, int line, LPCTSTR lpsz)
1195{
1196 DWORD dwErr = ::GetLastError();
1197 if ( dwErr != ERROR_SUCCESS )
1198 {
1199 CSimpleStr s;
1200 SystemErrorToMessageText(s, dwErr);
1201 TRACE2( "%s(%d): ", lpszFile, line );
1202 TRACE3( "API[%s()]失敗 Last Error Code = 0x%08x(%s)\n", lpsz, dwErr, s.Ref() );
1203 ::SetLastError(dwErr);
1204 }
1205 return dwErr;
1206}
1207
1208inline BOOL _DeleteObject(HGDIOBJ h)
1209{
1210 #ifdef _DEBUG
1211 if ( ::GetObjectType(h) == NULL )
1212 {
1213 ASSERT0( false, "DeleteObject", "破棄されたGDIオブジェクトを破棄しようとしています。" );
1214 return false;
1215 }
1216 BOOL r = ::DeleteObject(h);
1217 ASSERT1( r, "DeleteObject", "GDIオブジェクトの破棄に失敗しました。\ncode = %d\ntype = 0x%08X", ::GetLastError() );
1218 return r;
1219 #else
1220 return ::DeleteObject(h);
1221 #endif
1222}
1223
1224
1225#endif // _TnbDOXYGEN
1226
1227
1228
1230namespace HASH
1231{
1238 inline DWORD CalcHash(size_t iLen, LPCVOID P)
1239 {
1240 const BYTE* B = static_cast<const BYTE*>(P);
1241 DWORD dwRc = 0;
1242 loop ( i, iLen )
1243 {
1244 dwRc += B[i] << ((i + B[0]) & 0x0F);
1245 }
1246 return dwRc;
1247 }
1248};
1249
1250
1251
1258{
1263};
1264
1265
1266
1267#ifndef _TnbDOXYGEN //Document作成用シンボル
1268
1269 struct TTnbParam
1270 {
1271 LONG_PTR* plNullBase;
1272 LONG_PTR alNullBase[8];
1273 HINSTANCE ahInstances[4];
1274 };
1275 _SELECTANY TTnbParam g_tnbParam = { g_tnbParam.alNullBase };
1276 _SELECTANY TTnbParam* g_pTnbParam = &g_tnbParam;
1277
1278 #define g_plNullBase g_pTnbParam->plNullBase
1279
1280 // NULLエリア確認マクロ
1281 #define ASEERT_NULLBASE ASSERTLIB((g_plNullBase[0]|g_plNullBase[1]|g_plNullBase[2]|g_plNullBase[3]| \
1282 g_plNullBase[4]|g_plNullBase[5]|g_plNullBase[6]|g_plNullBase[7])==0);
1283
1284#endif //_TnbDOXYGEN
1285
1286
1287
1296inline void InitializeTnbLibrary(void)
1297{
1298 if ( g_pTnbParam == &g_tnbParam )
1299 {
1300 TCHAR buf[20] = _T("_!TnbLib!_"); //10文字
1301 STRLIB::IdToString(&buf[10], ::GetCurrentProcessId());
1302 HANDLE hMap = ::CreateFileMapping(reinterpret_cast<HANDLE>(-1), NULL, PAGE_READWRITE, 0, sizeof(TTnbParam), buf);
1303 ASSERT0( hMap != NULL, "InitializeTnbLibrary", "管理メモリが確保できませんでした。" );
1304 bool isFirst = (::GetLastError() != ERROR_ALREADY_EXISTS);
1305 g_pTnbParam = static_cast<TTnbParam*>(::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0));
1306 ASSERT0( g_pTnbParam != NULL, "InitializeTnbLibrary", "管理メモリが確保できませんでした。" );
1307 if ( isFirst )
1308 {
1309 g_pTnbParam->plNullBase = g_pTnbParam->alNullBase;
1310 }
1311 }
1312}
1313
1314
1315
1324inline void SetInstanceHandleByTnb(EInstanceType type, HINSTANCE hInstance)
1325{
1326 g_pTnbParam->ahInstances[type] = hInstance;
1327}
1328
1329
1330
1342{
1343 if ( g_pTnbParam->ahInstances[type] == NULL )
1344 {
1345 #ifdef __AFX_H__
1346 g_pTnbParam->ahInstances[type] = ::AfxGetInstanceHandle();
1347 #else
1348 g_pTnbParam->ahInstances[type] = g_pTnbParam->ahInstances[EI_Process];
1349 #endif
1350 }
1351 return g_pTnbParam->ahInstances[type];
1352}
1353
1354
1355
1357namespace MFC {};
1358
1360namespace DX {};
1361
1362
1363
1364}; // TNB
1365
1366
1367
1368#ifndef _TnbUSINGNAMESPACE_DISABLE
1369 using namespace TNB;
1370 using namespace TNB::MFC;
1371 using namespace TNB::DX;
1372#endif
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
~CCopyImpossible(void)
デストラクタ
Definition: TnbDef.h:614
CCopyImpossible(void)
デフォルトコンストラクタ
Definition: TnbDef.h:612
簡易文字列管理クラス.
Definition: TnbDef.h:869
CSimpleAscii & operator=(LPCSTR lpsz)
[代入] 代入.
Definition: TnbDef.h:904
CSimpleAscii(const CSimpleAscii &other)
コンストラクタ
Definition: TnbDef.h:884
bool IsEmpty(void) const
[確認] 空チェック
Definition: TnbDef.h:924
CSimpleAscii(LPCSTR lpsz=NULL)
コンストラクタ
Definition: TnbDef.h:876
LPCSTR Ref(void) const
[取得] 文字列参照
Definition: TnbDef.h:937
void Empty(void)
[操作] 空化
Definition: TnbDef.h:932
CSimpleAscii & operator=(const CSimpleAscii &other)
[代入] 代入.
Definition: TnbDef.h:894
簡易文字列管理クラス.
Definition: TnbDef.h:772
CSimpleStr & operator=(LPCTSTR lpsz)
[代入] 代入.
Definition: TnbDef.h:807
bool IsEmpty(void) const
[確認] 空チェック
Definition: TnbDef.h:827
CSimpleStr(const CSimpleStr &other)
コンストラクタ
Definition: TnbDef.h:787
LPCTSTR Ref(void) const
[取得] 文字列参照
Definition: TnbDef.h:840
CSimpleStr(LPCTSTR lpsz=NULL)
コンストラクタ
Definition: TnbDef.h:779
void Empty(void)
[操作] 空化
Definition: TnbDef.h:835
CSimpleStr & operator=(const CSimpleStr &other)
[代入] 代入.
Definition: TnbDef.h:797
ワークメモリテンプレート.
Definition: TnbDef.h:633
~CWorkMemT(void)
デストラクタ
Definition: TnbDef.h:649
size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbDef.h:665
void Fill(const TYP &other)
[設定] 全情報代入.
Definition: TnbDef.h:724
CWorkMemT(size_t l)
コンストラクタ
Definition: TnbDef.h:646
void Swap(CWorkMemT &_other)
[設定] スワップ.
Definition: TnbDef.h:736
TYP * Ref(void)
[取得] ポインタ取得
Definition: TnbDef.h:718
void Resize(size_t l)
[設定] サイズ再設定
Definition: TnbDef.h:672
CWorkMemT(void)
コンストラクタ.
Definition: TnbDef.h:641
void Free(void)
[設定] 解放.
Definition: TnbDef.h:652
void Reset(size_t l, const TYP *P)
[設定] 再設定
Definition: TnbDef.h:690
const TYP * Ref(void) const
[取得] ポインタ取得
Definition: TnbDef.h:712
CWorkMemT< BYTE > CWorkMem
ワークメモリ.
Definition: TnbDef.h:756
DWORD ToDword(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:395
int ToInt(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:367
void SystemErrorToMessageText(CSimpleStr &_str, DWORD dwError)
[変換] SystemErrorコード文字列化.
Definition: TnbDef.h:981
void Swap(T &t1, T &t2)
[変換] スワッパー.
Definition: TnbDef.h:963
bool IsInRange(INDEX value, size_t size)
[確認] 範囲チェック.
Definition: TnbDef.h:421
void IgnoreUnusedValue(const T &value)
[宣言] 参照しない値宣言.
Definition: TnbDef.h:434
void Zero(V &value)
[設定] ゼロクリア.
Definition: TnbDef.h:399
WORD SwapEndian(WORD val)
[変換] エンディアン変換.
Definition: TnbDef.h:1018
EInstanceType
インスタンスハンドル種.
Definition: TnbDef.h:1258
HINSTANCE GetInstanceHandleByTnb(EInstanceType type=EI_Process)
[取得] インスタンスハンドル取得.
Definition: TnbDef.h:1341
void SetInstanceHandleByTnb(EInstanceType type, HINSTANCE hInstance)
[設定] インスタンスハンドル指定.
Definition: TnbDef.h:1324
@ EI_Icon
アイコンリソース用
Definition: TnbDef.h:1261
@ EI_Bitmap
ビットマップリソース用
Definition: TnbDef.h:1260
@ EI_Process
プロセス用
Definition: TnbDef.h:1259
@ EI_String
文字列リソース用
Definition: TnbDef.h:1262
TNB DirectX Library
Definition: TnbDef.h:1360
DWORD CalcHash(const IConstCollectionT< TYP > &c)
COLLECT [HASH] ハッシュ値計算.
TNB MFC Library
Definition: TnbFAQ.txt:67
void IdToString(LPSTR _pWork, DWORD dw)
[作成] 識別用文字列作成(ASCII用) 32bit文字を一意の文字列に変換します。
Definition: TnbDef.h:1159
bool VPrintF(LPSTR _pWork, size_t iLen, LPCSTR lpFmt, va_list V)
[作成] 書式付き文字列作成(ASCII/SJIS用)
Definition: TnbDef.h:1080
bool PrintF(LPSTR _pWork, size_t iLen, LPCSTR lpFmt,...)
[作成] 書式付き文字列作成(ASCII/SJIS用)
Definition: TnbDef.h:1101
TNB Library
Definition: TnbDoxyTitle.txt:2
void MemCopy(T *_pDst, const void *pSrc, size_t len)
[複製] メモリコピー
Definition: TnbDef.h:376
void InitializeTnbLibrary(void)
[設定] ライブラリ初期化.
Definition: TnbDef.h:1296