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.

545 lines
18KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. /**
  20. A basic object container.
  21. This class isn't really for public use - it's used by the other
  22. array classes, but might come in handy for some purposes.
  23. It inherits from a critical section class to allow the arrays to use
  24. the "empty base class optimisation" pattern to reduce their footprint.
  25. @see Array, OwnedArray, ReferenceCountedArray
  26. @tags{Core}
  27. */
  28. template <class ElementType, class TypeOfCriticalSectionToUse>
  29. class ArrayBase : public TypeOfCriticalSectionToUse
  30. {
  31. private:
  32. using ParameterType = typename TypeHelpers::ParameterType<ElementType>::type;
  33. public:
  34. //==============================================================================
  35. ArrayBase() = default;
  36. ~ArrayBase()
  37. {
  38. clear();
  39. }
  40. ArrayBase (ArrayBase&& other) noexcept
  41. : elements (std::move (other.elements)),
  42. numAllocated (other.numAllocated),
  43. numUsed (other.numUsed)
  44. {
  45. other.numAllocated = 0;
  46. other.numUsed = 0;
  47. }
  48. ArrayBase& operator= (ArrayBase&& other) noexcept
  49. {
  50. if (this != &other)
  51. {
  52. auto tmp (std::move (other));
  53. swapWith (tmp);
  54. }
  55. return *this;
  56. }
  57. //==============================================================================
  58. template <class OtherArrayType>
  59. bool operator== (const OtherArrayType& other) const noexcept
  60. {
  61. if (size() != (int) other.size())
  62. return false;
  63. auto* e = begin();
  64. for (auto& o : other)
  65. if (! (*e++ == o))
  66. return false;
  67. return true;
  68. }
  69. template <class OtherArrayType>
  70. bool operator!= (const OtherArrayType& other) const noexcept
  71. {
  72. return ! operator== (other);
  73. }
  74. //==============================================================================
  75. inline ElementType& operator[] (const int index) const noexcept
  76. {
  77. jassert (elements != nullptr);
  78. jassert (isPositiveAndBelow (index, numUsed));
  79. return elements[index];
  80. }
  81. inline ElementType getValueWithDefault (const int index) const noexcept
  82. {
  83. return isPositiveAndBelow (index, numUsed) ? elements[index] : ElementType();
  84. }
  85. inline ElementType getFirst() const noexcept
  86. {
  87. return numUsed > 0 ? elements[0] : ElementType();
  88. }
  89. inline ElementType getLast() const noexcept
  90. {
  91. return numUsed > 0 ? elements[numUsed - 1] : ElementType();
  92. }
  93. //==============================================================================
  94. inline ElementType* begin() const noexcept
  95. {
  96. return elements;
  97. }
  98. inline ElementType* end() const noexcept
  99. {
  100. return elements + numUsed;
  101. }
  102. inline ElementType* data() const noexcept
  103. {
  104. return elements;
  105. }
  106. inline int size() const noexcept
  107. {
  108. return numUsed;
  109. }
  110. inline int capacity() const noexcept
  111. {
  112. return numAllocated;
  113. }
  114. //==============================================================================
  115. void setAllocatedSize (int numElements)
  116. {
  117. jassert (numElements >= numUsed);
  118. if (numAllocated != numElements)
  119. {
  120. if (numElements > 0)
  121. setAllocatedSizeInternal (numElements);
  122. else
  123. elements.free();
  124. }
  125. numAllocated = numElements;
  126. }
  127. void ensureAllocatedSize (int minNumElements)
  128. {
  129. if (minNumElements > numAllocated)
  130. setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
  131. jassert (numAllocated <= 0 || elements != nullptr);
  132. }
  133. void shrinkToNoMoreThan (int maxNumElements)
  134. {
  135. if (maxNumElements < numAllocated)
  136. setAllocatedSize (maxNumElements);
  137. }
  138. void clear()
  139. {
  140. for (int i = 0; i < numUsed; ++i)
  141. elements[i].~ElementType();
  142. numUsed = 0;
  143. }
  144. //==============================================================================
  145. void swapWith (ArrayBase& other) noexcept
  146. {
  147. elements.swapWith (other.elements);
  148. std::swap (numAllocated, other.numAllocated);
  149. std::swap (numUsed, other.numUsed);
  150. }
  151. //==============================================================================
  152. void add (const ElementType& newElement)
  153. {
  154. checkSourceIsNotAMember (&newElement);
  155. ensureAllocatedSize (numUsed + 1);
  156. addAssumingCapacityIsReady (newElement);
  157. }
  158. void add (ElementType&& newElement)
  159. {
  160. checkSourceIsNotAMember (&newElement);
  161. ensureAllocatedSize (numUsed + 1);
  162. addAssumingCapacityIsReady (std::move (newElement));
  163. }
  164. template <typename... OtherElements>
  165. void add (const ElementType& firstNewElement, OtherElements... otherElements)
  166. {
  167. checkSourceIsNotAMember (&firstNewElement);
  168. ensureAllocatedSize (numUsed + 1 + (int) sizeof... (otherElements));
  169. addAssumingCapacityIsReady (firstNewElement, otherElements...);
  170. }
  171. template <typename... OtherElements>
  172. void add (ElementType&& firstNewElement, OtherElements... otherElements)
  173. {
  174. checkSourceIsNotAMember (&firstNewElement);
  175. ensureAllocatedSize (numUsed + 1 + (int) sizeof... (otherElements));
  176. addAssumingCapacityIsReady (std::move (firstNewElement), otherElements...);
  177. }
  178. //==============================================================================
  179. template <typename Type>
  180. void addArray (const Type* elementsToAdd, int numElementsToAdd)
  181. {
  182. ensureAllocatedSize (numUsed + numElementsToAdd);
  183. addArrayInternal (elementsToAdd, numElementsToAdd);
  184. numUsed += numElementsToAdd;
  185. }
  186. template <typename TypeToCreateFrom>
  187. void addArray (const std::initializer_list<TypeToCreateFrom>& items)
  188. {
  189. ensureAllocatedSize (numUsed + (int) items.size());
  190. for (auto& item : items)
  191. new (elements + numUsed++) ElementType (item);
  192. }
  193. template <class OtherArrayType>
  194. void addArray (const OtherArrayType& arrayToAddFrom)
  195. {
  196. jassert ((const void*) this != (const void*) &arrayToAddFrom); // can't add from our own elements!
  197. ensureAllocatedSize (numUsed + (int) arrayToAddFrom.size());
  198. for (auto& e : arrayToAddFrom)
  199. addAssumingCapacityIsReady (e);
  200. }
  201. template <class OtherArrayType>
  202. typename std::enable_if<! std::is_pointer<OtherArrayType>::value, int>::type
  203. addArray (const OtherArrayType& arrayToAddFrom,
  204. int startIndex, int numElementsToAdd = -1)
  205. {
  206. jassert ((const void*) this != (const void*) &arrayToAddFrom); // can't add from our own elements!
  207. if (startIndex < 0)
  208. {
  209. jassertfalse;
  210. startIndex = 0;
  211. }
  212. if (numElementsToAdd < 0 || startIndex + numElementsToAdd > (int) arrayToAddFrom.size())
  213. numElementsToAdd = (int) arrayToAddFrom.size() - startIndex;
  214. addArray (arrayToAddFrom.data() + startIndex, numElementsToAdd);
  215. return numElementsToAdd;
  216. }
  217. //==============================================================================
  218. void insert (int indexToInsertAt, ParameterType newElement, int numberOfTimesToInsertIt)
  219. {
  220. checkSourceIsNotAMember (&newElement);
  221. auto* space = createInsertSpace (indexToInsertAt, numberOfTimesToInsertIt);
  222. for (int i = 0; i < numberOfTimesToInsertIt; ++i)
  223. new (space++) ElementType (newElement);
  224. numUsed += numberOfTimesToInsertIt;
  225. }
  226. void insertArray (int indexToInsertAt, const ElementType* newElements, int numberOfElements)
  227. {
  228. auto* space = createInsertSpace (indexToInsertAt, numberOfElements);
  229. for (int i = 0; i < numberOfElements; ++i)
  230. new (space++) ElementType (*(newElements++));
  231. numUsed += numberOfElements;
  232. }
  233. //==============================================================================
  234. void removeElements (int indexToRemoveAt, int numElementsToRemove)
  235. {
  236. jassert (indexToRemoveAt >= 0);
  237. jassert (numElementsToRemove >= 0);
  238. jassert (indexToRemoveAt + numElementsToRemove <= numUsed);
  239. if (numElementsToRemove > 0)
  240. {
  241. removeElementsInternal (indexToRemoveAt, numElementsToRemove);
  242. numUsed -= numElementsToRemove;
  243. }
  244. }
  245. //==============================================================================
  246. void swap (int index1, int index2)
  247. {
  248. if (isPositiveAndBelow (index1, numUsed)
  249. && isPositiveAndBelow (index2, numUsed))
  250. {
  251. std::swap (elements[index1],
  252. elements[index2]);
  253. }
  254. }
  255. //==============================================================================
  256. void move (int currentIndex, int newIndex) noexcept
  257. {
  258. if (isPositiveAndBelow (currentIndex, numUsed))
  259. {
  260. if (! isPositiveAndBelow (newIndex, numUsed))
  261. newIndex = numUsed - 1;
  262. moveInternal (currentIndex, newIndex);
  263. }
  264. }
  265. private:
  266. //==============================================================================
  267. template <typename T>
  268. using TriviallyCopyableVoid = typename std::enable_if<std::is_trivially_copyable<T>::value, void>::type;
  269. template <typename T>
  270. using NonTriviallyCopyableVoid = typename std::enable_if<! std::is_trivially_copyable<T>::value, void>::type;
  271. //==============================================================================
  272. template <typename T = ElementType>
  273. TriviallyCopyableVoid<T> addArrayInternal (const ElementType* otherElements, int numElements)
  274. {
  275. memcpy (elements + numUsed, otherElements, (size_t) numElements * sizeof (ElementType));
  276. }
  277. template <typename Type, typename T = ElementType>
  278. TriviallyCopyableVoid<T> addArrayInternal (const Type* otherElements, int numElements)
  279. {
  280. auto* start = elements + numUsed;
  281. while (--numElements >= 0)
  282. new (start++) ElementType (*(otherElements++));
  283. }
  284. template <typename Type, typename T = ElementType>
  285. NonTriviallyCopyableVoid<T> addArrayInternal (const Type* otherElements, int numElements)
  286. {
  287. auto* start = elements + numUsed;
  288. while (--numElements >= 0)
  289. new (start++) ElementType (*(otherElements++));
  290. }
  291. //==============================================================================
  292. template <typename T = ElementType>
  293. TriviallyCopyableVoid<T> setAllocatedSizeInternal (int numElements)
  294. {
  295. elements.realloc ((size_t) numElements);
  296. }
  297. template <typename T = ElementType>
  298. NonTriviallyCopyableVoid<T> setAllocatedSizeInternal (int numElements)
  299. {
  300. HeapBlock<ElementType> newElements (numElements);
  301. for (int i = 0; i < numUsed; ++i)
  302. {
  303. new (newElements + i) ElementType (std::move (elements[i]));
  304. elements[i].~ElementType();
  305. }
  306. elements = std::move (newElements);
  307. }
  308. //==============================================================================
  309. ElementType* createInsertSpace (int indexToInsertAt, int numElements)
  310. {
  311. ensureAllocatedSize (numUsed + numElements);
  312. if (! isPositiveAndBelow (indexToInsertAt, numUsed))
  313. return elements + numUsed;
  314. createInsertSpaceInternal (indexToInsertAt, numElements);
  315. return elements + indexToInsertAt;
  316. }
  317. template <typename T = ElementType>
  318. TriviallyCopyableVoid<T> createInsertSpaceInternal (int indexToInsertAt, int numElements)
  319. {
  320. auto* start = elements + indexToInsertAt;
  321. auto numElementsToShift = numUsed - indexToInsertAt;
  322. memmove (start + numElements, start, (size_t) numElementsToShift * sizeof (ElementType));
  323. }
  324. template <typename T = ElementType>
  325. NonTriviallyCopyableVoid<T> createInsertSpaceInternal (int indexToInsertAt, int numElements)
  326. {
  327. auto* end = elements + numUsed;
  328. auto* newEnd = end + numElements;
  329. auto numElementsToShift = numUsed - indexToInsertAt;
  330. for (int i = 0; i < numElementsToShift; ++i)
  331. {
  332. new (--newEnd) ElementType (std::move (*(--end)));
  333. end->~ElementType();
  334. }
  335. }
  336. //==============================================================================
  337. template <typename T = ElementType>
  338. TriviallyCopyableVoid<T> removeElementsInternal (int indexToRemoveAt, int numElementsToRemove)
  339. {
  340. auto* start = elements + indexToRemoveAt;
  341. auto numElementsToShift = numUsed - (indexToRemoveAt + numElementsToRemove);
  342. memmove (start, start + numElementsToRemove, (size_t) numElementsToShift * sizeof (ElementType));
  343. }
  344. template <typename T = ElementType>
  345. NonTriviallyCopyableVoid<T> removeElementsInternal (int indexToRemoveAt, int numElementsToRemove)
  346. {
  347. auto numElementsToShift = numUsed - (indexToRemoveAt + numElementsToRemove);
  348. auto* destination = elements + indexToRemoveAt;
  349. auto* source = destination + numElementsToRemove;
  350. for (int i = 0; i < numElementsToShift; ++i)
  351. moveAssignElement (destination++, std::move (*(source++)));
  352. for (int i = 0; i < numElementsToRemove; ++i)
  353. (destination++)->~ElementType();
  354. }
  355. //==============================================================================
  356. template <typename T = ElementType>
  357. TriviallyCopyableVoid<T> moveInternal (int currentIndex, int newIndex) noexcept
  358. {
  359. char tempCopy[sizeof (ElementType)];
  360. memcpy (tempCopy, elements + currentIndex, sizeof (ElementType));
  361. if (newIndex > currentIndex)
  362. {
  363. memmove (elements + currentIndex,
  364. elements + currentIndex + 1,
  365. sizeof (ElementType) * (size_t) (newIndex - currentIndex));
  366. }
  367. else
  368. {
  369. memmove (elements + newIndex + 1,
  370. elements + newIndex,
  371. sizeof (ElementType) * (size_t) (currentIndex - newIndex));
  372. }
  373. memcpy (elements + newIndex, tempCopy, sizeof (ElementType));
  374. }
  375. template <typename T = ElementType>
  376. NonTriviallyCopyableVoid<T> moveInternal (int currentIndex, int newIndex) noexcept
  377. {
  378. auto* e = elements + currentIndex;
  379. ElementType tempCopy (std::move (*e));
  380. auto delta = newIndex - currentIndex;
  381. if (delta > 0)
  382. {
  383. for (int i = 0; i < delta; ++i)
  384. {
  385. moveAssignElement (e, std::move (*(e + 1)));
  386. ++e;
  387. }
  388. }
  389. else
  390. {
  391. for (int i = 0; i < -delta; ++i)
  392. {
  393. moveAssignElement (e, std::move (*(e - 1)));
  394. --e;
  395. }
  396. }
  397. moveAssignElement (e, std::move (tempCopy));
  398. }
  399. //==============================================================================
  400. void addAssumingCapacityIsReady (const ElementType& element) { new (elements + numUsed++) ElementType (element); }
  401. void addAssumingCapacityIsReady (ElementType&& element) { new (elements + numUsed++) ElementType (std::move (element)); }
  402. template <typename... OtherElements>
  403. void addAssumingCapacityIsReady (const ElementType& firstNewElement, OtherElements... otherElements)
  404. {
  405. addAssumingCapacityIsReady (firstNewElement);
  406. addAssumingCapacityIsReady (otherElements...);
  407. }
  408. template <typename... OtherElements>
  409. void addAssumingCapacityIsReady (ElementType&& firstNewElement, OtherElements... otherElements)
  410. {
  411. addAssumingCapacityIsReady (std::move (firstNewElement));
  412. addAssumingCapacityIsReady (otherElements...);
  413. }
  414. //==============================================================================
  415. template <typename T = ElementType>
  416. typename std::enable_if<std::is_move_assignable<T>::value, void>::type
  417. moveAssignElement (ElementType* destination, ElementType&& source)
  418. {
  419. *destination = std::move (source);
  420. }
  421. template <typename T = ElementType>
  422. typename std::enable_if<! std::is_move_assignable<T>::value, void>::type
  423. moveAssignElement (ElementType* destination, ElementType&& source)
  424. {
  425. destination->~ElementType();
  426. new (destination) ElementType (std::move (source));
  427. }
  428. void checkSourceIsNotAMember (const ElementType* element)
  429. {
  430. // when you pass a reference to an existing element into a method like add() which
  431. // may need to reallocate the array to make more space, the incoming reference may
  432. // be deleted indirectly during the reallocation operation! To work around this,
  433. // make a local copy of the item you're trying to add (and maybe use std::move to
  434. // move it into the add() method to avoid any extra overhead)
  435. jassert (element < begin() || element >= end());
  436. ignoreUnused (element);
  437. }
  438. //==============================================================================
  439. HeapBlock<ElementType> elements;
  440. int numAllocated = 0, numUsed = 0;
  441. JUCE_DECLARE_NON_COPYABLE (ArrayBase)
  442. };
  443. } // namespace juce