home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / wxutil.h < prev   
Encoding:
C/C++ Source or Header  |  1996-06-13  |  11.4 KB  |  423 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1992 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11.  
  12. //
  13. // helper classes and functions for building multimedia filters
  14. //
  15.  
  16. #ifndef __WXUTIL__
  17. #define __WXUTIL__
  18.  
  19. // eliminate spurious "statement has no effect" warnings.
  20. #pragma warning(disable: 4705)
  21.  
  22. // wrapper for whatever critical section we have
  23. class CCritSec {
  24.  
  25.     // make copy constructor and assignment operator inaccessible
  26.  
  27.     CCritSec(const CCritSec &refCritSec);
  28.     CCritSec &operator=(const CCritSec &refCritSec);
  29.  
  30.     CRITICAL_SECTION m_CritSec;
  31.  
  32. #ifdef DEBUG
  33. public:
  34.     DWORD   m_currentOwner;
  35.     DWORD   m_lockCount;
  36.     BOOL    m_fTrace;        // Trace this one
  37. public:
  38.     CCritSec();
  39.     ~CCritSec();
  40.     void Lock();
  41.     void Unlock();
  42. #else
  43.  
  44. public:
  45.     CCritSec() {
  46.     InitializeCriticalSection(&m_CritSec);
  47.     };
  48.  
  49.     ~CCritSec() {
  50.     DeleteCriticalSection(&m_CritSec);
  51.     };
  52.  
  53.     void Lock() {
  54.     EnterCriticalSection(&m_CritSec);
  55.     };
  56.  
  57.     void Unlock() {
  58.     LeaveCriticalSection(&m_CritSec);
  59.     };
  60. #endif
  61. };
  62.  
  63. //
  64. // To make deadlocks easier to track it is useful to insert in the
  65. // code an assertion that says whether we own a critical section or
  66. // not.  We make the routines that do the checking globals to avoid
  67. // having different numbers of member functions in the debug and
  68. // retail class implementations of CCritSec.  In addition we provide
  69. // a routine that allows usage of specific critical sections to be
  70. // traced.  This is NOT on by default - there are far too many.
  71. //
  72.  
  73. #ifdef DEBUG
  74.     BOOL CritCheckIn(CCritSec * pcCrit);
  75.     BOOL CritCheckOut(CCritSec * pcCrit);
  76.     void DbgLockTrace(BOOL fTrace);
  77. #else
  78.     #define CritCheckIn(x) TRUE
  79.     #define CritCheckOut(x) TRUE
  80.     #define DbgLockTrace(pc, fT)
  81. #endif
  82.  
  83.  
  84. // locks a critical section, and unlocks it automatically
  85. // when the lock goes out of scope
  86. class CAutoLock {
  87.  
  88.     // make copy constructor and assignment operator inaccessible
  89.  
  90.     CAutoLock(const CAutoLock &refAutoLock);
  91.     CAutoLock &operator=(const CAutoLock &refAutoLock);
  92.  
  93. protected:
  94.     CCritSec * m_pLock;
  95.  
  96. public:
  97.     CAutoLock(CCritSec * plock)
  98.     {
  99.         m_pLock = plock;
  100.         m_pLock->Lock();
  101.     };
  102.  
  103.     ~CAutoLock() {
  104.         m_pLock->Unlock();
  105.     };
  106. };
  107.  
  108.  
  109.  
  110. // wrapper for event objects
  111. class CAMEvent {
  112.  
  113.     // make copy constructor and assignment operator inaccessible
  114.  
  115.     CAMEvent(const CAMEvent &refEvent);
  116.     CAMEvent &operator=(const CAMEvent &refEvent);
  117.  
  118. protected:
  119.     HANDLE m_hEvent;
  120. public:
  121.     CAMEvent(BOOL fManualReset = FALSE);
  122.     ~CAMEvent();
  123.  
  124.     // Cast to HANDLE - we don't support this as an lvalue
  125.     operator HANDLE () const { return m_hEvent; };
  126.  
  127.     void Set() { SetEvent(m_hEvent); };
  128.     BOOL Wait(DWORD dwTimeout = INFINITE) {
  129.     return (WaitForSingleObject(m_hEvent, dwTimeout) == WAIT_OBJECT_0);
  130.     };
  131.     void Reset() { ResetEvent(m_hEvent); };
  132.     BOOL Check() { return Wait(0); };
  133. };
  134.  
  135. // old name supported for the time being
  136. #define CTimeoutEvent CAMEvent
  137.  
  138. // support for a worker thread
  139.  
  140. // simple thread class supports creation of worker thread, synchronization
  141. // and communication. Can be derived to simplify parameter passing
  142. class CThread {
  143.  
  144.     // make copy constructor and assignment operator inaccessible
  145.  
  146.     CThread(const CThread &refThread);
  147.     CThread &operator=(const CThread &refThread);
  148.  
  149.     HANDLE m_hThread;
  150.     CAMEvent m_EventSend;
  151.     CAMEvent m_EventComplete;
  152.  
  153.     DWORD m_dwParam;
  154.     DWORD m_dwReturnVal;
  155.  
  156. protected:
  157.     // thread will run this function on startup
  158.     // must be supplied by derived class
  159.     virtual DWORD ThreadProc() = 0;
  160.  
  161.  
  162. public:
  163.     CThread();
  164.     ~CThread();
  165.  
  166.     CCritSec m_AccessLock;    // locks access by client threads
  167.     CCritSec m_WorkerLock;    // locks access to shared objects
  168.  
  169.     // thread initially runs this. param is actually 'this'. function
  170.     // just gets this and calls ThreadProc
  171.     static DWORD WINAPI InitialThreadProc(LPVOID pv);
  172.  
  173.     // start thread running  - error if already running
  174.     BOOL Create();
  175.  
  176.     // signal the thread, and block for a response
  177.     //
  178.     DWORD CallWorker(DWORD);
  179.  
  180.     // accessor thread calls this when done with thread (having told thread
  181.     // to exit)
  182.     void Close() {
  183.         if (m_hThread) {
  184.             WaitForSingleObject(m_hThread, INFINITE);
  185.             CloseHandle(m_hThread);
  186.             m_hThread = 0;
  187.         }
  188.     };
  189.  
  190.     // returns TRUE if thread has been created and hasn't yet exited
  191.     BOOL ThreadExists();
  192.  
  193.     // wait for the next request
  194.     DWORD GetRequest();
  195.  
  196.     // is there a request?
  197.     BOOL CheckRequest(DWORD * pParam);
  198.  
  199.     // reply to the request
  200.     void Reply(DWORD);
  201.  
  202.     // If you want to do WaitForMultipleObjects you'll need to include
  203.     // this handle in your wait list or you won't be responsive
  204.     HANDLE GetRequestHandle() const { return m_EventSend; };
  205.  
  206.     // Find out what the request was
  207.     DWORD GetRequestParam() const { return m_dwParam; };
  208. };
  209.  
  210.  
  211. // CQueue
  212. //
  213. // Implements a simple Queue ADT.  The queue contains a finite number of
  214. // objects, access to which is controlled by a semaphore.  The semaphore
  215. // is created with an initial count (N).  Each time an object is added
  216. // a call to WaitForSingleObject is made on the semaphore's handle.  When
  217. // this function returns a slot has been reserved in the queue for the new
  218. // object.  If no slots are available the function blocks until one becomes
  219. // available.  Each time an object is removed from the queue ReleaseSemaphore
  220. // is called on the semaphore's handle, thus freeing a slot in the queue.
  221. // If no objects are present in the queue the function blocks until an
  222. // object has been added.
  223.  
  224. #define DEFAULT_QUEUESIZE   2
  225.  
  226. template <class T> class CQueue {
  227. private:
  228.     HANDLE          hSemPut;        // Semaphore controlling queue "putting"
  229.     HANDLE          hSemGet;        // Semaphore controlling queue "getting"
  230.     CRITICAL_SECTION CritSect;      // Thread seriallization
  231.     int             nMax;           // Max objects allowed in queue
  232.     int             iNextPut;       // Array index of next "PutMsg"
  233.     int             iNextGet;       // Array index of next "GetMsg"
  234.     T              *QueueObjects;   // Array of objects (ptr's to void)
  235.  
  236.     void Initialize(int n) {
  237.         iNextPut = iNextGet = 0;
  238.         nMax = n;
  239.         InitializeCriticalSection(&CritSect);
  240.         hSemPut = CreateSemaphore(NULL, n, n, NULL);
  241.         hSemGet = CreateSemaphore(NULL, 0, n, NULL);
  242.         QueueObjects = new T[n];
  243.     }
  244.  
  245.  
  246. public:
  247.     CQueue(int n) {
  248.         Initialize(n);
  249.     }
  250.  
  251.     CQueue() {
  252.         Initialize(DEFAULT_QUEUESIZE);
  253.     }
  254.  
  255.     ~CQueue() {
  256.         delete [] QueueObjects;
  257.         DeleteCriticalSection(&CritSect);
  258.         CloseHandle(hSemPut);
  259.         CloseHandle(hSemGet);
  260.     }
  261.  
  262.     T GetQueueObject() {
  263.         int iSlot;
  264.         T Object;
  265.         LONG lPrevious;
  266.  
  267.         // Wait for someone to put something on our queue, returns straight
  268.         // away is there is already an object on the queue.
  269.         //
  270.         WaitForSingleObject(hSemGet, INFINITE);
  271.  
  272.         EnterCriticalSection(&CritSect);
  273.         iSlot = iNextGet++ % nMax;
  274.         Object = QueueObjects[iSlot];
  275.         LeaveCriticalSection(&CritSect);
  276.  
  277.         // Release anyone waiting to put an object onto our queue as there
  278.         // is now space available in the queue.
  279.         //
  280.         ReleaseSemaphore(hSemPut, 1L, &lPrevious);
  281.         return Object;
  282.     }
  283.  
  284.     void PutQueueObject(T Object) {
  285.         int iSlot;
  286.         LONG lPrevious;
  287.  
  288.         // Wait for someone to get something from our queue, returns straight
  289.         // away is there is already an empty slot on the queue.
  290.         //
  291.         WaitForSingleObject(hSemPut, INFINITE);
  292.  
  293.         EnterCriticalSection(&CritSect);
  294.         iSlot = iNextPut++ % nMax;
  295.         QueueObjects[iSlot] = Object;
  296.         LeaveCriticalSection(&CritSect);
  297.  
  298.         // Release anyone waiting to remove an object from our queue as there
  299.         // is now an object available to be removed.
  300.         //
  301.         ReleaseSemaphore(hSemGet, 1L, &lPrevious);
  302.     }
  303. };
  304.  
  305. // miscellaneous string conversion functions
  306. // NOTE: as we need to use the same binaries on Win95 as on NT this code should
  307. // be compiled WITHOUT unicode being defined.  Otherwise we will not pick up
  308. // these internal routines and the binary will not run on Win95.
  309.  
  310. #ifndef UNICODE
  311. #define wsprintfW wsprintfWInternal
  312. int WINAPIV wsprintfWInternal(LPWSTR, LPCWSTR, ...);
  313.  
  314. #define lstrcpyW lstrcpyWInternal
  315. LPWSTR
  316. WINAPI
  317. lstrcpyWInternal(
  318.     LPWSTR lpString1,
  319.     LPCWSTR lpString2
  320.     );
  321. #define lstrcpynW lstrcpynWInternal
  322. LPWSTR
  323. WINAPI
  324. lstrcpynWInternal(
  325.     LPWSTR lpString1,
  326.     LPCWSTR lpString2,
  327.     int     iMaxLength
  328.     );
  329. #define lstrcmpW lstrcmpWInternal
  330. int
  331. WINAPI
  332. lstrcmpWInternal(
  333.     LPCWSTR lpString1,
  334.     LPCWSTR lpString2
  335.     );
  336. #define lstrcmpiW lstrcmpiWInternal
  337. int
  338. WINAPI
  339. lstrcmpiWInternal(
  340.     LPCWSTR lpString1,
  341.     LPCWSTR lpString2
  342.     );
  343. #define lstrlenW lstrlenWInternal
  344. int
  345. WINAPI
  346. lstrlenWInternal(
  347.     LPCWSTR lpString
  348.     );
  349. #endif
  350.  
  351. void * __cdecl memchrInternal(const void *pv, int c, size_t sz);
  352.  
  353. void WINAPI IntToWstr(int i, LPWSTR wstr);
  354.  
  355. #define WstrToInt(sz) atoiW(sz)
  356.  
  357. inline int atoiW(const WCHAR *sz)
  358. {
  359.     int i = 0;
  360.  
  361.     while (*sz && *sz >= L'0' && *sz <= L'9')
  362.         i = i*10 + *sz++ - L'0';
  363.         
  364.     return i;        
  365. }
  366.  
  367. inline int atoiA(const CHAR *sz)
  368. {
  369.     int i = 0;
  370.  
  371.     while (*sz && *sz >= '0' && *sz <= '9')
  372.         i = i*10 + *sz++ - '0';
  373.         
  374.     return i;        
  375. }
  376.  
  377. #ifdef UNICODE
  378. #define atoi    atoiW
  379. #else
  380. #define atoi    atoiA
  381. #endif
  382.  
  383.  
  384.  
  385. // These are available to help managing bitmap VIDEOINFO media structures
  386.  
  387. extern const DWORD bits555[3];
  388. extern const DWORD bits565[3];
  389. extern const DWORD bits888[3];
  390.  
  391. // These help convert between VIDEOINFO and BITMAPINFO structures
  392.  
  393. STDAPI_(const GUID) GetTrueColorType(const BITMAPINFOHEADER *pbmiHeader);
  394. STDAPI_(const GUID) GetBitmapSubtype(const BITMAPINFOHEADER *pbmiHeader);
  395. STDAPI_(WORD) GetBitCount(const GUID *pSubtype);
  396. STDAPI_(TCHAR *) GetSubtypeName(const GUID *pSubtype);
  397. STDAPI_(LONG) GetBitmapFormatSize(const BITMAPINFOHEADER *pHeader);
  398. STDAPI_(DWORD) GetBitmapSize(const BITMAPINFOHEADER *pHeader);
  399. STDAPI_(BOOL) ContainsPalette(const VIDEOINFO *pVideoInfo);
  400. STDAPI_(const RGBQUAD *) GetBitmapPalette(const VIDEOINFO *pVideoInfo);
  401.  
  402.  
  403. // Compares two interfaces and returns TRUE if they are on the same object
  404. BOOL IsEqualObject(IUnknown *pFirst, IUnknown *pSecond);
  405.  
  406. // This is for comparing pins
  407. #define EqualPins(pPin1, pPin2) IsEqualObject(pPin1, pPin2)
  408.  
  409.  
  410. // Arithmetic helper functions
  411.  
  412. // Compute (a * b + rnd) / c
  413. LONGLONG llMulDiv(LONGLONG a, LONGLONG b, LONGLONG c, LONGLONG rnd);
  414. LONGLONG Int64x32Div32(LONGLONG a, LONG b, LONG c, LONG rnd);
  415.  
  416.  
  417. // Avoids us dyna-linking to SysAllocString to copy BSTR strings
  418. HRESULT WriteBSTR(BSTR * pstrDest, LPCWSTR szSrc);
  419. HRESULT FreeBSTR(BSTR* pstr);
  420.  
  421. #endif /* __WXUTIL__ */
  422.  
  423.