The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

126 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_ARRAYALLOCATIONBASE_JUCEHEADER__
  19. #define __JUCE_ARRAYALLOCATIONBASE_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. Implements some basic array storage allocation functions.
  23. This class isn't really for public use - it's used by the other
  24. array classes, but might come in handy for some purposes.
  25. @see Array, OwnedArray, ReferenceCountedArray
  26. */
  27. template <class ElementType>
  28. class ArrayAllocationBase
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates an empty array. */
  33. ArrayAllocationBase() throw()
  34. : elements (0),
  35. numAllocated (0)
  36. {
  37. }
  38. /** Destructor. */
  39. ~ArrayAllocationBase()
  40. {
  41. delete[] elements;
  42. }
  43. //==============================================================================
  44. /** Changes the amount of storage allocated.
  45. This will retain any data currently held in the array, and either add or
  46. remove extra space at the end.
  47. @param numElements the number of elements that are needed
  48. */
  49. void setAllocatedSize (const int numElements) throw()
  50. {
  51. if (numAllocated != numElements)
  52. {
  53. if (numElements > 0)
  54. {
  55. ElementType* const newElements = new ElementType [numElements];
  56. const int itemsToRetain = jmin (numElements, numAllocated);
  57. for (int i = 0; i < itemsToRetain; ++i)
  58. newElements[i] = elements[i];
  59. delete[] elements;
  60. elements = newElements;
  61. }
  62. else
  63. {
  64. delete[] elements;
  65. elements = 0;
  66. }
  67. numAllocated = numElements;
  68. }
  69. }
  70. /** Increases the amount of storage allocated if it is less than a given amount.
  71. This will retain any data currently held in the array, but will add
  72. extra space at the end to make sure there it's at least as big as the size
  73. passed in. If it's already bigger, no action is taken.
  74. @param minNumElements the minimum number of elements that are needed
  75. */
  76. void ensureAllocatedSize (int minNumElements) throw()
  77. {
  78. if (minNumElements > numAllocated)
  79. setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
  80. }
  81. /** Minimises the amount of storage allocated so that it's no more than
  82. the given number of elements.
  83. */
  84. void shrinkToNoMoreThan (int maxNumElements) throw()
  85. {
  86. if (maxNumElements < numAllocated)
  87. setAllocatedSize (maxNumElements);
  88. }
  89. //==============================================================================
  90. ElementType* elements;
  91. int numAllocated;
  92. private:
  93. ArrayAllocationBase (const ArrayAllocationBase&);
  94. const ArrayAllocationBase& operator= (const ArrayAllocationBase&);
  95. };
  96. #endif // __JUCE_ARRAYALLOCATIONBASE_JUCEHEADER__