home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / BaseClasses / combase.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  10.5 KB  |  320 lines

  1. //------------------------------------------------------------------------------
  2. // File: ComBase.h
  3. //
  4. // Desc: DirectShow base classes - defines a class hierarchy for creating
  5. //       COM objects.
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9.  
  10.  
  11. /*
  12.  
  13. a. Derive your COM object from CUnknown
  14.  
  15. b. Make a static CreateInstance function that takes an LPUNKNOWN, an HRESULT *
  16.    and a TCHAR *. The LPUNKNOWN defines the object to delegate IUnknown calls
  17.    to. The HRESULT * allows error codes to be passed around constructors and
  18.    the TCHAR * is a descriptive name that can be printed on the debugger.
  19.  
  20.    It is important that constructors only change the HRESULT * if they have
  21.    to set an ERROR code, if it was successful then leave it alone or you may
  22.    overwrite an error code from an object previously created.
  23.  
  24.    When you call a constructor the descriptive name should be in static store
  25.    as we do not copy the string. To stop large amounts of memory being used
  26.    in retail builds by all these static strings use the NAME macro,
  27.  
  28.    CMyFilter = new CImplFilter(NAME("My filter"),pUnknown,phr);
  29.    if (FAILED(hr)) {
  30.        return hr;
  31.    }
  32.  
  33.    In retail builds NAME(_x_) compiles to NULL, the base CBaseObject class
  34.    knows not to do anything with objects that don't have a name.
  35.  
  36. c. Have a constructor for your object that passes the LPUNKNOWN, HRESULT * and
  37.    TCHAR * to the CUnknown constructor. You can set the HRESULT if you have an
  38.    error, or just simply pass it through to the constructor.
  39.  
  40.    The object creation will fail in the class factory if the HRESULT indicates
  41.    an error (ie FAILED(HRESULT) == TRUE)
  42.  
  43. d. Create a FactoryTemplate with your object's class id and CreateInstance
  44.    function.
  45.  
  46. Then (for each interface) either
  47.  
  48. Multiple inheritance
  49.  
  50. 1. Also derive it from ISomeInterface
  51. 2. Include DECLARE_IUNKNOWN in your class definition to declare
  52.    implementations of QueryInterface, AddRef and Release that
  53.    call the outer unknown
  54. 3. Override NonDelegatingQueryInterface to expose ISomeInterface by
  55.    code something like
  56.  
  57.      if (riid == IID_ISomeInterface) {
  58.          return GetInterface((ISomeInterface *) this, ppv);
  59.      } else {
  60.          return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  61.      }
  62.  
  63. 4. Declare and implement the member functions of ISomeInterface.
  64.  
  65. or: Nested interfaces
  66.  
  67. 1. Declare a class derived from CUnknown
  68. 2. Include DECLARE_IUNKNOWN in your class definition
  69. 3. Override NonDelegatingQueryInterface to expose ISomeInterface by
  70.    code something like
  71.  
  72.      if (riid == IID_ISomeInterface) {
  73.          return GetInterface((ISomeInterface *) this, ppv);
  74.      } else {
  75.          return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  76.      }
  77.  
  78. 4. Implement the member functions of ISomeInterface. Use GetOwner() to
  79.    access the COM object class.
  80.  
  81. And in your COM object class:
  82.  
  83. 5. Make the nested class a friend of the COM object class, and declare
  84.    an instance of the nested class as a member of the COM object class.
  85.  
  86.    NOTE that because you must always pass the outer unknown and an hResult
  87.    to the CUnknown constructor you cannot use a default constructor, in
  88.    other words you will have to make the member variable a pointer to the
  89.    class and make a NEW call in your constructor to actually create it.
  90.  
  91. 6. override the NonDelegatingQueryInterface with code like this:
  92.  
  93.      if (riid == IID_ISomeInterface) {
  94.          return m_pImplFilter->
  95.             NonDelegatingQueryInterface(IID_ISomeInterface, ppv);
  96.      } else {
  97.          return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  98.      }
  99.  
  100. You can have mixed classes which support some interfaces via multiple
  101. inheritance and some via nested classes
  102.  
  103. */
  104.  
  105. #ifndef __COMBASE__
  106. #define __COMBASE__
  107.  
  108. // Filter Setup data structures no defined in axextend.idl
  109.  
  110. typedef REGPINTYPES
  111. AMOVIESETUP_MEDIATYPE, * PAMOVIESETUP_MEDIATYPE, * FAR LPAMOVIESETUP_MEDIATYPE;
  112.  
  113. typedef REGFILTERPINS
  114. AMOVIESETUP_PIN, * PAMOVIESETUP_PIN, * FAR LPAMOVIESETUP_PIN;
  115.  
  116. typedef struct _AMOVIESETUP_FILTER
  117. {
  118.     const CLSID * clsID;
  119.     const WCHAR * strName;
  120.     DWORD      dwMerit;
  121.     UINT       nPins;
  122.     const AMOVIESETUP_PIN * lpPin;
  123. }
  124. AMOVIESETUP_FILTER, * PAMOVIESETUP_FILTER, * FAR LPAMOVIESETUP_FILTER;
  125.  
  126. /* The DLLENTRY module initialises the module handle on loading */
  127.  
  128. extern HINSTANCE g_hInst;
  129.  
  130. /* On DLL load remember which platform we are running on */
  131.  
  132. extern DWORD g_amPlatform;
  133. extern OSVERSIONINFO g_osInfo;     // Filled in by GetVersionEx
  134.  
  135. /* Version of IUnknown that is renamed to allow a class to support both
  136.    non delegating and delegating IUnknowns in the same COM object */
  137.  
  138. #ifndef INONDELEGATINGUNKNOWN_DEFINED
  139. DECLARE_INTERFACE(INonDelegatingUnknown)
  140. {
  141.     STDMETHOD(NonDelegatingQueryInterface) (THIS_ REFIID, LPVOID *) PURE;
  142.     STDMETHOD_(ULONG, NonDelegatingAddRef)(THIS) PURE;
  143.     STDMETHOD_(ULONG, NonDelegatingRelease)(THIS) PURE;
  144. };
  145. #define INONDELEGATINGUNKNOWN_DEFINED
  146. #endif
  147.  
  148. typedef INonDelegatingUnknown *PNDUNKNOWN;
  149.  
  150.  
  151. /* This is the base object class that supports active object counting. As
  152.    part of the debug facilities we trace every time a C++ object is created
  153.    or destroyed. The name of the object has to be passed up through the class
  154.    derivation list during construction as you cannot call virtual functions
  155.    in the constructor. The downside of all this is that every single object
  156.    constructor has to take an object name parameter that describes it */
  157.  
  158. class CBaseObject
  159. {
  160.  
  161. private:
  162.  
  163.     // Disable the copy constructor and assignment by default so you will get
  164.     //   compiler errors instead of unexpected behaviour if you pass objects
  165.     //   by value or assign objects.
  166.     CBaseObject(const CBaseObject& objectSrc);          // no implementation
  167.     void operator=(const CBaseObject& objectSrc);       // no implementation
  168.  
  169. private:
  170.     static LONG m_cObjects;     /* Total number of objects active */
  171.  
  172. protected:
  173. #ifdef DEBUG
  174.     DWORD m_dwCookie;           /* Cookie identifying this object */
  175. #endif
  176.  
  177.  
  178. public:
  179.  
  180.     /* These increment and decrement the number of active objects */
  181.  
  182.     CBaseObject(const TCHAR *pName);
  183. #ifdef UNICODE
  184.     CBaseObject(const char *pName);
  185. #endif
  186.     ~CBaseObject();
  187.  
  188.     /* Call this to find if there are any CUnknown derived objects active */
  189.  
  190.     static LONG ObjectsActive() {
  191.         return m_cObjects;
  192.     };
  193. };
  194.  
  195.  
  196. /* An object that supports one or more COM interfaces will be based on
  197.    this class. It supports counting of total objects for DLLCanUnloadNow
  198.    support, and an implementation of the core non delegating IUnknown */
  199.  
  200. class AM_NOVTABLE CUnknown : public INonDelegatingUnknown,
  201.                  public CBaseObject
  202. {
  203. private:
  204.     const LPUNKNOWN m_pUnknown; /* Owner of this object */
  205.  
  206. protected:                      /* So we can override NonDelegatingRelease() */
  207.     volatile LONG m_cRef;       /* Number of reference counts */
  208.  
  209. public:
  210.  
  211.     CUnknown(const TCHAR *pName, LPUNKNOWN pUnk);
  212.     virtual ~CUnknown() {};
  213.  
  214.     // This is redundant, just use the other constructor
  215.     //   as we never touch the HRESULT in this anyway
  216.     CUnknown(TCHAR *pName, LPUNKNOWN pUnk,HRESULT *phr);
  217. #ifdef UNICODE
  218.     CUnknown(const char *pName, LPUNKNOWN pUnk);
  219.     CUnknown(char *pName, LPUNKNOWN pUnk,HRESULT *phr);
  220. #endif
  221.  
  222.     /* Return the owner of this object */
  223.  
  224.     LPUNKNOWN GetOwner() const {
  225.         return m_pUnknown;
  226.     };
  227.  
  228.     /* Called from the class factory to create a new instance, it is
  229.        pure virtual so it must be overriden in your derived class */
  230.  
  231.     /* static CUnknown *CreateInstance(LPUNKNOWN, HRESULT *) */
  232.  
  233.     /* Non delegating unknown implementation */
  234.  
  235.     STDMETHODIMP NonDelegatingQueryInterface(REFIID, void **);
  236.     STDMETHODIMP_(ULONG) NonDelegatingAddRef();
  237.     STDMETHODIMP_(ULONG) NonDelegatingRelease();
  238. };
  239.  
  240. #if (_MSC_VER <= 1200)
  241. #pragma warning(disable:4211)
  242.  
  243. /* The standard InterlockedXXX functions won't take volatiles */
  244. static inline LONG WINAPI InterlockedIncrement( volatile LONG * plong )
  245. { return InterlockedIncrement( const_cast<LONG*>( plong ) ); }
  246.  
  247. static inline LONG WINAPI InterlockedDecrement( volatile LONG * plong )
  248. { return InterlockedDecrement( const_cast<LONG*>( plong ) ); }
  249.  
  250. #pragma warning(default:4211)
  251. #endif
  252.  
  253.  
  254. /* Return an interface pointer to a requesting client
  255.    performing a thread safe AddRef as necessary */
  256.  
  257. STDAPI GetInterface(LPUNKNOWN pUnk, void **ppv);
  258.  
  259. /* A function that can create a new COM object */
  260.  
  261. typedef CUnknown *(CALLBACK *LPFNNewCOMObject)(LPUNKNOWN pUnkOuter, HRESULT *phr);
  262.  
  263. /*  A function (can be NULL) which is called from the DLL entrypoint
  264.     routine for each factory template:
  265.  
  266.     bLoading - TRUE on DLL load, FALSE on DLL unload
  267.     rclsid   - the m_ClsID of the entry
  268. */
  269. typedef void (CALLBACK *LPFNInitRoutine)(BOOL bLoading, const CLSID *rclsid);
  270.  
  271. /* Create one of these per object class in an array so that
  272.    the default class factory code can create new instances */
  273.  
  274. class CFactoryTemplate {
  275.  
  276. public:
  277.  
  278.     const WCHAR *              m_Name;
  279.     const CLSID *              m_ClsID;
  280.     LPFNNewCOMObject           m_lpfnNew;
  281.     LPFNInitRoutine            m_lpfnInit;
  282.     const AMOVIESETUP_FILTER * m_pAMovieSetup_Filter;
  283.  
  284.     BOOL IsClassID(REFCLSID rclsid) const {
  285.         return (IsEqualCLSID(*m_ClsID,rclsid));
  286.     };
  287.  
  288.     CUnknown *CreateInstance(LPUNKNOWN pUnk, HRESULT *phr) const {
  289.         CheckPointer(phr,NULL);
  290.         return m_lpfnNew(pUnk, phr);
  291.     };
  292. };
  293.  
  294.  
  295. /* You must override the (pure virtual) NonDelegatingQueryInterface to return
  296.    interface pointers (using GetInterface) to the interfaces your derived
  297.    class supports (the default implementation only supports IUnknown) */
  298.  
  299. #define DECLARE_IUNKNOWN                                        \
  300.     STDMETHODIMP QueryInterface(REFIID riid, void **ppv) {      \
  301.         return GetOwner()->QueryInterface(riid,ppv);            \
  302.     };                                                          \
  303.     STDMETHODIMP_(ULONG) AddRef() {                             \
  304.         return GetOwner()->AddRef();                            \
  305.     };                                                          \
  306.     STDMETHODIMP_(ULONG) Release() {                            \
  307.         return GetOwner()->Release();                           \
  308.     };
  309.  
  310.  
  311.  
  312. HINSTANCE   LoadOLEAut32();
  313.  
  314.  
  315. #endif /* __COMBASE__ */
  316.  
  317.  
  318.  
  319.  
  320.