home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / MFCINC.PAK / AFXTLS_.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.4 KB  |  242 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1995 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #ifndef __AFXTLS_H__
  12. #define __AFXTLS_H__
  13.  
  14. #ifdef _AFX_PACKING
  15. #pragma pack(push, _AFX_PACKING)
  16. #endif
  17.  
  18. #undef AFX_DATA
  19. #define AFX_DATA AFX_CORE_DATA
  20.  
  21. // Classes declared in this file
  22.  
  23. class CSimpleList;
  24. class CThreadSlotData;                  // for manipulationg thread local storage
  25. class CThreadLocalObject;               // for storing thread/process local data
  26. class CProcessLocalObject;
  27. class CNoTrackObject;
  28. // template class CTypedSimpleList<>
  29. // template class CThreadLocal<>
  30. // template class CProcessLocal<>
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CSimpleList (simple/small subset of CList)
  34.  
  35. class CSimpleList
  36. {
  37. public:
  38.     CSimpleList(int nNextOffset = 0);
  39.     void Construct(int nNextOffset);
  40.  
  41. // Operations
  42.     BOOL IsEmpty() const;
  43.     void AddHead(void* p);
  44.     void RemoveAll();
  45.     void* GetHead() const;
  46.     void* GetNext(void* p) const;
  47.     BOOL Remove(void* p);
  48.  
  49. // Implementation
  50.     void* m_pHead;
  51.     size_t m_nNextOffset;
  52.  
  53.     void** GetNextPtr(void* p) const;   // somewhat trusting...
  54. };
  55.  
  56. inline CSimpleList::CSimpleList(int nNextOffset)
  57.     { m_pHead = NULL; m_nNextOffset = nNextOffset; }
  58. inline void CSimpleList::Construct(int nNextOffset)
  59.     { ASSERT(m_pHead == NULL); m_nNextOffset = nNextOffset; }
  60. inline BOOL CSimpleList::IsEmpty() const
  61.     { return m_pHead == NULL; }
  62. inline void** CSimpleList::GetNextPtr(void* p) const
  63.     { ASSERT(p != NULL); return (void**)((BYTE*)p+m_nNextOffset); }
  64. inline void CSimpleList::RemoveAll()
  65.     { m_pHead = NULL; }
  66. inline void* CSimpleList::GetHead() const
  67.     { return m_pHead; }
  68. inline void* CSimpleList::GetNext(void* prevElement) const
  69.     { return *GetNextPtr(prevElement); }
  70.  
  71. template<class TYPE>
  72. class CTypedSimpleList : public CSimpleList
  73. {
  74. public:
  75.     CTypedSimpleList(int nNextOffset = 0)
  76.         : CSimpleList(nNextOffset) { }
  77.     void AddHead(TYPE p)
  78.         { CSimpleList::AddHead(p); }
  79.     TYPE GetHead()
  80.         { return (TYPE)CSimpleList::GetHead(); }
  81.     TYPE GetNext(TYPE p)
  82.         { return (TYPE)CSimpleList::GetNext(p); }
  83.     BOOL Remove(TYPE p)
  84.         { return CSimpleList::Remove((TYPE)p); }
  85.     operator TYPE()
  86.         { return (TYPE)CSimpleList::GetHead(); }
  87. };
  88.  
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CThreadSlotData - manages owned array of "slots" for thread local storage
  91.  
  92. struct CThreadData; // private to implementation
  93. struct CSlotData;   // private to implementation
  94.  
  95. class CThreadSlotData
  96. {
  97. public:
  98.     CThreadSlotData(BOOL bThreadLocal);
  99.  
  100. // Operations
  101.     int AllocSlot();
  102.     void FreeSlot(int nSlot);
  103.     void* GetValue(int nSlot);
  104.     void SetValue(int nSlot, void* pValue);
  105.     void DeleteValues(HINSTANCE hInst); // delete all values in process/thread
  106.     void AssignInstance(HINSTANCE hInst);
  107.  
  108. // Implementation
  109.     DWORD m_tlsIndex;   // used if bThreadLocal == TRUE
  110.     CThreadData* m_pData;   // used if bThreadLocal == FALSE
  111.  
  112.     int m_nAlloc;       // number of slots allocated (in UINTs)
  113.     int m_nRover;       // (optimization) for quick finding of free slots
  114.     int m_nMax;         // size of slot table below (in bits)
  115.     CSlotData* m_pSlotData; // state of each slot (allocated or not)
  116.     CTypedSimpleList<CThreadData*> m_list;  // list of CThreadData structures
  117.     CRITICAL_SECTION m_sect;
  118.  
  119.     void* GetThreadValue(int nSlot); // special version for threads only!
  120.     void* PASCAL operator new(size_t, void* p)
  121.         { return p; }
  122.     ~CThreadSlotData();
  123. };
  124.  
  125. class CNoTrackObject
  126. {
  127. public:
  128.     void* PASCAL operator new(size_t nSize);
  129.     void PASCAL operator delete(void*);
  130.  
  131. #if defined(_DEBUG) && !defined(_AFX_NO_DEBUG_CRT)
  132.     void* PASCAL operator new(size_t nSize, LPCSTR, int);
  133. #endif
  134.     virtual ~CNoTrackObject() { }
  135. };
  136.  
  137. class CThreadLocalObject
  138. {
  139. public:
  140. // Attributes
  141.     CNoTrackObject* GetData(CNoTrackObject* (AFXAPI* pfnCreateObject)());
  142.     CNoTrackObject* GetDataNA();
  143.  
  144. // Implementation
  145.     int m_nSlot;
  146.     ~CThreadLocalObject();
  147. };
  148.  
  149. class CProcessLocalObject
  150. {
  151. public:
  152. // Attributes
  153.     CNoTrackObject* GetData(CNoTrackObject* (AFXAPI* pfnCreateObject)());
  154.     CNoTrackObject* GetDataNA();
  155.  
  156. // Implementation
  157.     int m_nSlot;
  158.     ~CProcessLocalObject();
  159. };
  160.  
  161. template<class TYPE>
  162. class CThreadLocal : public CThreadLocalObject
  163. {
  164. // Attributes
  165. public:
  166.     inline TYPE* GetData()
  167.     {
  168.         TYPE* pData = (TYPE*)CThreadLocalObject::GetData(&CreateObject);
  169.         ASSERT(pData != NULL);
  170.         return pData;
  171.     }
  172.     inline TYPE* GetDataNA()
  173.     {
  174.         TYPE* pData = (TYPE*)CThreadLocalObject::GetDataNA();
  175.         return pData;
  176.     }
  177.     inline operator TYPE*()
  178.         { return GetData(); }
  179.     inline TYPE* operator->()
  180.         { return GetData(); }
  181.  
  182. // Implementation
  183. public:
  184.     static CNoTrackObject* AFXAPI CreateObject()
  185.         { return new TYPE; }
  186. };
  187.  
  188. #define THREAD_LOCAL(class_name, ident_name) \
  189.     AFX_DATADEF CThreadLocal<class_name> ident_name;
  190. #define EXTERN_THREAD_LOCAL(class_name, ident_name) \
  191.     extern AFX_DATA THREAD_LOCAL(class_name, ident_name)
  192.  
  193. template<class TYPE>
  194. class CProcessLocal : public CProcessLocalObject
  195. {
  196. // Attributes
  197. public:
  198.     inline TYPE* GetData()
  199.     {
  200.         TYPE* pData = (TYPE*)CProcessLocalObject::GetData(&CreateObject);
  201.         ASSERT(pData != NULL);
  202.         return pData;
  203.     }
  204.     inline TYPE* GetDataNA()
  205.     {
  206.         TYPE* pData = (TYPE*)CProcessLocalObject::GetDataNA();
  207.         return pData;
  208.     }
  209.     inline operator TYPE*()
  210.         { return GetData(); }
  211.     inline TYPE* operator->()
  212.         { return GetData(); }
  213.  
  214. // Implementation
  215. public:
  216.     static CNoTrackObject* AFXAPI CreateObject()
  217.         { return new TYPE; }
  218. };
  219.  
  220. #define PROCESS_LOCAL(class_name, ident_name) \
  221.     AFX_DATADEF CProcessLocal<class_name> ident_name;
  222. #define EXTERN_PROCESS_LOCAL(class_name, ident_name) \
  223.     extern AFX_DATA PROCESS_LOCAL(class_name, ident_name)
  224.  
  225. /////////////////////////////////////////////////////////////////////////////
  226.  
  227. void AFXAPI AfxInitLocalData(HINSTANCE hInstInit);
  228. void AFXAPI AfxTermLocalData(HINSTANCE hInstTerm);
  229. void AFXAPI AfxTlsAddRef();
  230. void AFXAPI AfxTlsRelease();
  231.  
  232. #ifdef _AFX_PACKING
  233. #pragma pack(pop)
  234. #endif
  235.  
  236. #undef AFX_DATA
  237. #define AFX_DATA
  238.  
  239. #endif //__AFXTLS_H__
  240.  
  241. /////////////////////////////////////////////////////////////////////////////
  242.