home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / comobjec.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  3.5 KB  |  170 lines

  1. /*  COMOBJEC.H
  2.  
  3.     Template helper class for easy usage of COM interfaces.
  4.  
  5. */
  6.  
  7. /*
  8.  *      C/C++ Run Time Library - Version 10.0
  9.  *
  10.  *      Copyright (c) 1999, 2000 by Inprise Corporation
  11.  *      All Rights Reserved.
  12.  *
  13.  */
  14.  
  15. /* $Revision:   9.2  $ */
  16.  
  17.  
  18. #ifndef COMOBJECT_H
  19. #define COMOBJECT_H
  20.  
  21. #ifndef __cplusplus
  22. #error Must use C++ for the TComObject class.
  23. #endif
  24.  
  25. #include <typeinfo>
  26. #include <windows.h>
  27. #include <objbase.h>
  28.  
  29.  
  30. template <class T>
  31. class __stdcall TComObject : public T {
  32. protected:
  33.     TComObject()
  34.         : refc(1)
  35.     { }
  36.  
  37.     template <class T1>
  38.     TComObject(T1 arg)
  39.         : T(arg), refc(1)
  40.     { }
  41.  
  42.     template <class T1, class T2>
  43.     TComObject(T1 arg1, T2 arg2)
  44.         : T(arg1, arg2), refc(1)
  45.     { }
  46.  
  47.     template <class T1, class T2, class T3>
  48.     TComObject(T1 arg1, T2 arg2, T3 arg3)
  49.         : T(arg1, arg2, arg3), refc(1)
  50.     { }
  51.  
  52.     template <class T1, class T2, class T3, class T4>
  53.     TComObject(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
  54.         : T(arg1, arg2, arg3, arg4), refc(1)
  55.     { }
  56.  
  57.     template <class T1, class T2, class T3, class T4, class T5>
  58.     TComObject(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
  59.         : T(arg1, arg2, arg3, arg4, arg5), refc(1)
  60.     { }
  61.  
  62.     template <class T1, class T2, class T3, class T4, class T5, class T6>
  63.     TComObject(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
  64.         : T(arg1, arg2, arg3, arg4, arg5, arg6), refc(1)
  65.     { }
  66.  
  67. public:
  68.     // constructor methods
  69.     static TComObject *Create()
  70.     {
  71.         return new TComObject;
  72.     }
  73.  
  74.     template <class T1>
  75.     static TComObject *Create(T1 arg)
  76.     {
  77.         return new TComObject(arg);
  78.     }
  79.  
  80.     template <class T1, class T2>
  81.     static TComObject *Create(T1 arg1, T2 arg2)
  82.     {
  83.         return new TComObject(arg1, arg2);
  84.     }
  85.  
  86.     template <class T1, class T2, class T3>
  87.     static TComObject *Create(T1 arg1, T2 arg2, T3 arg3)
  88.     {
  89.         return new TComObject(arg1, arg2, arg3);
  90.     }
  91.  
  92.     template <class T1, class T2, class T3, class T4>
  93.     static TComObject *Create(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
  94.     {
  95.         return new TComObject(arg1, arg2, arg3, arg4);
  96.     }
  97.  
  98.     template <class T1, class T2, class T3, class T4, class T5>
  99.     static TComObject *Create(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
  100.     {
  101.         return new TComObject(arg1, arg2, arg3, arg4, arg5);
  102.     }
  103.  
  104.     template <class T1, class T2, class T3, class T4, class T5, class T6>
  105.     static TComObject *Create(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
  106.     {
  107.         return new TComObject(arg1, arg2, arg3, arg4, arg5, arg6);
  108.     }
  109.  
  110.  
  111.     // IUnknown "implementation"
  112.     HRESULT QueryInterface(REFIID rid, void **p)
  113.     {
  114.         if (rid == IID_IUnknown)
  115.             *p = this;
  116.         else if (IUnknown *q = FindInterface(typeid(*this), rid))
  117.             *p = q;
  118.         else {
  119.             *p = NULL;
  120.             return E_NOINTERFACE;
  121.         }
  122.  
  123.         AddRef();
  124.         return S_OK;
  125.     }
  126.  
  127.     ULONG AddRef()
  128.     {
  129.         return InterlockedIncrement(&refc);
  130.     }
  131.  
  132.     ULONG Release()
  133.     {
  134.         ULONG rc = InterlockedDecrement(&refc);
  135.         if (!rc)
  136.             delete this;
  137.         return rc;
  138.     }
  139.  
  140. protected:
  141. #pragma option push -w-inl
  142.     IUnknown *FindInterface(std::type_info const &tp, REFIID rid)
  143.     {
  144.         IUnknown *rc = NULL;
  145.         if ((tp._guid() != NULL) && (*((GUID*)tp._guid()) == rid))
  146.             rc = (IUnknown *)tp._rtti_cast(this);
  147.         else
  148.         {
  149.             std::type_info::_base_info b;
  150.             for (std::type_info const *p = tp._first_base(b); p; p = tp._next_base(b))
  151.             {
  152.                 rc = FindInterface(*p, rid);
  153.                 if (rc)
  154.                     break;
  155.             }
  156.         }
  157.         return rc;
  158.     }
  159. #pragma option pop // -w-inl
  160.  
  161. private:
  162.     LONG refc;
  163.  
  164.     // no copies
  165.     TComObject(TComObject const &);
  166.     TComObject const &operator=(TComObject const &);
  167. };
  168.  
  169. #endif // ! COMOBJECT_H
  170.