VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_core / containers / juce_ArrayAllocationBase.h
blob51de01fbddaf8c5f1786c4eb14659467412e6a0d
1 /*
2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
20 ==============================================================================
23 namespace juce
26 //==============================================================================
27 /**
28 Implements some basic array storage allocation functions.
30 This class isn't really for public use - it used to be part of the
31 container classes but has since been superseded by ArrayBase. Eventually
32 it will be removed from the API.
34 @tags{Core}
36 template <class ElementType, class TypeOfCriticalSectionToUse>
37 class ArrayAllocationBase : public TypeOfCriticalSectionToUse
39 public:
40 //==============================================================================
41 /** Creates an empty array. */
42 ArrayAllocationBase() = default;
44 /** Destructor. */
45 ~ArrayAllocationBase() = default;
47 ArrayAllocationBase (ArrayAllocationBase&& other) noexcept
48 : elements (std::move (other.elements)),
49 numAllocated (other.numAllocated)
53 ArrayAllocationBase& operator= (ArrayAllocationBase&& other) noexcept
55 elements = std::move (other.elements);
56 numAllocated = other.numAllocated;
57 return *this;
60 //==============================================================================
61 /** Changes the amount of storage allocated.
63 This will retain any data currently held in the array, and either add or
64 remove extra space at the end.
66 @param numElements the number of elements that are needed
68 void setAllocatedSize (int numElements)
70 if (numAllocated != numElements)
72 if (numElements > 0)
73 elements.realloc ((size_t) numElements);
74 else
75 elements.free();
77 numAllocated = numElements;
81 /** Increases the amount of storage allocated if it is less than a given amount.
83 This will retain any data currently held in the array, but will add
84 extra space at the end to make sure there it's at least as big as the size
85 passed in. If it's already bigger, no action is taken.
87 @param minNumElements the minimum number of elements that are needed
89 void ensureAllocatedSize (int minNumElements)
91 if (minNumElements > numAllocated)
92 setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
94 jassert (numAllocated <= 0 || elements != nullptr);
97 /** Minimises the amount of storage allocated so that it's no more than
98 the given number of elements.
100 void shrinkToNoMoreThan (int maxNumElements)
102 if (maxNumElements < numAllocated)
103 setAllocatedSize (maxNumElements);
106 /** Swap the contents of two objects. */
107 void swapWith (ArrayAllocationBase& other) noexcept
109 elements.swapWith (other.elements);
110 std::swap (numAllocated, other.numAllocated);
113 //==============================================================================
114 HeapBlock<ElementType> elements;
115 int numAllocated = 0;
117 private:
118 JUCE_DECLARE_NON_COPYABLE (ArrayAllocationBase)
121 } // namespace juce