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

  1. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2.  
  3.     comhelp.h
  4.     Created: 10/20/95
  5.     Copyright (c) 1995, Borland International
  6.     $Header:   Y:\admin\bride\addon\deliver\interfac\comhelp.h_v   1.21   18 Nov 1996 11:29:16   JDOUGLAS  $
  7.     $Revision:   1.21  $
  8.  
  9. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/    
  10.  
  11. #ifndef __COMHELP_H
  12. #define __COMHELP_H
  13.  
  14. #include <windows.h>
  15. #include <objbase.h>
  16. #include <ideaddon\iide.h>
  17. #include <ideaddon\ivfile.h>
  18. #include <ideaddon\iview.h>
  19. #include <ideaddon\ipolystr.h>
  20.  
  21. //
  22. // helper functions in comhelp.cpp
  23. //
  24.  
  25. // Manage the IdeServer instance pointer and get various interfaces from it
  26. extern void   SetIdeServer( IIdeServer * pIdeServer );
  27. extern IIdeServer * GetIdeServer( void );        // does an addref
  28. extern void ReleaseIdeServer( void );
  29. extern IUnknown * GetInterface ( REFCLSID rclsid );
  30.  
  31. // These 3 call the IdeServer to make unique instances
  32. extern IPolyString  * CreatePolyString();
  33. extern IVirtualFile * CreateVirtualFile();
  34. extern IViewType    * CreateViewType();
  35.  
  36. // Poly string helpers
  37. extern IPolyString  * MakePolyString( const char * cstr );
  38. extern char *PolyCstrDup (    IPolyString *polystr );
  39.  
  40. //.............................................................................
  41. // 
  42. // IUnknownImp template
  43. //     Derive from this, passing in an IUnknown-derived type to inherit default
  44. //     IUnknown implementation. For an example see examples\project.h
  45. // 
  46. //.............................................................................
  47. template <class T> 
  48. class IUnknownImp : public T {
  49.  public:
  50.    IUnknownImp( REFIID riid, LPUNKNOWN outerUnk = 0) {
  51.       _iid = riid; _refCount = 1;
  52.       d_outerUnk = outerUnk;
  53.    }
  54.  
  55.     // IUnknown
  56.    virtual STDMETHODIMP QueryInterface( REFIID riid, LPVOID * ppvObj);
  57.     virtual STDMETHODIMP_(ULONG) AddRef  (THIS);
  58.     virtual STDMETHODIMP_(ULONG) Release (THIS);
  59.    virtual ~IUnknownImp() {}
  60.  
  61.  protected:
  62.     IID _iid;    
  63.     ULONG _refCount;
  64.    LPUNKNOWN d_outerUnk;
  65. };
  66.  
  67. template <class T>
  68. T * QueryInterfaceHelper( T * /*ignore*/, IUnknown * obj, REFIID id ) {
  69.    T * ret = NULL;
  70.    if ( obj ) {
  71.       HRESULT res = obj->QueryInterface( id, (void **) &ret );
  72.       if ( res == NOERROR ) {
  73.          return ret;
  74.       }
  75.    }
  76.    return NULL;
  77. }   
  78.  
  79. //.............................................................................
  80. //
  81. //    Define various helper macros so you can do things such as:
  82. //
  83. //        IToolServer *pToolSrvr = GET_INTERFACE(IToolServer);     // ret NULL if err
  84. //
  85. // instead of the usual:
  86. //
  87. //        IToolServer *pToolSrvr;
  88. //        IIdeServer  *pIdeServer;
  89. //        HRESULT hRes = ::CoCreateInstance ( IID_Addon_IIdeAddon, NULL, 
  90. //                                                      CLSCTX_INPROC_SERVER, 
  91. //                                                      IID_IUnknown, (void **) &pIdeServer);
  92. //        hRes = pIdeServer->QueryInterface( IID_Addon_IToolServer, (void **) &pToolSrvr );
  93. //
  94. // SetIdeServer must be called with the IdeServer pointer passed in to 
  95. // BcwAddonEntry() at startup before the first GET_INTERFACE is called. 
  96. // Addon clients should then call ReleaseIdeServer() in the shutdown call
  97. // to BcwAddonEntry() ro release the IdeServer pointer. 
  98. //     
  99. // IUNKNOWNIMPL_INIT( Type )
  100. //     When deriving from IUnknownImpl, use this macro to pass the 
  101. //     the IID reference to the IUnknownImpl constructor.
  102. //
  103. // These macros rely on the IID consisting of "IID_Addon_" + "ClassName"
  104. //
  105. //    For example, the poly string class is IPolyString and the IID is
  106. // IID_Addon_IPolyString
  107. //
  108.  
  109. #define MAKE_IID_NAME(Type) \
  110.             IID_Addon_##Type
  111.  
  112. #define GET_INTERFACE(Type) \
  113.          (Type *) ::GetInterface( MAKE_IID_NAME(Type) )
  114.  
  115. #define IUNKNOWNIMPL_INIT( Type ) \
  116.         IUnknownImp< Type > ( MAKE_IID_NAME( Type ) )
  117.  
  118. #define IUNKNOWNIMPL_INIT2( Type, outerUnk ) \
  119.         IUnknownImp< Type > ( MAKE_IID_NAME( Type ) , outerUnk)
  120.       
  121. #define QUERY_INTERFACE( Object, Type ) \
  122.       ::QueryInterfaceHelper< Type >( (Type *)0, (IUnknown *) Object, MAKE_IID_NAME( Type ) )
  123.  
  124.  
  125. //.............................................................................
  126. template <class T> 
  127. STDMETHODIMP IUnknownImp<T>::QueryInterface( REFIID riid, LPVOID * ppobj ) {
  128.     HRESULT hRes = ResultFromScode( E_NOINTERFACE );
  129.     *ppobj = NULL;
  130.  
  131.     if ( ( riid == IID_IUnknown ) || ( riid == _iid ) )  {
  132.         *ppobj = (LPVOID) this;
  133.     }
  134.  
  135.     if ( *ppobj != NULL ) {
  136.         AddRef();
  137.         hRes = NOERROR;
  138.     } else if (d_outerUnk) {
  139.       return d_outerUnk->QueryInterface(riid, ppobj);
  140.    }
  141.     return hRes;
  142. }
  143.  
  144.  
  145. template <class T> 
  146. STDMETHODIMP_(ULONG) IUnknownImp<T>::AddRef() {
  147.    if (d_outerUnk) {
  148.       d_outerUnk->AddRef();
  149.    }
  150.    return ++_refCount;
  151. }
  152.  
  153. template <class T> 
  154. STDMETHODIMP_(ULONG) IUnknownImp<T>::Release (THIS)  {
  155.    if (d_outerUnk) {
  156.       d_outerUnk->Release();
  157.    }
  158.    return --_refCount == 0 ? (delete this, 0) : _refCount;
  159. }
  160.  
  161.  
  162.  
  163. #endif        //  __COMHELP_H
  164.