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.

238 lines
5.6 KiB

  1. //
  2. // cuiarray.cpp
  3. // = array object in CUILib =
  4. //
  5. #include "private.h"
  6. #include "cuiarray.h"
  7. /*=============================================================================*/
  8. /* */
  9. /* C U I F O B J E C T A R R A Y */
  10. /* */
  11. /*=============================================================================*/
  12. /* C U I F O B J E C T A R R A Y */
  13. /*------------------------------------------------------------------------------
  14. constructor of CUIFObjectArrayBase
  15. ------------------------------------------------------------------------------*/
  16. CUIFObjectArrayBase::CUIFObjectArrayBase( void )
  17. {
  18. m_pBuffer = NULL;
  19. m_nBuffer = 0;
  20. m_nObject = 0;
  21. }
  22. /* ~ C U I F O B J E C T A R R A Y */
  23. /*------------------------------------------------------------------------------
  24. destructor of CUIFObjectArrayBase
  25. ------------------------------------------------------------------------------*/
  26. CUIFObjectArrayBase::~CUIFObjectArrayBase( void )
  27. {
  28. if (m_pBuffer) {
  29. MemFree( m_pBuffer );
  30. }
  31. }
  32. /* A D D */
  33. /*------------------------------------------------------------------------------
  34. Add object to the list
  35. ------------------------------------------------------------------------------*/
  36. BOOL CUIFObjectArrayBase::Add( void *pv )
  37. {
  38. // sanity check
  39. if (pv == NULL) {
  40. Assert( FALSE );
  41. return FALSE;
  42. }
  43. // check if the object is alrady in the list
  44. if (0 <= Find( pv )) {
  45. return FALSE;
  46. }
  47. // ensure buffer size
  48. if (!EnsureBuffer( m_nObject + 1 )) {
  49. return FALSE;
  50. }
  51. // add to list
  52. Assert( m_nObject < m_nBuffer );
  53. m_pBuffer[ m_nObject ] = pv;
  54. m_nObject++;
  55. return TRUE;
  56. }
  57. /* R E M O V E */
  58. /*------------------------------------------------------------------------------
  59. Remove object from the list
  60. ------------------------------------------------------------------------------*/
  61. BOOL CUIFObjectArrayBase::Remove( void *pv )
  62. {
  63. int i;
  64. // sanity check
  65. if (pv == NULL) {
  66. Assert( FALSE );
  67. return FALSE;
  68. }
  69. // check if the object is in the list
  70. i = Find( pv );
  71. if (i < 0) {
  72. return FALSE;
  73. }
  74. // remove from the list
  75. if (i < m_nObject - 1) {
  76. MemMove( &m_pBuffer[ i ], &m_pBuffer[ i+1 ], (m_nObject-i-1) * sizeof(void*) );
  77. }
  78. m_nObject--;
  79. return TRUE;
  80. }
  81. /* G E T C O U N T */
  82. /*------------------------------------------------------------------------------
  83. Get count of objects in the list
  84. ------------------------------------------------------------------------------*/
  85. int CUIFObjectArrayBase::GetCount( void )
  86. {
  87. return m_nObject;
  88. }
  89. /* G E T */
  90. /*------------------------------------------------------------------------------
  91. Get object in the list
  92. ------------------------------------------------------------------------------*/
  93. void *CUIFObjectArrayBase::Get( int i )
  94. {
  95. if (i < 0 || m_nObject <= i) {
  96. return NULL;
  97. }
  98. return m_pBuffer[ i ];
  99. }
  100. /* G E T F I R S T */
  101. /*------------------------------------------------------------------------------
  102. Get fist object in the list
  103. ------------------------------------------------------------------------------*/
  104. void *CUIFObjectArrayBase::GetFirst( void )
  105. {
  106. return Get( 0 );
  107. }
  108. /* G E T L A S T */
  109. /*------------------------------------------------------------------------------
  110. Get last object in the list
  111. ------------------------------------------------------------------------------*/
  112. void *CUIFObjectArrayBase::GetLast( void )
  113. {
  114. return Get( m_nObject - 1 );
  115. }
  116. /* F I N D */
  117. /*------------------------------------------------------------------------------
  118. Find object
  119. Returns index of object in list when found, -1 when not found.
  120. ------------------------------------------------------------------------------*/
  121. int CUIFObjectArrayBase::Find( void *pv )
  122. {
  123. int i;
  124. for (i = 0; i < m_nObject; i++) {
  125. if (m_pBuffer[i] == pv) {
  126. return i;
  127. }
  128. }
  129. return -1;
  130. }
  131. /* E N S U R E B U F F E R */
  132. /*------------------------------------------------------------------------------
  133. Ensure buffer size (create/enlarge buffer when no more room)
  134. Returns TRUE when buffer size is enough, FALSE when error occured
  135. ------------------------------------------------------------------------------*/
  136. BOOL CUIFObjectArrayBase::EnsureBuffer( int iSize )
  137. {
  138. void **pBufferNew;
  139. int nBufferNew;
  140. Assert( 0 < iSize );
  141. // check if there is room
  142. if (iSize <= m_nBuffer) {
  143. Assert( m_pBuffer != NULL );
  144. return TRUE;
  145. }
  146. // calc new buffer size
  147. nBufferNew = ((iSize - 1) / 16 + 1) * 16;
  148. // create new buffer
  149. if (m_pBuffer == NULL) {
  150. Assert( m_nBuffer == 0 );
  151. pBufferNew = (void**)MemAlloc( nBufferNew * sizeof(void*) );
  152. }
  153. else {
  154. Assert( 0 < m_nBuffer );
  155. pBufferNew = (void**)MemReAlloc( m_pBuffer, nBufferNew * sizeof(void*) );
  156. }
  157. // check if buffer has been created
  158. if (pBufferNew == NULL) {
  159. Assert( FALSE );
  160. return FALSE;
  161. }
  162. // update buffer info
  163. m_pBuffer = pBufferNew;
  164. m_nBuffer = nBufferNew;
  165. return TRUE;
  166. }