Source code of Windows XP (NT5)
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.

205 lines
6.4 KiB

  1. // stdcooki.h : Declaration of base cookie class and related classes
  2. #ifndef __STDCOOKI_H_INCLUDED__
  3. #define __STDCOOKI_H_INCLUDED__
  4. // forward declarations
  5. class CCookie;
  6. class CRefcountedObject
  7. {
  8. public:
  9. inline CRefcountedObject()
  10. : m_nRefcount( 1 )
  11. {};
  12. virtual ~CRefcountedObject() {};
  13. inline void AddRef() {m_nRefcount++;}
  14. inline void Release()
  15. {
  16. if (0 >= --m_nRefcount)
  17. delete this;
  18. }
  19. private:
  20. int m_nRefcount;
  21. };
  22. class CHasMachineName
  23. {
  24. public:
  25. virtual void SetMachineName( LPCTSTR lpcszMachineName ) = 0;
  26. virtual LPCTSTR QueryNonNULLMachineName() = 0;
  27. virtual LPCTSTR QueryTargetServer() = 0;
  28. // returns <0, 0 or >0
  29. HRESULT CompareMachineNames( CHasMachineName& refHasMachineName, int* pnResult );
  30. };
  31. //
  32. // CBaseCookieBlock holds a block of cookies and the data
  33. // to which the cookies point. It starts off with a
  34. // reference count of 1. When a data object is created
  35. // which references one of these cookies, AddRef() the cookie block; when such
  36. // a data object is released, Release() the cookie block. Similarly,
  37. // when the parent cookie is finished with the cookie block, it should
  38. // Release() the cookie block. The cookie block will delete itself
  39. // when the reference count reaches 0. Do not attempt to explicitly
  40. // delete the cookie block.
  41. //
  42. class CBaseCookieBlock : public CRefcountedObject
  43. {
  44. public:
  45. virtual CCookie* QueryBaseCookie(int i) = 0;
  46. virtual int QueryNumCookies() = 0;
  47. };
  48. template<class COOKIE_TYPE>
  49. class CCookieBlock
  50. : public CBaseCookieBlock
  51. {
  52. private:
  53. COOKIE_TYPE* m_aCookies;
  54. int m_cCookies;
  55. public:
  56. CCookieBlock(COOKIE_TYPE* aCookies, // use vector ctor, we use vector dtor
  57. int cCookies );
  58. virtual ~CCookieBlock();
  59. virtual CCookie* QueryBaseCookie(int i);
  60. virtual int QueryNumCookies();
  61. };
  62. #define DEFINE_COOKIE_BLOCK(COOKIE_TYPE) \
  63. CCookieBlock<COOKIE_TYPE>::CCookieBlock<COOKIE_TYPE> \
  64. (COOKIE_TYPE* aCookies, int cCookies) \
  65. : m_aCookies( aCookies ) \
  66. , m_cCookies( cCookies ) \
  67. { \
  68. ASSERT(NULL != aCookies && 0 < cCookies); \
  69. } \
  70. CCookieBlock<COOKIE_TYPE>::~CCookieBlock<COOKIE_TYPE>() \
  71. { \
  72. delete[] m_aCookies; \
  73. } \
  74. CCookie* CCookieBlock<COOKIE_TYPE>::QueryBaseCookie(int i) \
  75. { \
  76. return (CCookie*)&(m_aCookies[i]); \
  77. } \
  78. int CCookieBlock<COOKIE_TYPE>::QueryNumCookies() \
  79. { \
  80. return m_cCookies; \
  81. }
  82. #define COMPARESIMILARCOOKIE_FULL -1
  83. //
  84. // I am trying to allow child classes to derive from CCookie using
  85. // multiple inheritance, but this is tricky
  86. //
  87. class CCookie
  88. {
  89. public:
  90. CTypedPtrList<CPtrList, CBaseCookieBlock*> m_listScopeCookieBlocks;
  91. CTypedPtrList<CPtrList, CBaseCookieBlock*> m_listResultCookieBlocks;
  92. HSCOPEITEM m_hScopeItem;
  93. private:
  94. LONG m_nResultCookiesRefcount;
  95. public:
  96. inline CCookie()
  97. : m_nResultCookiesRefcount( 0 ),
  98. m_hScopeItem (0)
  99. {
  100. }
  101. inline void ReleaseScopeChildren()
  102. {
  103. while ( !m_listScopeCookieBlocks.IsEmpty() )
  104. {
  105. (m_listScopeCookieBlocks.RemoveHead())->Release();
  106. }
  107. }
  108. // returns new refcount
  109. inline ULONG AddRefResultChildren()
  110. {
  111. return ++m_nResultCookiesRefcount;
  112. }
  113. inline void ReleaseResultChildren()
  114. {
  115. ASSERT( 0 < m_nResultCookiesRefcount );
  116. if ( 0 >= --m_nResultCookiesRefcount )
  117. {
  118. while ( !m_listResultCookieBlocks.IsEmpty() )
  119. {
  120. (m_listResultCookieBlocks.RemoveHead())->Release();
  121. }
  122. }
  123. }
  124. virtual ~CCookie();
  125. // On entry, if not COMPARESIMILARCOOKIE_FULL, *pnResult is the column on which to sort,
  126. // otherwise, try to do a full cookie comparison.
  127. // On exit, *pnResult should be <0, 0 or >0.
  128. // Note that this is a sorting function and should not be used to establish
  129. // object identity where better identity functions are available.
  130. virtual HRESULT CompareSimilarCookies( CCookie* pOtherCookie, int* pnResult ) = 0;
  131. };
  132. #define DECLARE_FORWARDS_MACHINE_NAME(targ) \
  133. virtual LPCTSTR QueryNonNULLMachineName(); \
  134. virtual LPCTSTR QueryTargetServer(); \
  135. virtual void SetMachineName( LPCTSTR lpcszMachineName );
  136. #define DEFINE_FORWARDS_MACHINE_NAME(thisclass,targ) \
  137. LPCTSTR thisclass::QueryNonNULLMachineName() \
  138. { \
  139. ASSERT( (targ) != NULL ); \
  140. return ((CHasMachineName*)(targ))->QueryNonNULLMachineName(); \
  141. } \
  142. LPCTSTR thisclass::QueryTargetServer() \
  143. { \
  144. ASSERT( (targ) != NULL ); \
  145. return ((CHasMachineName*)(targ))->QueryTargetServer(); \
  146. } \
  147. void thisclass::SetMachineName( LPCTSTR lpcszMachineName ) \
  148. { \
  149. ASSERT( (targ) != NULL ); \
  150. ((CHasMachineName*)(targ))->SetMachineName( lpcszMachineName ); \
  151. }
  152. #define STORES_MACHINE_NAME \
  153. public: \
  154. CString m_strMachineName; \
  155. virtual void SetMachineName( LPCTSTR lpcszMachineName ) \
  156. { \
  157. m_strMachineName = lpcszMachineName; \
  158. } \
  159. virtual LPCTSTR QueryNonNULLMachineName() \
  160. { \
  161. return (LPCTSTR)m_strMachineName; \
  162. } \
  163. virtual LPCTSTR QueryTargetServer() \
  164. { \
  165. return (m_strMachineName.IsEmpty()) \
  166. ? NULL : (LPCTSTR)(m_strMachineName); \
  167. }
  168. class CStoresMachineName : public CHasMachineName
  169. {
  170. public:
  171. CStoresMachineName( LPCTSTR lpcszMachineName )
  172. : m_strMachineName( lpcszMachineName )
  173. {}
  174. STORES_MACHINE_NAME;
  175. };
  176. #endif // ~__STDCOOKI_H_INCLUDED__