aNavArray.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 Autodesk, Inc. All rights reserved.
  4. //
  5. // Use of this software is subject to the terms of the Autodesk license
  6. // agreement provided at the time of installation or download, or which
  7. // otherwise accompanies this software in either electronic or hard copy form.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef _ANavArray_h
  11. #define _ANavArray_h
  12. #pragma once
  13. #include "afxtempl.h" // MFC array template classes
  14. /////////////////////////////////////////////////////////////////////////////
  15. // File Navigation Array
  16. template <class T>
  17. class CNavArray : public CTypedPtrArray<CObArray, T*> {
  18. public:
  19. CNavArray ();
  20. virtual ~CNavArray ();
  21. // Data management
  22. protected:
  23. virtual T *NewData ();
  24. public:
  25. T *AddData ();
  26. int GetCount ();
  27. T *GetData (INT_PTR index);
  28. BOOL IsIndexValid (INT_PTR index);
  29. void RemoveData (INT_PTR index);
  30. void RemoveAllData ();
  31. };
  32. /////////////////////////////////////////////////////////////////////////////
  33. // File Navigation Array - implementation
  34. template <class T>
  35. CNavArray<T>::CNavArray () :
  36. CTypedPtrArray<CObArray, T*>()
  37. {
  38. }
  39. template <class T>
  40. CNavArray<T>::~CNavArray ()
  41. {
  42. RemoveAllData();
  43. }
  44. //----------------
  45. // Data management
  46. template <class T>
  47. T *CNavArray<T>::AddData ()
  48. {
  49. T *d = NewData();
  50. ASSERT(d != NULL);
  51. if (d != NULL)
  52. this->Add(d);
  53. return d;
  54. }
  55. template <class T>
  56. int CNavArray<T>::GetCount ()
  57. {
  58. const size_t nCount = this->GetUpperBound() + 1;
  59. ASSERT(nCount < 0x1000000); // 16M sanity check
  60. return (int)nCount;
  61. }
  62. template <class T>
  63. T *CNavArray<T>::GetData (INT_PTR index)
  64. {
  65. T *d = NULL;
  66. if (IsIndexValid(index))
  67. d = this->GetAt(index);
  68. return d;
  69. }
  70. template <class T>
  71. BOOL CNavArray<T>::IsIndexValid (INT_PTR index)
  72. {
  73. return ((index >= 0) && (index <= this->GetUpperBound()));
  74. }
  75. template <class T>
  76. T *CNavArray<T>::NewData ()
  77. {
  78. return new T;
  79. }
  80. template <class T>
  81. void CNavArray<T>::RemoveData (INT_PTR index)
  82. {
  83. T *d;
  84. if (IsIndexValid(index)) {
  85. d = this->GetAt(index);
  86. this->RemoveAt(index);
  87. delete d;
  88. }
  89. }
  90. template <class T>
  91. void CNavArray<T>::RemoveAllData ()
  92. {
  93. const int n = this->GetCount();
  94. for (int i = 0; i < n; i++) {
  95. T *d = this->GetAt(i);
  96. delete d;
  97. }
  98. this->RemoveAll();
  99. #ifndef _ADESK_MAC_
  100. this->FreeExtra();
  101. #endif //_ADESK_MAC_
  102. }
  103. /////////////////////////////////////////////////////////////////////////////
  104. //{{AFX_INSERT_LOCATION}}
  105. // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
  106. #endif