Old fashion design for PC-104
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.

83 lines
1.9 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 02/08/2001
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: GenericList.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of GenericList.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "GenericList.h"
  16. template<class TYPE, TYPE def>
  17. CGenericList<TYPE,def>::CGenericList()
  18. {
  19. m_nData = 0;
  20. m_pData = NULL;
  21. }
  22. template<class TYPE, TYPE def>
  23. CGenericList<TYPE,def>::~CGenericList()
  24. {
  25. RemoveAll();
  26. }
  27. template<class TYPE, TYPE def>
  28. BOOL CGenericList<TYPE,def>::Add( TYPE pData, INT nIndex )
  29. {
  30. // allocate space for new element
  31. m_pData = (TYPE*) realloc( m_pData, sizeof(TYPE) * ( m_nData + 1 ) );
  32. if( !m_pData )
  33. {
  34. m_nData = 0;
  35. return FALSE;
  36. }
  37. // add to end
  38. if( nIndex < 0 || nIndex >= m_nData )
  39. nIndex = m_nData;
  40. // shift elements
  41. ::MoveMemory( &m_pData[ nIndex + 1 ], &m_pData[ nIndex ], sizeof(TYPE) * ( m_nData - nIndex ) );
  42. // initialize new element
  43. m_pData[ nIndex ] = pData;
  44. return TRUE;
  45. }
  46. template<class TYPE, TYPE def>
  47. BOOL CGenericList<TYPE,def>::Remove( INT nIndex )
  48. {
  49. // validate position
  50. if( nIndex < 0 || nIndex >= m_nData )
  51. return FALSE;
  52. // decrease number of elements
  53. m_nData --;
  54. // shift elements
  55. ::MoveMemory( &m_pData[ nIndex ], &m_pData[ nIndex + 1 ], sizeof(TYPE) * ( m_nData - nIndex ) );
  56. // deallocate space
  57. m_pData = (TYPE*) realloc( m_pData, sizeof(TYPE) * m_nData );
  58. if( !m_pData )
  59. {
  60. m_nData = 0;
  61. return FALSE;
  62. }
  63. return TRUE;
  64. }
  65. template<class TYPE, TYPE def>
  66. void CGenericList<TYPE,def>::RemoveAll()
  67. {
  68. while( GetCount() )
  69. Remove( 0 );
  70. }