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.

174 lines
4.2 KiB

  1. #ifndef _SMALLUTIL_H_
  2. #define _SMALLUTIL_H_
  3. // CCancellableThread
  4. //
  5. // Lets you define a thread object that can be cancelled by the creator.
  6. //To implement the thread, derivce from CCancellableThread and override the
  7. //run() function. run() will be ran in its own thread, and the value returned
  8. //by run can be accessed by GetResult(). run() should check IsCancelled()
  9. //at appropriate intervals and exit early if true.
  10. // Clients of the cancellable thread then create the object, and execute Run()
  11. //when ready. If they wish to cancel, they can call NotifyCancel().
  12. class CCancellableThread
  13. {
  14. private:
  15. HANDLE _hCancelEvent;
  16. HANDLE _hThread;
  17. static DWORD WINAPI threadProc(void *pParameter);
  18. BOOL _fIsFinished;
  19. DWORD _dwThreadResult;
  20. public:
  21. CCancellableThread();
  22. ~CCancellableThread();
  23. virtual BOOL Initialize();
  24. BOOL IsCancelled();
  25. BOOL IsFinished();
  26. BOOL GetResult(PDWORD pdwResult);
  27. BOOL Run();
  28. BOOL NotifyCancel();
  29. BOOL WaitForNotRunning(DWORD dwMilliseconds, PBOOL pfFinished = NULL);
  30. protected:
  31. virtual DWORD run() = 0;
  32. };
  33. // CQueueSortOf - a queue(sort of) used to store stuff
  34. //in a renumerable way..
  35. class CQueueSortOf
  36. {
  37. // This sort-of-queue is just a data structure to build up a list of items
  38. //(always adding to the end) and then be able to enumerate that list
  39. //repeatedly from start to end.
  40. // The list does not own any of the objects added to it..
  41. typedef struct SEntry
  42. {
  43. SEntry* pNext;
  44. void* data;
  45. } *PSEntry;
  46. PSEntry m_pHead, m_pTail;
  47. public:
  48. CQueueSortOf()
  49. {
  50. m_pHead = NULL;
  51. m_pTail = NULL;
  52. }
  53. ~CQueueSortOf()
  54. {
  55. while(m_pHead != NULL)
  56. {
  57. PSEntry temp = m_pHead;
  58. m_pHead = m_pHead->pNext;
  59. delete temp;
  60. }
  61. }
  62. bool InsertAtEnd(void* newElement)
  63. {
  64. PSEntry pNewEntry = new SEntry;
  65. if (pNewEntry == NULL)
  66. return false;
  67. pNewEntry->data = newElement;
  68. pNewEntry->pNext = NULL;
  69. if (m_pHead == NULL)
  70. {
  71. m_pHead = m_pTail = pNewEntry;
  72. }
  73. else
  74. {
  75. m_pTail->pNext = pNewEntry;
  76. m_pTail = pNewEntry;
  77. }
  78. return true;
  79. }
  80. // enumerations are managed by an 'iterator' which simply a void pointer into
  81. //the list. To start an enumeration, pass NULL as the iterator. The end
  82. //of enumeration will be indicated by a NULL iterator being returned.
  83. void* StepEnumerate(void* iterator)
  84. {
  85. return (iterator == NULL) ? m_pHead : ((PSEntry)iterator)->pNext;
  86. }
  87. void* Get(void* iterator)
  88. {
  89. return ((PSEntry)iterator)->data;
  90. }
  91. };
  92. // CGrowingString is a simple utility class that allows you to create
  93. //a string and append to it without worrying about reallocating memory
  94. //every time.
  95. class CGrowingString
  96. {
  97. public:
  98. WCHAR* m_pszString;
  99. long m_iBufferSize;
  100. long m_iStringLength;
  101. CGrowingString()
  102. {
  103. m_pszString = NULL;
  104. m_iBufferSize = 0;
  105. m_iStringLength = 0;
  106. }
  107. ~CGrowingString()
  108. {
  109. delete[] m_pszString;
  110. }
  111. BOOL AppendToString(LPCWSTR pszNew)
  112. {
  113. long iLength = lstrlen(pszNew);
  114. if (m_pszString == NULL
  115. || m_iStringLength + iLength + 1 > m_iBufferSize)
  116. {
  117. long iNewSize = max(1024, m_iStringLength + iLength * 10);
  118. WCHAR* pNewBuffer = new WCHAR[iNewSize];
  119. if (pNewBuffer == NULL)
  120. return FALSE;
  121. if (m_pszString == NULL)
  122. {
  123. m_pszString = pNewBuffer;
  124. m_iBufferSize = iNewSize;
  125. }
  126. else
  127. {
  128. StrCpyNW(pNewBuffer, m_pszString, m_iStringLength + 1);
  129. delete[] m_pszString;
  130. m_pszString = pNewBuffer;
  131. m_iBufferSize = iNewSize;
  132. }
  133. }
  134. StrCpyNW(m_pszString + m_iStringLength, pszNew, iLength+1);
  135. m_iStringLength += iLength;
  136. m_pszString[m_iStringLength] = L'\0';
  137. return TRUE;
  138. }
  139. };
  140. #endif