TNB Library
TnbUnion.h
[詳解]
1#pragma once
11#include "TnbPointerHandle.h"
12#include "TnbException.h"
13
14
15
16//TNB Library
17namespace TNB
18{
19
20
21
64class CUnion
65{
66public:
67
69 struct IFuncBase
70 {
72 virtual ~IFuncBase(void) {}
74 virtual void Destractor(LPVOID V) = 0;
75 };
76
77
78 //-----------------
79
80
85 CUnion(void) : m_hpHead(new THead)
86 {
87 }
88
96 template<typename T>
97 CUnion(const T& t, DWORD dwParam = 0) : m_hpHead(new THead)
98 {
99 m_hpHead->Set(t, dwParam, new CFuncT<T>);
100 }
101
106 CUnion(const CUnion& other) : m_hpHead(other.m_hpHead)
107 {
108 }
109
112 {
113 Empty();
114 }
115
121 CUnion& operator=(const CUnion& other)
122 {
123 m_hpHead = other.m_hpHead;
124 return *this;
125 }
126
131 void Empty(void)
132 {
133 m_hpHead = new THead;
134 }
135
145 template<typename T>
146 void Set(const T& t, DWORD dwParam = 0)
147 {
148 m_hpHead = new THead;
149 m_hpHead->Set(t, dwParam, new CFuncT<T>);
150 }
151
156 DWORD GetParam(void) const
157 {
158 return m_hpHead->m_dwParam;
159 }
160
169 template<typename T>
170 bool Get(T& _t) const
171 {
172 #ifdef _DEBUG
173 if ( strcmp(m_hpHead->m_strType, typeid(T).name()) != 0 )
174 {
175 return false; //型が異なる
176 }
177 #endif
178 if ( m_hpHead->m_dwSizeof != sizeof(T) )
179 {
180 return false; //型が異なる
181 }
182 _t = *(static_cast<const T*>(m_hpHead->m_pVal));
183 return true;
184 }
185
194 template<typename T>
195 const T& Ref(T* P) const
196 {
197 #ifdef _DEBUG
198 if ( strcmp(m_hpHead->m_strType, typeid(T).name()) != 0 )
199 {
200 throw CNoSuchTypeException(); //型が異なる
201 }
202 #endif
203 if ( m_hpHead->m_dwSizeof != sizeof(T) )
204 {
205 throw CNoSuchTypeException(); //型が異なる
206 }
207 return *(static_cast<const T*>(m_hpHead->m_pVal));
208 }
209
215 LPCSTR GetTypeName(void) const
216 {
217 #ifdef _DEBUG
218 return m_hpHead->m_strType;
219 #else
220 return "";
221 #endif
222 }
223
224protected:
225
226 #ifndef _TnbDOXYGEN //Document作成用シンボル
227
229 struct THead
230 {
231 IFuncBase* m_pFuncBase;
232 LPVOID m_pVal;
233 DWORD m_dwSizeof;
234 DWORD m_dwParam;
235 #ifdef _DEBUG
236 CSimpleAscii m_strType;
237 #endif
239 THead(void) : m_pFuncBase(NULL), m_pVal(NULL), m_dwSizeof(0), m_dwParam(0)
240 {
241 }
243 ~THead(void)
244 {
245 if ( m_pFuncBase != NULL )
246 {
247 m_pFuncBase->Destractor(m_pVal);
248 delete m_pFuncBase;
249 m_pFuncBase = NULL;
250 }
251 }
253 template<typename T>
254 void Set(const T& t, DWORD dwParam, IFuncBase* P = NULL)
255 {
256 m_pFuncBase = P;
257 m_pVal = new T(t);
258 m_dwParam = dwParam;
259 m_dwSizeof = sizeof(T);
260 #ifdef _DEBUG
261 m_strType = typeid(T).name();
262 #endif
263 }
264 };
265
267 template<typename T>
268 struct CFuncT : IFuncBase
269 {
270 virtual void Destractor(LPVOID V)
271 {
272 delete static_cast<T*>(V);
273 }
274 };
275
276 #endif // _TnbDOXYGEN
277
279};
280
281
282
297class CUnionEx : public CUnion
298{
299 DEFSUPER(CUnion);
300public:
301
304 {
306 virtual void ToString(CSimpleStr& _s, LPVOID V) = 0;
307 };
308
309
310 //------------
311
312
317 CUnionEx(void) : _super()
318 {
319 }
320
328 template<typename T>
329 CUnionEx(const T& t, DWORD dwParam = 0) : m_hpHead(new THead)
330 {
331 m_hpHead->Set(t, dwParam, new CFuncT<T>);
332 }
333
338 CUnionEx(const CUnionEx& other) : _super(other)
339 {
340 }
341
351 template<typename T>
352 void Set(const T& t, DWORD dwParam = 0)
353 {
354 m_hpHead = new THead;
355 m_hpHead->Set(t, dwParam, new CFuncT<T>);
356 }
357
363 void ToString(CSimpleStr& _s) const
364 {
365 if ( m_hpHead->m_pFuncBase == NULL )
366 {
367 throw CNullPointerException();
368 }
369 reinterpret_cast<IFuncBaseEx*>(m_hpHead->m_pFuncBase)->ToString(_s, m_hpHead->m_pVal);
370 }
371
372private:
374 template<typename T>
375 struct CFuncT : CUnionEx::IFuncBaseEx
376 {
377 virtual void Destractor(LPVOID V)
378 {
379 delete static_cast<T*>(V);
380 }
381 virtual void ToString(CSimpleStr& _s, LPVOID V)
382 {
383 _s = static_cast<T*>(V)->ToString();
384 }
385 };
386};
387
388
389
390};//TNB
391
392
393
394#if 0
395 int i = 99;
396 CStr s = "A";
397 CString ss ="X";
398 CUnion u(i);
399 CUnion u2(u);
400 TRACE1("%s\n",u.GetTypeName());
401 TRACE1("%s\n",u2.GetTypeName());
402 u.Put(ss);
403 TRACE1("%s\n",u.GetTypeName());
404 u.Put(s);
405 u.Put(s);
406 VERIFY( u.Get(s) );
407 VERIFY( ! u2.Get(s) );
408 TRACE1("%s\n",u.GetTypeName());
409 TRACE1("%s\n",u2.GetTypeName());
410 TRACE1("%d\n",u.Ref((CStr*)0).GetLength());
411 TRACE1("%s\n",u.Ref((CStr*)0));
412 TRACE1("%d\n",u2.Ref((int*)0));
413 u.Put(i);
414 u.Get(i);
415 TRACE1("%s\n",u.GetTypeName());
416 TRACE1("%s\n",u2.GetTypeName());
417#endif
例外状態管理関係のヘッダ
ポインタハンドル関係のヘッダ
タイプ相違例外
Definition: TnbException.h:146
NULLポインタ例外
Definition: TnbException.h:172
簡易文字列管理クラス.
Definition: TnbDef.h:869
簡易文字列管理クラス.
Definition: TnbDef.h:772
UNION管理拡張クラス
Definition: TnbUnion.h:298
CUnionEx(const CUnionEx &other)
コピーコンストラクタ.
Definition: TnbUnion.h:338
CUnionEx(const T &t, DWORD dwParam=0)
コンストラクタ.
Definition: TnbUnion.h:329
void ToString(CSimpleStr &_s) const
[取得] 文字列取得
Definition: TnbUnion.h:363
void Set(const T &t, DWORD dwParam=0)
[設定] 代入.
Definition: TnbUnion.h:352
CUnionEx(void)
コンストラクタ.
Definition: TnbUnion.h:317
UNION管理クラス
Definition: TnbUnion.h:65
CUnion(const CUnion &other)
コピーコンストラクタ.
Definition: TnbUnion.h:106
~CUnion(void)
デストラクタ
Definition: TnbUnion.h:111
LPCSTR GetTypeName(void) const
[取得] 型名取得.
Definition: TnbUnion.h:215
bool Get(T &_t) const
[取得] 取得.
Definition: TnbUnion.h:170
const T & Ref(T *P) const
[取得] 参照取得.
Definition: TnbUnion.h:195
CUnion(const T &t, DWORD dwParam=0)
コンストラクタ.
Definition: TnbUnion.h:97
CUnion(void)
コンストラクタ.
Definition: TnbUnion.h:85
CUnion & operator=(const CUnion &other)
[代入] コピーオペレータ.
Definition: TnbUnion.h:121
CPointerHandleT< THead > m_hpHead
実体参照ハンドル
Definition: TnbUnion.h:278
void Empty(void)
[設定] 空化.
Definition: TnbUnion.h:131
DWORD GetParam(void) const
[取得] パラメータ取得.
Definition: TnbUnion.h:156
void Set(const T &t, DWORD dwParam=0)
[設定] 代入.
Definition: TnbUnion.h:146
TNB Library
Definition: TnbDoxyTitle.txt:2
UNION管理拡張クラス用ファンクションインターフェース
Definition: TnbUnion.h:304
virtual void ToString(CSimpleStr &_s, LPVOID V)=0
文字列へ変換
UNION管理クラス用ファンクションインターフェース
Definition: TnbUnion.h:70
virtual ~IFuncBase(void)
デストラクタ
Definition: TnbUnion.h:72
virtual void Destractor(LPVOID V)=0
デストラクト実行