123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- #pragma once
- template <class C, typename T> class TLSHolder
- {
- public:
- TLSHolder ()
- {
- Create ();
- }
- TLSHolder (T * from)
- {
- Create (from);
- }
- ~TLSHolder ()
- {
- Close ();
- }
- public:
- static DWORD TLSIndex;
- protected:
- static T * Create ()
- {
- void * p = TlsGetValue (TLSIndex);
- if (! p)
- {
- p = new T;
- C::Init ((T *) p);
- BOOL rc = TlsSetValue (TLSIndex, p);
- if (! rc)
- {
- #ifdef _DEBUG
- DWORD code = GetLastError ();
- #endif
- delete p;
- }
- }
- return Get ();
- }
- static T * Create (T * from)
- {
- void * p = TlsGetValue (TLSIndex);
- if (! p)
- {
- p = from->Clone ();
- // C::Init ((T *) p);
- BOOL rc = TlsSetValue (TLSIndex, p);
- if (! rc)
- {
- #ifdef _DEBUG
- DWORD code = GetLastError ();
- #endif
- delete p;
- }
- }
- return Get ();
- }
- static void Close ()
- {
- T * p = Get ();
- delete p;
- BOOL rc = TlsSetValue (TLSIndex, 0);
- if (! rc)
- {
- #ifdef _DEBUG
- DWORD code = GetLastError ();
- putchar (' ');
- #endif
- }
- }
- public:
- static T * Get ()
- {
- void * p = TlsGetValue (TLSIndex);
- return (T *) p;
- }
- };
|