home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / apiapi.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.3 KB  |  192 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. // APIAPI.H  This header file containes the main api repository
  20. // interface.  Use the APIAPI to register interfaces and retrieve
  21. // registered interfaces.
  22.  
  23. #ifndef __APIAPI_H
  24. #define __APIAPI_H
  25.  
  26. #ifdef _WIN32
  27. #include "objbase.h"
  28. #else
  29. #include <memory.h>
  30. #include "compobj.h"
  31. #endif
  32. #include "assert.h"
  33.  
  34. #define APIID(id,x)        DEFINE_GUID(id,0x30611040 + x,0x8c7a,0x11cf,0xac,0x4c,0x44,0x45,0x53,0x54,0x00,0x00);
  35.  
  36. APIID(IID_IApiapi,0);        // base id
  37.  
  38. typedef enum {
  39.     ApiPrivate = 0x0,
  40.     ApiPublic 
  41. } APIEXPORT;
  42.  
  43. typedef int (*APIBOOTER)(char *);
  44. typedef char * APICLASS;
  45. typedef void * APISIGNATURE;
  46.  
  47. #define APISIGCMP(a,b) ((a)==(b))
  48.  
  49. #define    APICLASS_APIAPI        "APIAPI"
  50.  
  51. class IApiapi {
  52. public:
  53.     virtual LPUNKNOWN GetFirstInstance ( 
  54.         APICLASS lpszClassName
  55.         ) = 0;
  56.  
  57.     virtual LPUNKNOWN GetNextInstance (
  58.         APICLASS lpszClassName,
  59.         LPUNKNOWN pUnk
  60.         ) = 0;
  61.  
  62.     virtual void * GetFirstApiInstance ( 
  63.         REFIID refiid,
  64.         LPUNKNOWN * ppUnk 
  65.         ) = 0;
  66.  
  67.     virtual int RemoveInstance(
  68.         LPUNKNOWN pUnk
  69.         ) = 0;
  70.  
  71.     virtual LPUNKNOWN CreateClassInstance (
  72.            APICLASS lpszClassName,
  73.         LPUNKNOWN pUnkOuter = NULL,
  74.         APISIGNATURE apiSig = 0
  75.         ) = 0;
  76.  
  77.     virtual int RegisterClassFactory (
  78.            APICLASS lpszClassName,
  79.            LPCLASSFACTORY pUnknown,
  80.         char * szModuleName = 0
  81.         ) = 0;
  82. };
  83.  
  84. typedef IApiapi * LPAPIAPI;
  85.  
  86. #ifdef WIN32
  87.     #ifdef __BORLANDC__
  88.        #define API_PUBLIC(__x)        __x
  89.     #else      
  90.         #define API_PUBLIC(__x)        _declspec(dllexport) __x
  91.    #endif
  92. #else
  93.     #define    API_PUBLIC(__x)        __x _cdecl _export _loadds
  94. #endif
  95.  
  96. extern "C" {
  97.     API_PUBLIC(LPAPIAPI) GetAPIAPI(void);
  98. }
  99.  
  100. class ApiApi {
  101. public:
  102.     IApiapi * m_Apiapi;
  103.     ApiApi ( ) 
  104.     { 
  105.          m_Apiapi = GetAPIAPI();
  106.       assert(m_Apiapi);
  107.     }
  108.     IApiapi * operator -> ( )
  109.     {
  110.         return m_Apiapi;
  111.     }
  112. };
  113.  
  114. class ApiPtr {
  115. protected:
  116.     LPUNKNOWN m_pUnknown;
  117.     void * m_pSomeInterface;
  118. public:
  119.     ApiPtr ( REFIID apiid, LPUNKNOWN pUnknown = 0 )
  120.     {  
  121.         if (!pUnknown) {
  122.             LPAPIAPI  pApiapi = GetAPIAPI();
  123.             assert(pApiapi);
  124.             m_pSomeInterface = pApiapi->GetFirstApiInstance(apiid,&m_pUnknown);
  125.             m_pUnknown = NULL;
  126.         }                          
  127.         else {
  128.             m_pUnknown = pUnknown;
  129.             assert(pUnknown->QueryInterface(apiid,(LPVOID*)&m_pSomeInterface)==NOERROR);
  130.         }
  131.  
  132.     }
  133.     ~ApiPtr ( )
  134.     {
  135.         if (m_pUnknown)
  136.             m_pUnknown->Release();
  137.     }
  138.     void * GetAPI ( ) 
  139.     {
  140.         return m_pSomeInterface;
  141.     }
  142. };        
  143.  
  144. #define APIPTRDEF(apiid,i,v,unk) ApiPtr v##_ptr(apiid,unk); \
  145.                         i * v = (i *)v##_ptr.GetAPI();
  146.  
  147. #define ApiApiPtr(v) LPAPIAPI v = GetAPIAPI()
  148. #define DECLARE_FACTORY(f) static f __##f
  149.  
  150. // Generic Classes
  151.  
  152. class CGenericObject    :   public  IUnknown
  153. {
  154. protected:
  155.     ULONG       m_ulRefCount;
  156.    
  157. public:
  158.     CGenericObject ( ) {
  159.         m_ulRefCount = 0;
  160.     }
  161.  
  162.     virtual ~CGenericObject() {}
  163.  
  164.     // IUnknown Interface
  165.     STDMETHODIMP            QueryInterface(REFIID,LPVOID *);
  166.     STDMETHODIMP_(ULONG)    AddRef(void);
  167.     STDMETHODIMP_(ULONG)    Release(void);
  168. };
  169.  
  170. class CGenericFactory   :   public  IClassFactory
  171. {
  172. protected:
  173.     ULONG m_ulRefCount;
  174.    
  175. public:
  176.     CGenericFactory ( ) {
  177.         m_ulRefCount = 0;
  178.     }
  179.  
  180.     // IUnknown Interface
  181.     STDMETHODIMP            QueryInterface(REFIID,LPVOID *);
  182.     STDMETHODIMP_(ULONG)    AddRef(void);
  183.     STDMETHODIMP_(ULONG)    Release(void);
  184.    
  185.     // IClassFactory Interface
  186.     STDMETHODIMP            CreateInstance(LPUNKNOWN,REFIID,LPVOID*);
  187.     STDMETHODIMP            LockServer(BOOL);
  188.  
  189. };
  190.  
  191. #endif
  192.