home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / oleaut / dispcalc / dispcalc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  6.1 KB  |  246 lines

  1. /*** 
  2. *dispcalc.h
  3. *
  4. *  This is a part of the Microsoft Source Code Samples.
  5. *
  6. *  Copyright (C) 1992-1997 Microsoft Corporation. All rights reserved.
  7. *
  8. *  This source code is only intended as a supplement to Microsoft Development
  9. *  Tools and/or WinHelp documentation.  See these sources for detailed
  10. *  information regarding the Microsoft samples programs.
  11. *
  12. *Purpose:
  13. *  UNDONE
  14. *
  15. *
  16. *Implementation Notes:
  17. *
  18. *****************************************************************************/
  19.  
  20. #include "hostenv.h"
  21. #include "resource.h"
  22. #include "clsid.h"
  23.  
  24. #ifndef CLASS
  25. # ifdef __TURBOC__
  26. #  define CLASS class huge
  27. # else
  28. #  define CLASS class FAR
  29. # endif
  30. #endif
  31.  
  32. #pragma warning(disable:4355)
  33.  
  34. #ifdef _MAC
  35. # define NEARDATA
  36. #else
  37. # ifdef WIN32
  38. #  define NEARDATA
  39. # else
  40. #  define NEARDATA __near
  41. # endif
  42. #endif
  43.  
  44. #ifdef _MAC
  45. # define UNUSED(X) ((void)(void*)&(X))
  46. #else
  47. # define UNUSED(X) (X)
  48. #endif
  49.  
  50. #define DIM(X) (sizeof(X)/sizeof(X[0]))
  51.  
  52.  
  53.  
  54. // forward decl
  55. CLASS CCalc;
  56.  
  57. // Introduced "calculator" interface
  58. //
  59. // This class implementes core arithmetic functionality
  60. // (such as it is) *and* is the interface that will be
  61. // exposed via IDispatch for external programmability.
  62.  
  63. #if defined(_MAC) && !(!defined(applec) || defined(__SC__) || defined(_MSC_VER))
  64.  
  65. // Mac Note: The default implementation of IDispatch places
  66. // a couple of requirements on the layout of an instance that
  67. // in can invoke on.
  68. //
  69. //   1. It assumes that the vtable pointer is at offset 0
  70. //      from the beginning of the instance. This appears
  71. //      to always be the case if the class derives from
  72. //      an interface, and hence the games I play below
  73. //      creating a stub _CArith interface containing a
  74. //      single pure virtual function. The only reason for
  75. //      this is to ensure that the vtable ptr is at offset
  76. //      0 when this class is a member of CCalc. If your
  77. //      class derives from an Ole interface, then you are ok.
  78. //
  79. //   2. It assumes that the vtable is a simple array of
  80. //      function pointers, with index zero reserved. In
  81. //      order to ensure this, the class must derive from
  82. //      SingleObject. Note that IUnknown derives from SingleObject,
  83. //      so if your class derives from any Ole interface, then
  84. //      you are also ok - otherwise you need to explicitly
  85. //      derive from SingleObject as I have done below.
  86. //
  87. //  (The above comments apply to MPW C++ v3.3)
  88. //      
  89.  
  90. interface _CArith : public SingleObject
  91. {
  92.     STDMETHOD_(void,  put_Accum)(long l) PURE;
  93. };
  94. CLASS CArith : public _CArith
  95. #else
  96. CLASS CArith
  97. #endif
  98. {
  99. public:
  100. #if defined(_MAC)
  101.     BEGIN_INTERFACE
  102. #endif //_MAC
  103.  
  104.     STDMETHOD_(void,  put_Accum)(long l);
  105.     STDMETHOD_(long,  get_Accum)(void);
  106.     STDMETHOD_(void,  put_Opnd)(long l);
  107.     STDMETHOD_(long,  get_Opnd)(void);
  108.     STDMETHOD_(void,  put_Op)(short op);
  109.     STDMETHOD_(short, get_Op)(void);
  110.     STDMETHOD_(short, Eval)(void);
  111.     STDMETHOD_(void,  Clear)(void);
  112.     STDMETHOD_(void,  Display)(void);
  113.     STDMETHOD_(void,  Quit)(void);
  114.     STDMETHOD_(short, Button)(BSTR button);
  115.  
  116.     // the following method is internal, and not exposed for programmability
  117.     int ButtonPush(int button);
  118.  
  119.     CArith(CCalc FAR* pcalc){
  120.       m_pcalc = pcalc;
  121.       Clear();
  122.     }
  123.     enum states { STATE_LOPND, STATE_OP, STATE_ROPND, STATE_EVAL };
  124.  
  125. private:
  126.     CCalc FAR*  m_pcalc;
  127.  
  128.     short    m_op;
  129.     long    m_opnd;
  130.     long    m_accum;
  131.     enum states m_state;
  132. };
  133.  
  134.  
  135. CLASS CCalc : public IUnknown {
  136. public:
  137.     friend CArith;
  138.  
  139.     static CCalc FAR* Create();
  140.  
  141.     STDMETHOD(QueryInterface)(REFIID riid, void FAR* FAR* ppv);
  142.     STDMETHOD_(unsigned long, AddRef)(void);
  143.     STDMETHOD_(unsigned long, Release)(void);
  144.  
  145.     CCalc();
  146.  
  147. #ifdef _MAC
  148.     DialogPtr m_pdlg; 
  149. #else
  150.     HWND m_hwnd;
  151. #endif
  152.     CArith m_arith;
  153.  
  154. private:
  155.     unsigned long m_refs;
  156.     IUnknown FAR* m_punkStdDisp;
  157. };
  158.  
  159. enum operators {
  160.     OP_NONE = 0,
  161.     OP_PLUS,
  162.     OP_MINUS,
  163.     OP_MULT,
  164.     OP_DIV
  165. };
  166.  
  167. // the following enum defines method indices used by the
  168. // default IDispatch implementation - DispInvoke().
  169. //
  170. // Note: these must match the order of the preceeding declarations
  171. //
  172. enum IMETH_CARITH {
  173.     IMETH_PUTACCUM = 0,
  174.     IMETH_GETACCUM,
  175.     IMETH_PUTOPERAND,
  176.     IMETH_GETOPERAND,
  177.     IMETH_PUTOPERATOR,
  178.     IMETH_GETOPERATOR,
  179.     IMETH_EVAL,
  180.     IMETH_CLEAR,
  181.     IMETH_DISPLAY,
  182.     IMETH_QUIT,
  183.     IMETH_BUTTON,
  184.  
  185.     // Define the "property" indices. these are defined to be
  186.     // the first index in a set/get property method pair. These
  187.     // definitions are used to build the METHODDATA that drives
  188.     // our implementation of IDispatch. see cdisp.cpp.
  189.     //
  190.     IMETH_ACCUM    = IMETH_PUTACCUM,
  191.     IMETH_OPERAND  = IMETH_PUTOPERAND,
  192.     IMETH_OPERATOR = IMETH_PUTOPERATOR
  193. };
  194.  
  195. // the following enum defines the IDs used by IDispatch
  196. //
  197. // Note: these values do *not* depend on order of declaration,
  198. // but are sensitive to the kind of the method - ie, if a get/set
  199. // method pair implements a property, then they need to share
  200. // an ID.
  201. //
  202. // Note: by assigning "accum" the ID 'DISPID_VALUE', we are
  203. // choosing to expose it as the default "value" property.
  204. //
  205. enum IDMEMBER_CARITH {
  206.     IDMEMBER_ACCUM = DISPID_VALUE,    // the default property
  207.     IDMEMBER_OPERAND,
  208.     IDMEMBER_OPERATOR,
  209.     IDMEMBER_EVAL,
  210.     IDMEMBER_CLEAR,
  211.     IDMEMBER_DISPLAY,
  212.     IDMEMBER_QUIT,
  213.     IDMEMBER_BUTTON
  214. };
  215.  
  216.  
  217. // the CCalc Class Factory
  218. //
  219. CLASS CCalcCF : public IClassFactory {
  220. public:
  221.     static IClassFactory FAR* Create();
  222.  
  223.     STDMETHOD(QueryInterface)(REFIID riid, void FAR* FAR* ppv);
  224.     STDMETHOD_(unsigned long, AddRef)(void);
  225.     STDMETHOD_(unsigned long, Release)(void);
  226.  
  227.     STDMETHOD(CreateInstance)(
  228.       IUnknown FAR* punkOuter, REFIID riid, void FAR* FAR* ppv);
  229. #ifdef _MAC
  230.     STDMETHOD(LockServer)(unsigned long fLock);
  231. #else
  232.     STDMETHOD(LockServer)(BOOL fLock);
  233. #endif
  234.  
  235.     CCalcCF() { m_refs = 1; }
  236.  
  237. private:
  238.     unsigned long m_refs;
  239. };
  240.  
  241. extern HRESULT InitOle(void);
  242. extern HRESULT UninitOle(void);
  243.  
  244. extern CCalc FAR* g_pcalc;
  245.  
  246.