home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / tutsamp / locserve / utilcar.h < prev   
C/C++ Source or Header  |  1997-08-05  |  7KB  |  177 lines

  1. /*+==========================================================================
  2.   File:      UTILCAR.H
  3.  
  4.   Summary:   Include file for the aggregatable COUtilityCar COM object
  5.              class.
  6.  
  7.              UTILCAR showcases the construction of the COUtilityCar COM
  8.              object class with the IUnknown, ICar, and IUtility
  9.              interfaces. This is done through Containment reuse of COCar's
  10.              ICar interface features.
  11.  
  12.              This multiple interface COM Object Class is achieved via the
  13.              technique of nested classes: the implementation of the ICar
  14.              and IUtility interfaces are nested inside of the COUtilityCar
  15.              COM object class.
  16.  
  17.              For a comprehensive tutorial code tour of this module's
  18.              contents and offerings see the tutorial LOCSERVE.HTM
  19.              file. For more specific technical details on the internal
  20.              workings see the comments dispersed throughout the module's
  21.              source code.
  22.  
  23.   Classes:   COUtilityCar
  24.  
  25.   Functions: .
  26.  
  27.   Origin:    11-14-95: atrent - Editor-inheritance from UTILCAR.H in
  28.                the DLLSERVE Tutorial Code Sample.
  29.  
  30. ----------------------------------------------------------------------------
  31.   This file is part of the Microsoft COM Tutorial Code Samples.
  32.  
  33.   Copyright (C) Microsoft Corporation, 1997.  All rights reserved.
  34.  
  35.   This source code is intended only as a supplement to Microsoft
  36.   Development Tools and/or on-line documentation.  See these other
  37.   materials for detailed information regarding Microsoft code samples.
  38.  
  39.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  40.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  41.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  42.   PARTICULAR PURPOSE.
  43. ==========================================================================+*/
  44.  
  45. #if !defined(UTILCAR_H)
  46. #define UTILCAR_H
  47.  
  48. #ifdef __cplusplus
  49.  
  50. /*O+O+++O+++O+++O+++O+++O+++O+++O+++O+++O+++O+++O+++O+++O+++O+++O+++O+++O+++O
  51.   ObjectClass: COUtilityCar
  52.  
  53.   Summary:     COM Object Class for COUtilityCar COM Objects.  COM objects
  54.                of this class augment COCar COM objects (which offer ICar
  55.                interface features of Shift, Clutch, Speed, and Steer) with
  56.                IUtility interface features (Offroad and Winch).  This
  57.                COUtilityCar COM object class is constructed by containment
  58.                reuse of the COCar COM object class.  The mulitple
  59.                interfaces on this COM object class are constructed via
  60.                the nested interface classes technique.
  61.  
  62.   Interfaces:  IUnknown
  63.                  Standard interface providing COM object features.
  64.                ICar
  65.                  Basic Car operation features.
  66.                IUtility
  67.                  Sport-utility vehicle offroad features.
  68.  
  69.   Aggregation: Yes, COUtilityCar COM objects are aggregatable by passing
  70.                a non-NULL pUnkOuter IUnknown pointer into the constructor.
  71. O---O---O---O---O---O---O---O---O---O---O---O---O---O---O---O---O---O---O-O*/
  72. class COUtilityCar : public IUnknown
  73. {
  74.   public:
  75.     // Main Object Constructor & Destructor.
  76.     COUtilityCar(IUnknown* pUnkOuter, CServer* pServer);
  77.     ~COUtilityCar(void);
  78.  
  79.     // A general method for initializing a newly created COUtilityCar.
  80.     HRESULT Init(void);
  81.  
  82.     // IUnknown methods. Main object, non-delegating.
  83.     STDMETHODIMP         QueryInterface(REFIID, PPVOID);
  84.     STDMETHODIMP_(ULONG) AddRef(void);
  85.     STDMETHODIMP_(ULONG) Release(void);
  86.  
  87.     // We get this ICar interface pointer via containment reuse of the
  88.     // ICar interface in an instantiated COCar.
  89.     ICar*           m_pICar;
  90.  
  91.   private:
  92.     // We show nested interface class implementations here.
  93.  
  94.     // We implement the basic ICar interface in this COUtilityCar
  95.     // COM object class.
  96.     class CImpICar : public ICar
  97.     {
  98.       public:
  99.         // Interface Implementation Constructor & Destructor.
  100.         CImpICar(COUtilityCar* pBackObj, IUnknown* pUnkOuter);
  101.         ~CImpICar(void);
  102.  
  103.         // IUnknown methods.
  104.         STDMETHODIMP         QueryInterface(REFIID, PPVOID);
  105.         STDMETHODIMP_(ULONG) AddRef(void);
  106.         STDMETHODIMP_(ULONG) Release(void);
  107.  
  108.         // ICar methods.
  109.         STDMETHODIMP Shift(short nGear);
  110.         STDMETHODIMP Clutch(short nEngaged);
  111.         STDMETHODIMP Speed(short nMph);
  112.         STDMETHODIMP Steer(short nAngle);
  113.  
  114.       private:
  115.         // Data private to this COUtilityCar interface implementation of ICar.
  116.         ULONG         m_cRefI;       // Interface Ref Count (for debugging).
  117.         COUtilityCar* m_pBackObj;    // Parent Object back pointer.
  118.         IUnknown*     m_pUnkOuter;   // Outer unknown for Delegation.
  119.     };
  120.  
  121.     // We implement the IUtility interface (ofcourse) in this COUtilityCar
  122.     // COM object class.  This is the interface that we are using as an
  123.     // augmentation to the existing COCar COM object class.
  124.     class CImpIUtility : public IUtility
  125.     {
  126.       public:
  127.         // Interface Implementation Constructor & Destructor.
  128.         CImpIUtility(COUtilityCar* pBackObj, IUnknown* pUnkOuter);
  129.         ~CImpIUtility(void);
  130.  
  131.         // IUnknown methods.
  132.         STDMETHODIMP         QueryInterface(REFIID, PPVOID);
  133.         STDMETHODIMP_(ULONG) AddRef(void);
  134.         STDMETHODIMP_(ULONG) Release(void);
  135.  
  136.         // IUtility methods.
  137.         STDMETHODIMP Offroad(short nGear);
  138.         STDMETHODIMP Winch(short nRpm);
  139.  
  140.       private:
  141.         // Data private to this interface implementation of IUtility.
  142.         ULONG         m_cRefI;       // Interface Ref Count (for debugging).
  143.         COUtilityCar* m_pBackObj;    // Parent Object back pointer.
  144.         IUnknown*     m_pUnkOuter;   // Outer unknown for Delegation.
  145.     };
  146.  
  147.     // Make the otherwise private and nested ICar interface implementation
  148.     // a friend to COM object instantiations of this selfsame COUtilityCar
  149.     // COM object class.
  150.     friend CImpICar;
  151.     friend CImpIUtility;
  152.  
  153.     // Private data of COUtilityCar COM objects.
  154.  
  155.     // Nested ICar implementation instantiation.
  156.     CImpICar        m_ImpICar;
  157.  
  158.     // Nested IUtility implementation instantiation.
  159.     CImpIUtility    m_ImpIUtility;
  160.  
  161.     // Main Object reference count.
  162.     ULONG           m_cRefs;
  163.  
  164.     // Outer unknown (aggregation & delegation).
  165.     IUnknown*       m_pUnkOuter;
  166.  
  167.     // Pointer to this component server's control object.
  168.     CServer*        m_pServer;
  169. };
  170.  
  171. typedef COUtilityCar* PCOUtilityCar;
  172.  
  173. #endif // __cplusplus
  174.  
  175.  
  176. #endif // UTILCAR_H
  177.