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.

219 lines
4.4 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. RemoteDesktopTopLevelObject
  5. Abstract:
  6. This module defines the common parent for all client-side
  7. RDP device redirection classes, CRemoteDesktopTopLevelObject.
  8. Author:
  9. Tad Brockway 02/00
  10. Revision History:
  11. --*/
  12. #ifndef __REMOTEDESKTOPTOPLEVELOBJECT_H__
  13. #define __REMOTEDESKTOPTOPLEVELOBJECT_H__
  14. #include <RemoteDesktop.h>
  15. #include <RemoteDesktopDBG.h>
  16. ///////////////////////////////////////////////////////////////
  17. //
  18. // CRemoteDesktopException
  19. //
  20. class CRemoteDesktopException
  21. {
  22. public:
  23. DWORD m_ErrorCode;
  24. CRemoteDesktopException(DWORD errorCode = 0) : m_ErrorCode(errorCode) {}
  25. };
  26. ///////////////////////////////////////////////////////////////
  27. //
  28. // CRemoteDesktopTopLevelObject
  29. //
  30. class CRemoteDesktopTopLevelObject
  31. {
  32. private:
  33. BOOL _isValid;
  34. protected:
  35. //
  36. // Remember if this instance is valid.
  37. //
  38. VOID SetValid(BOOL set) { _isValid = set; }
  39. public:
  40. //
  41. // Mark an instance as allocated or bogus.
  42. //
  43. #if DBG
  44. ULONG _magicNo;
  45. #endif
  46. //
  47. // Constructor/Destructor
  48. //
  49. CRemoteDesktopTopLevelObject() : _isValid(TRUE)
  50. {
  51. #if DBG
  52. _magicNo = GOODMEMMAGICNUMBER;
  53. #endif
  54. }
  55. virtual ~CRemoteDesktopTopLevelObject()
  56. {
  57. DC_BEGIN_FN("CRemoteDesktopTopLevelObject::~CRemoteDesktopTopLevelObject");
  58. #if DBG
  59. ASSERT(_magicNo == GOODMEMMAGICNUMBER);
  60. memset(&_magicNo, REMOTEDESKTOPBADMEM, sizeof(_magicNo));
  61. #endif
  62. SetValid(FALSE);
  63. DC_END_FN();
  64. }
  65. //
  66. // Return whether this class instance is valid.
  67. //
  68. virtual BOOL IsValid()
  69. {
  70. DC_BEGIN_FN("CRemoteDesktopTopLevelObject::IsValid");
  71. ASSERT(_magicNo == GOODMEMMAGICNUMBER);
  72. DC_END_FN();
  73. return _isValid;
  74. }
  75. //
  76. // Memory Management Operators
  77. //
  78. #if DBG
  79. #ifdef DEBUGMEM
  80. inline void *__cdecl operator new(size_t sz, DWORD tag=REMOTEDESKTOPOBJECT_TAG)
  81. {
  82. void *ptr = RemoteDesktopAllocateMem(sz, tag);
  83. return ptr;
  84. }
  85. inline void __cdecl operator delete(void *ptr)
  86. {
  87. RemoteDesktopFreeMem(ptr);
  88. }
  89. #endif
  90. #endif
  91. //
  92. // Return the class name.
  93. //
  94. virtual const LPTSTR ClassName() = 0;
  95. };
  96. ///////////////////////////////////////////////////////////////
  97. //
  98. // An STL Memory Allocator that Throws C++ Exception on Failure
  99. //
  100. template<class T> inline
  101. T *_RemoteDesktopAllocate(int sz, T *)
  102. {
  103. DC_BEGIN_FN("_RemoteDesktopAllocate");
  104. if (sz < 0)
  105. sz = 0;
  106. T* ret = (T *)operator new((size_t)sz * sizeof(T));
  107. if (ret == NULL) {
  108. TRC_ERR((TB, TEXT("Can't allocate %ld bytes."),
  109. (size_t)sz * sizeof(T)));
  110. DC_END_FN();
  111. throw CRemoteDesktopException(ERROR_NOT_ENOUGH_MEMORY);
  112. }
  113. DC_END_FN();
  114. return ret;
  115. }
  116. template<class T1, class T2> inline
  117. void _RemoteDesktopConstruct(T1 *ptr, const T2& args)
  118. {
  119. DC_BEGIN_FN("_RemoteDesktopConstruct");
  120. void *val = new ((void *)ptr)T1(args);
  121. if (val == NULL) {
  122. throw CRemoteDesktopException(ERROR_NOT_ENOUGH_MEMORY);
  123. }
  124. DC_END_FN();
  125. }
  126. template<class T> inline
  127. void _RemoteDesktopDestroy(T *ptr)
  128. {
  129. (ptr)->~T();
  130. }
  131. inline void _RemoteDesktopDestroy(char *ptr)
  132. {
  133. }
  134. inline void _RemoteDesktopDestroy(wchar_t *ptr)
  135. {
  136. }
  137. template<class T>
  138. class CRemoteDesktopAllocator {
  139. public:
  140. typedef size_t size_type;
  141. typedef int difference_type;
  142. typedef T *pointer;
  143. typedef const T *const_pointer;
  144. typedef T & reference;
  145. typedef const T & const_reference;
  146. typedef T value_type;
  147. pointer address(reference obj) const
  148. {return (&obj); }
  149. const_pointer address(const_reference obj) const
  150. {return (&obj); }
  151. pointer allocate(size_type sz, const void *) // throws REMOTDESKTOPEXCEPTION
  152. {return (_RemoteDesktopAllocate((difference_type)sz, (pointer)0)); }
  153. char *_Charalloc(size_type sz) // throws REMOTEDESKTOPEXCEPTION
  154. {return (_RemoteDesktopAllocate((difference_type)sz,
  155. (char *)0)); }
  156. void deallocate(void *ptr, size_type)
  157. {operator delete(ptr); }
  158. void construct(pointer ptr, const T& args)
  159. {_RemoteDesktopConstruct(ptr, args); }
  160. void destroy(pointer ptr)
  161. {_RemoteDesktopDestroy(ptr); }
  162. size_t max_size() const
  163. {size_t sz = (size_t)(-1) / sizeof(T);
  164. return (0 < sz ? sz : 1); }
  165. };
  166. #endif //__REMOTEDESKTOPTOPLEVELOBJECT_H__