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.

281 lines
7.1 KiB

  1. /***************************************************************************\
  2. *
  3. * File: MsgObject.inl
  4. *
  5. * Description:
  6. * MsgObject.inl implements the "Message Object" class that is used to receive
  7. * messages in DirectUser. This object is created for each instance of a
  8. * class that is instantiated.
  9. *
  10. *
  11. * History:
  12. * 8/05/2000: JStall: Created
  13. *
  14. * Copyright (C) 2000 by Microsoft Corporation. All rights reserved.
  15. *
  16. \***************************************************************************/
  17. #if !defined(MSG__MsgObject_inl__INCLUDED)
  18. #define MSG__MsgObject_inl__INCLUDED
  19. #pragma once
  20. #include "MsgTable.h"
  21. /***************************************************************************\
  22. *****************************************************************************
  23. *
  24. * class MsgObject
  25. *
  26. *****************************************************************************
  27. \***************************************************************************/
  28. //------------------------------------------------------------------------------
  29. inline MsgObject *
  30. CastMsgObject(BaseObject * pbase)
  31. {
  32. if ((pbase != NULL) && TestFlag(pbase->GetHandleMask(), hmMsgObject)) {
  33. return (MsgObject *) pbase;
  34. }
  35. return NULL;
  36. }
  37. //------------------------------------------------------------------------------
  38. inline const MsgObject *
  39. CastMsgObject(const BaseObject * pbase)
  40. {
  41. if ((pbase != NULL) && TestFlag(pbase->GetHandleMask(), hmMsgObject)) {
  42. return (const MsgObject *) pbase;
  43. }
  44. return NULL;
  45. }
  46. //------------------------------------------------------------------------------
  47. inline MsgObject *
  48. ValidateMsgObject(HGADGET hgad)
  49. {
  50. return CastMsgObject(BaseObject::ValidateHandle(hgad));
  51. }
  52. //------------------------------------------------------------------------------
  53. inline
  54. MsgObject::MsgObject()
  55. {
  56. }
  57. //------------------------------------------------------------------------------
  58. inline
  59. MsgObject::~MsgObject()
  60. {
  61. }
  62. //------------------------------------------------------------------------------
  63. inline MsgObject *
  64. MsgObject::RawCastMsgObject(DUser::Gadget * pg)
  65. {
  66. Assert(pg != NULL);
  67. return reinterpret_cast<MsgObject *> (((BYTE *) pg) - offsetof(MsgObject, m_emo));
  68. }
  69. //------------------------------------------------------------------------------
  70. inline DUser::Gadget *
  71. MsgObject::RawCastGadget(MsgObject * pmo)
  72. {
  73. Assert(pmo != NULL);
  74. return reinterpret_cast<DUser::Gadget *> (((BYTE *) pmo) + offsetof(MsgObject, m_emo));
  75. }
  76. //------------------------------------------------------------------------------
  77. inline DUser::Gadget *
  78. MsgObject::CastGadget(HGADGET hgad)
  79. {
  80. return CastGadget(ValidateMsgObject(hgad));
  81. }
  82. //------------------------------------------------------------------------------
  83. inline DUser::Gadget *
  84. MsgObject::CastGadget(MsgObject * pmo)
  85. {
  86. if (pmo == NULL) {
  87. return NULL;
  88. } else {
  89. return RawCastGadget(pmo);
  90. }
  91. }
  92. //------------------------------------------------------------------------------
  93. inline HGADGET
  94. MsgObject::CastHandle(DUser::Gadget * pg)
  95. {
  96. if (pg == NULL) {
  97. return NULL;
  98. } else {
  99. return (HGADGET) RawCastMsgObject(pg)->GetHandle();
  100. }
  101. }
  102. //------------------------------------------------------------------------------
  103. inline HGADGET
  104. MsgObject::CastHandle(MsgObject * pmo)
  105. {
  106. if (pmo == NULL) {
  107. return NULL;
  108. } else {
  109. return (HGADGET) pmo->GetHandle();
  110. }
  111. }
  112. //------------------------------------------------------------------------------
  113. inline MsgObject *
  114. MsgObject::CastMsgObject(DUser::Gadget * pg)
  115. {
  116. if (pg == NULL) {
  117. return NULL;
  118. } else {
  119. return RawCastMsgObject(pg);
  120. }
  121. }
  122. //------------------------------------------------------------------------------
  123. inline MsgObject *
  124. MsgObject::CastMsgObject(HGADGET hgad)
  125. {
  126. return ValidateMsgObject(hgad);
  127. }
  128. //------------------------------------------------------------------------------
  129. inline DUser::Gadget *
  130. MsgObject::GetGadget() const
  131. {
  132. return RawCastGadget(const_cast<MsgObject *>(this));
  133. }
  134. //------------------------------------------------------------------------------
  135. inline HRESULT
  136. MsgObject::PreAllocThis(int cSlots)
  137. {
  138. #if DBG
  139. AssertMsg(m_emo.m_arpThis.GetSize() == 0, "Only can preallocate once");
  140. #endif
  141. BOOL fSuccess = m_emo.m_arpThis.SetSize(cSlots);
  142. #if DBG
  143. if (fSuccess) {
  144. for (int idxSlot = 0 ; idxSlot < cSlots; idxSlot++) {
  145. m_emo.m_arpThis[idxSlot] = ULongToPtr(0xA0E20000 + idxSlot);
  146. }
  147. }
  148. #endif
  149. return fSuccess ? S_OK : E_OUTOFMEMORY;
  150. }
  151. //------------------------------------------------------------------------------
  152. inline void
  153. MsgObject::FillThis(int idxSlotStart, int idxSlotEnd, void * pvThis, const MsgTable * pmtNew)
  154. {
  155. AssertMsg(idxSlotStart <= idxSlotEnd, "Must give valid indicies");
  156. AssertMsg(idxSlotEnd < m_emo.m_arpThis.GetSize(), "Must preallocate this array");
  157. for (int idxSlot = idxSlotStart; idxSlot <= idxSlotEnd; idxSlot++) {
  158. AssertMsg(m_emo.m_arpThis[idxSlot] == ULongToPtr(0xA0E20000 + idxSlot),
  159. "Slot must not be already set");
  160. m_emo.m_arpThis[idxSlot] = pvThis;
  161. }
  162. m_emo.m_pmt = pmtNew;
  163. }
  164. //------------------------------------------------------------------------------
  165. inline HRESULT
  166. MsgObject::Promote(void * pvThis, const MsgTable * pmtNew)
  167. {
  168. int idxAdd = m_emo.m_arpThis.Add(pvThis);
  169. if (idxAdd < 0) {
  170. return E_OUTOFMEMORY;
  171. }
  172. m_emo.m_pmt = pmtNew;
  173. return S_OK;
  174. }
  175. //------------------------------------------------------------------------------
  176. inline void
  177. MsgObject::Demote(int cLevels)
  178. {
  179. int cThis = m_emo.m_arpThis.GetSize();
  180. AssertMsg(cThis >= 1, "Must have been previously promoted");
  181. AssertMsg(cLevels <= cThis, "Can only remove as many levels as available");
  182. Verify(m_emo.m_arpThis.SetSize(cThis - cLevels));
  183. }
  184. //------------------------------------------------------------------------------
  185. inline void *
  186. MsgObject::GetThis(int idxThis) const
  187. {
  188. return m_emo.m_arpThis[idxThis];
  189. }
  190. //------------------------------------------------------------------------------
  191. inline int
  192. MsgObject::GetDepth() const
  193. {
  194. return m_emo.m_arpThis.GetSize();
  195. }
  196. //------------------------------------------------------------------------------
  197. inline int
  198. MsgObject::GetBuildDepth() const
  199. {
  200. return m_emo.m_pmt != NULL ? m_emo.m_pmt->GetDepth() + 1 : 0;
  201. }
  202. //------------------------------------------------------------------------------
  203. inline void
  204. MsgObject::InvokeMethod(
  205. IN MethodMsg * pmsg // Method to invoke
  206. ) const
  207. {
  208. GetGadget()->CallStubMethod(pmsg);
  209. }
  210. //------------------------------------------------------------------------------
  211. inline DUser::Gadget *
  212. MsgObject::CastClass(const MsgClass * pmcTest) const
  213. {
  214. if (InstanceOf(pmcTest)) {
  215. return GetGadget();
  216. } else {
  217. return NULL;
  218. }
  219. }
  220. #endif // MSG__MsgObject_inl__INCLUDED