AllocationArray.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_ALLOCATIONARRAY_H
  13. #define SE_INCL_ALLOCATIONARRAY_H
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. #include <Engine/Templates/StaticStackArray.h>
  18. /*
  19. * Template class for stack-like array with static allocation of objects.
  20. */
  21. template<class Type>
  22. class CAllocationArray : public CStaticArray<Type> {
  23. public:
  24. CStaticStackArray<INDEX> aa_aiFreeElements; // array of indices of free elements
  25. INDEX aa_ctAllocationStep; // how many elements to allocate when pool overflows
  26. public:
  27. /* Default constructor. */
  28. inline CAllocationArray(void);
  29. /* Destructor. */
  30. inline ~CAllocationArray(void);
  31. /* Set how many elements to allocate when pool overflows. */
  32. inline void SetAllocationStep(INDEX ctStep);
  33. /* Create a given number of objects - do not use. */
  34. inline void New(INDEX iCount);
  35. /* Destroy all objects - do not use. */
  36. inline void Delete(void);
  37. /* Destroy all objects, and reset the array to initial (empty) state. */
  38. inline void Clear(void);
  39. /* Alocate a new object. */
  40. inline INDEX Allocate(void);
  41. /* Free object with given index. */
  42. inline void Free(INDEX iToFree);
  43. /* Free all objects, but keep pool space. */
  44. inline void FreeAll(void);
  45. // check if an index is allocated (slow!)
  46. inline BOOL IsAllocated(INDEX i);
  47. /* Random access operator. */
  48. inline Type &operator[](INDEX iObject);
  49. inline const Type &operator[](INDEX iObject) const;
  50. /* Get number of allocated objects in array. */
  51. INDEX Count(void) const;
  52. /* Get index of a object from it's pointer. */
  53. INDEX Index(Type *ptObject);
  54. /* Assignment operator. */
  55. CAllocationArray<Type> &operator=(const CAllocationArray<Type> &aaOriginal);
  56. };
  57. #endif /* include-once check. */