Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 2000 - 2000
  6. //
  7. // File: eventlock.h
  8. //
  9. // This file contains code needed to fire script event in a safer way
  10. // Locks made on stack will postpone firing the event on particular interface
  11. // as long as the last lock is released.
  12. //--------------------------------------------------------------------------
  13. #pragma once
  14. #if !defined(EVENTLOCK_H_INCLUDED)
  15. #define EVENTLOCK_H_INCLUDED
  16. #include <queue>
  17. #include "baseapi.h"
  18. /***************************************************************************\
  19. *
  20. * CLASS: CEventBuffer
  21. *
  22. * PURPOSE: objects this class maintains the interface locks by exposing
  23. * methods Lock(), Unlock() and IsLocked(); It also implements queue
  24. * of script events accessible thru ScEmitOrPostpone(); Events in the queue
  25. * will be automatically emited when the last lock is removed by
  26. * calling Unlock() method.
  27. *
  28. * USAGE: Object of this class are constucted as global or static variables
  29. * per each monitored interface.
  30. * Currently it is used (as static variable) by GetEventBuffer template
  31. * function and is accessed by CEventLock object put on the stack by
  32. * LockComEventInterface macro
  33. *
  34. \***************************************************************************/
  35. class MMCBASE_API CEventBuffer
  36. {
  37. // structure containing postponed script event
  38. // since it is a dipinterface call, data consists of pointer to
  39. // IDispatch interface , disp_id and the array of parameters
  40. struct DispCallStr
  41. {
  42. IDispatchPtr spDispatch;
  43. DISPID dispid;
  44. std::vector<CComVariant> vars;
  45. };
  46. // the following member is not used from anywhere else but from mmcbase.dll - based methods
  47. // thus it is safe to have it here ( but we need to silence the warning )
  48. #pragma warning(disable:4251)
  49. // queue of postponed events
  50. std::queue<DispCallStr> m_postponed;
  51. #pragma warning(default:4251)
  52. // lock count
  53. int m_locks;
  54. public:
  55. // constructor. No locks initially
  56. CEventBuffer();
  57. ~CEventBuffer();
  58. // locking methods
  59. void Lock() { m_locks++; }
  60. void Unlock() { ASSERT(m_locks > 0); if (--m_locks == 0) ScFlushPostponed(); }
  61. bool IsLocked() { return m_locks != 0; }
  62. // event emitting / postponing
  63. SC ScEmitOrPostpone(IDispatch *pDispatch, DISPID dispid, CComVariant *pVar, int count);
  64. private:
  65. // helper emiting postponed events
  66. SC ScFlushPostponed();
  67. };
  68. /***************************************************************************\
  69. *
  70. * FUNCTION: GetEventBuffer
  71. *
  72. * PURPOSE: This function provides access to static object created in it's body
  73. * Having it as template allows us to define as many static objects as
  74. * interfaces we have.
  75. *
  76. * PARAMETERS:
  77. *
  78. * RETURNS:
  79. * CEventBuffer& - reference to the static object created inside
  80. *
  81. \***************************************************************************/
  82. MMCBASE_API CEventBuffer& GetEventBuffer();
  83. /***************************************************************************\
  84. *
  85. * CLASS: CEventLock
  86. *
  87. * PURPOSE: Template class to allow simple Lock()/Unlock() functionality
  88. * by placing instances of this class on the stack.
  89. * Constructor will put a lock on the event interface, destructor
  90. * will release it.
  91. *
  92. * USAGE: You can place the lock on stack by constructing object directly
  93. * or by using LockComEventInterface macro (which does the same)
  94. *
  95. \***************************************************************************/
  96. template <typename _dispinterface>
  97. class MMCBASE_API CEventLock
  98. {
  99. public:
  100. CEventLock() { GetEventBuffer().Lock(); }
  101. ~CEventLock() { GetEventBuffer().Unlock(); }
  102. };
  103. /***************************************************************************\
  104. *
  105. * MACRO: LockComEventInterface
  106. *
  107. * PURPOSE: Constructs the object on stack which holds a lock on event interface
  108. *
  109. \***************************************************************************/
  110. #define LockComEventInterface(_dispinterface) \
  111. CEventLock<_dispinterface> _LocalEventInterfaceLock;
  112. #endif // !defined(EVENTLOCK_H_INCLUDED)