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.

221 lines
4.5 KiB

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