home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / C_C++ / BorlandCompiler / freecommandLinetools.exe / Include / msputils.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-27  |  7.5 KB  |  365 lines

  1. /*++
  2.  
  3. Copyright (c) 1997-1999 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     MSPutils.h
  8.  
  9. Abstract:
  10.     
  11.     This file defines several utility classes used by the MSP base classes.
  12.  
  13. --*/
  14.  
  15. #ifndef __MSPUTILS_H_
  16. #pragma option push -b -a8 -pc -A- /*P_O_Push*/
  17. #define __MSPUTILS_H_
  18.  
  19. #define DECLARE_VQI() \
  20.     STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject) = 0; \
  21.     STDMETHOD_(ULONG, AddRef)() = 0; \
  22.     STDMETHOD_(ULONG, Release)() = 0;
  23.  
  24. //
  25. // Make sure we have exactly one media type. That's not the case if
  26. // dwMediaType is 0 or more than one bit is set in dwMediaType. Note
  27. // that DWORD is unsigned so this should be safe.
  28. //
  29. inline BOOL IsSingleMediaType(DWORD dwMediaType) 
  30. {   
  31.     return !((dwMediaType == 0) || ((dwMediaType & (dwMediaType - 1)) != 0));
  32. }
  33.  
  34. //
  35. // Check to see if the mediatype is a single type and is in the mask.
  36. //
  37.  
  38. inline BOOL IsValidSingleMediaType(DWORD dwMediaType, DWORD dwMask)
  39. {
  40.     return IsSingleMediaType(dwMediaType)
  41.         && ((dwMediaType & dwMask) == dwMediaType);
  42. }
  43.  
  44. /*++
  45.  
  46. CMSPArray template Description:
  47.  
  48.     Definitions for a simple vector template. The implementaion is borrowed
  49.     from CMSPArray in atlapp.h. Modified only the allocation behavior.
  50.  
  51.     This array should only be used to store simple types. It doesn't call the
  52.     constructor nor the destructor for each element in the array.
  53.  
  54. --*/
  55. const DWORD INITIAL = 8;
  56. const DWORD DELTA   = 8;
  57.  
  58. template <class T, DWORD dwInitial = INITIAL, DWORD dwDelta = DELTA>
  59. class CMSPArray
  60. {
  61.  
  62. protected:
  63.     T* m_aT;
  64.     int m_nSize;
  65.     int m_nAllocSize;
  66.  
  67. public:
  68. // Construction/destruction
  69.     CMSPArray() : m_aT(NULL), m_nSize(0), m_nAllocSize(0)
  70.     { }
  71.  
  72.     ~CMSPArray()
  73.     {
  74.         RemoveAll();
  75.     }
  76.  
  77. // Operations
  78.     int GetSize() const
  79.     {
  80.         return m_nSize;
  81.     }
  82.     BOOL Grow()
  83.     {
  84.         T* aT;
  85.         int nNewAllocSize = 
  86.             (m_nAllocSize == 0) ? dwInitial : (m_nSize + DELTA);
  87.  
  88.         aT = (T*)realloc(m_aT, nNewAllocSize * sizeof(T));
  89.         if(aT == NULL)
  90.             return FALSE;
  91.         m_nAllocSize = nNewAllocSize;
  92.         m_aT = aT;
  93.         return TRUE;
  94.     }
  95.  
  96.     BOOL Add(T& t)
  97.     {
  98.         if(m_nSize == m_nAllocSize)
  99.         {
  100.             if (!Grow()) return FALSE;
  101.         }
  102.         m_nSize++;
  103.         SetAtIndex(m_nSize - 1, t);
  104.         return TRUE;
  105.     }
  106.     BOOL Remove(T& t)
  107.     {
  108.         int nIndex = Find(t);
  109.         if(nIndex == -1)
  110.             return FALSE;
  111.         return RemoveAt(nIndex);
  112.     }
  113.     BOOL RemoveAt(int nIndex)
  114.     {
  115.         if(nIndex != (m_nSize - 1))
  116.             memmove((void*)&m_aT[nIndex], (void*)&m_aT[nIndex + 1], 
  117.                 (m_nSize - (nIndex + 1)) * sizeof(T));
  118.         m_nSize--;
  119.         return TRUE;
  120.     }
  121.     void RemoveAll()
  122.     {
  123.         if(m_nAllocSize > 0)
  124.         {
  125.             free(m_aT);
  126.             m_aT = NULL;
  127.             m_nSize = 0;
  128.             m_nAllocSize = 0;
  129.         }
  130.     }
  131.     T& operator[] (int nIndex) const
  132.     {
  133.         _ASSERTE(nIndex >= 0 && nIndex < m_nSize);
  134.         return m_aT[nIndex];
  135.     }
  136.     T* GetData() const
  137.     {
  138.         return m_aT;
  139.     }
  140.  
  141. // Implementation
  142.     void SetAtIndex(int nIndex, T& t)
  143.     {
  144.         _ASSERTE(nIndex >= 0 && nIndex < m_nSize);
  145.         m_aT[nIndex] = t;
  146.     }
  147.     int Find(T& t) const
  148.     {
  149.         for(int i = 0; i < m_nSize; i++)
  150.         {
  151.             if(m_aT[i] == t)
  152.                 return i;
  153.         }
  154.         return -1;  // not found
  155.     }
  156. };
  157.  
  158. /*++
  159.  
  160. CMSPCritSection Description:
  161.  
  162.     Definitions for a auto initialize critical section.
  163.  
  164. --*/
  165. class CMSPCritSection
  166. {
  167. private:
  168.     CRITICAL_SECTION m_CritSec;
  169.  
  170. public:
  171.     CMSPCritSection()
  172.     {
  173.         InitializeCriticalSection(&m_CritSec);
  174.     }
  175.  
  176.     ~CMSPCritSection()
  177.     {
  178.         DeleteCriticalSection(&m_CritSec);
  179.     }
  180.  
  181.     void Lock() 
  182.     {
  183.         EnterCriticalSection(&m_CritSec);
  184.     }
  185.  
  186.     BOOL TryLock() 
  187.     {
  188.         return TryEnterCriticalSection(&m_CritSec);
  189.     }
  190.  
  191.     void Unlock() 
  192.     {
  193.         LeaveCriticalSection(&m_CritSec);
  194.     }
  195. };
  196.  
  197. /*++
  198.  
  199. CMSPCritSection Description:
  200.  
  201.     Definitions for a auto lock that unlocks when the variable is out
  202.     of scope.
  203.  
  204. --*/
  205. class CLock
  206. {
  207. private:
  208.     CMSPCritSection &m_CriticalSection;
  209.  
  210. public:
  211.     CLock(CMSPCritSection &CriticalSection)
  212.         : m_CriticalSection(CriticalSection)
  213.     {
  214.         m_CriticalSection.Lock();
  215.     }
  216.  
  217.     ~CLock()
  218.     {
  219.         m_CriticalSection.Unlock();
  220.     }
  221. };
  222.  
  223.  
  224. /*++
  225.  
  226. LINK list:
  227.  
  228.     Definitions for a double link list.
  229.  
  230. --*/
  231.  
  232. //
  233. // Calculate the address of the base of the structure given its type, and an
  234. // address of a field within the structure.
  235. //
  236. #ifndef CONTAINING_RECORD
  237. #define CONTAINING_RECORD(address, type, field) \
  238.     ((type *)((PCHAR)(address) - (ULONG_PTR)(&((type *)0)->field)))
  239. #endif
  240.  
  241.  
  242. #ifndef InitializeListHead
  243. //
  244. //  VOID
  245. //  InitializeListHead(
  246. //      PLIST_ENTRY ListHead
  247. //      );
  248. //
  249.  
  250. #define InitializeListHead(ListHead) (\
  251.     (ListHead)->Flink = (ListHead)->Blink = (ListHead))
  252.  
  253. //
  254. //  BOOLEAN
  255. //  IsListEmpty(
  256. //      PLIST_ENTRY ListHead
  257. //      );
  258. //
  259.  
  260. #define IsListEmpty(ListHead) \
  261.     ((ListHead)->Flink == (ListHead))
  262.  
  263. //
  264. //  PLIST_ENTRY
  265. //  RemoveHeadList(
  266. //      PLIST_ENTRY ListHead
  267. //      );
  268. //
  269.  
  270. #define RemoveHeadList(ListHead) \
  271.     (ListHead)->Flink;\
  272.     {RemoveEntryList((ListHead)->Flink)}
  273.  
  274. //
  275. //  PLIST_ENTRY
  276. //  RemoveTailList(
  277. //      PLIST_ENTRY ListHead
  278. //      );
  279. //
  280.  
  281. #define RemoveTailList(ListHead) \
  282.     (ListHead)->Blink;\
  283.     {RemoveEntryList((ListHead)->Blink)}
  284.  
  285. //
  286. //  VOID
  287. //  RemoveEntryList(
  288. //      PLIST_ENTRY Entry
  289. //      );
  290. //
  291.  
  292. #define RemoveEntryList(Entry) {\
  293.     PLIST_ENTRY _EX_Blink;\
  294.     PLIST_ENTRY _EX_Flink;\
  295.     _EX_Flink = (Entry)->Flink;\
  296.     _EX_Blink = (Entry)->Blink;\
  297.     _EX_Blink->Flink = _EX_Flink;\
  298.     _EX_Flink->Blink = _EX_Blink;\
  299.     }
  300.  
  301. //
  302. //  VOID
  303. //  InsertTailList(
  304. //      PLIST_ENTRY ListHead,
  305. //      PLIST_ENTRY Entry
  306. //      );
  307. //
  308.  
  309. #define InsertTailList(ListHead,Entry) {\
  310.     PLIST_ENTRY _EX_Blink;\
  311.     PLIST_ENTRY _EX_ListHead;\
  312.     _EX_ListHead = (ListHead);\
  313.     _EX_Blink = _EX_ListHead->Blink;\
  314.     (Entry)->Flink = _EX_ListHead;\
  315.     (Entry)->Blink = _EX_Blink;\
  316.     _EX_Blink->Flink = (Entry);\
  317.     _EX_ListHead->Blink = (Entry);\
  318.     }
  319.  
  320. //
  321. //  VOID
  322. //  InsertHeadList(
  323. //      PLIST_ENTRY ListHead,
  324. //      PLIST_ENTRY Entry
  325. //      );
  326. //
  327.  
  328. #define InsertHeadList(ListHead,Entry) {\
  329.     PLIST_ENTRY _EX_Flink;\
  330.     PLIST_ENTRY _EX_ListHead;\
  331.     _EX_ListHead = (ListHead);\
  332.     _EX_Flink = _EX_ListHead->Flink;\
  333.     (Entry)->Flink = _EX_Flink;\
  334.     (Entry)->Blink = _EX_ListHead;\
  335.     _EX_Flink->Blink = (Entry);\
  336.     _EX_ListHead->Flink = (Entry);\
  337.     }
  338.  
  339. #endif //InitializeListHead
  340.  
  341. //
  342. // Templates for private addref and release. See Platform SDK documentation.
  343. //
  344.  
  345. template <class T> ULONG MSPAddRefHelper (T * pMyThis)
  346. {
  347.     LOG((MSP_INFO, "MSPAddRefHelper - this = 0x%08x", pMyThis));
  348.     typedef CComAggObject<T> AggClass;
  349.     AggClass * p = CONTAINING_RECORD(pMyThis, AggClass, m_contained);
  350.     return p->AddRef();
  351. }
  352.  
  353. template <class T> ULONG MSPReleaseHelper (T * pMyThis)
  354. {
  355.     LOG((MSP_INFO, "MSPReleaseHelper - this = 0x%08x", pMyThis));
  356.     typedef CComAggObject<T> AggClass;
  357.     AggClass * p = CONTAINING_RECORD(pMyThis, AggClass, m_contained);
  358.     return p->Release();
  359. }
  360.  
  361. #pragma option pop /*P_O_Pop*/
  362. #endif  //__MSPUTILS_H_
  363.  
  364. // eof
  365.