30#if ! (defined (DOXYGEN) || JUCE_EXCEPTIONS_DISABLED)
31namespace HeapBlockHelper
33 template <
bool shouldThrow>
34 struct ThrowOnFail {
static void checkPointer (
void*) {} };
37 struct ThrowOnFail<
true> {
static void checkPointer (
void* data) {
if (data ==
nullptr)
throw std::bad_alloc(); } };
89template <
class ElementType,
bool throwOnFailure = false>
93 template <
class OtherElementType>
94 using AllowConversion =
typename std::enable_if<std::is_base_of<typename std::remove_pointer<ElementType>::type,
95 typename std::remove_pointer<OtherElementType>::type>::value>::type;
114 template <
typename SizeType>
118 throwOnAllocationFailure();
126 template <
typename SizeType>
132 throwOnAllocationFailure();
161 template <
class OtherElementType,
bool otherThrowOnFailure,
typename = AllowConversion<OtherElementType>>
172 template <
class OtherElementType,
bool otherThrowOnFailure,
typename = AllowConversion<OtherElementType>>
204 inline operator void*()
const noexcept {
return static_cast<void*
> (data); }
210 inline operator const void*()
const noexcept {
return static_cast<const void*
> (data); }
222 template <
typename IndexType>
228 template <
typename IndexType>
255 template <
typename SizeType>
260 throwOnAllocationFailure();
266 template <
typename SizeType>
271 throwOnAllocationFailure();
278 template <
typename SizeType>
285 throwOnAllocationFailure();
293 template <
typename SizeType>
298 throwOnAllocationFailure();
313 template <
bool otherBlockThrows>
323 template <
typename SizeType>
336 void throwOnAllocationFailure()
const
338 #if JUCE_EXCEPTIONS_DISABLED
339 jassert (data !=
nullptr);
341 HeapBlockHelper::ThrowOnFail<throwOnFailure>::checkPointer (data);
345 template <
class OtherElementType,
bool otherThrowOnFailure>
348 #if ! (defined (JUCE_DLL) || defined (JUCE_DLL_BUILD))
350 JUCE_PREVENT_HEAP_ALLOCATION
Holds a resizable array of primitive or copy-by-value objects.
Array()=default
Creates an empty array.
ElementType * data() const noexcept
Returns a pointer to the first element in the array.
Very simple container class to hold a pointer to some data on the heap.
HeapBlock(HeapBlock< OtherElementType, otherThrowOnFailure > &&other) noexcept
Converting move constructor.
HeapBlock & operator=(HeapBlock &&other) noexcept
Move assignment operator.
void clear(SizeType numElements) noexcept
This fills the block with zeros, up to the number of elements specified.
void swapWith(HeapBlock< ElementType, otherBlockThrows > &other) noexcept
Swaps this object's data with the data of another HeapBlock.
bool operator!=(const ElementType *otherPointer) const noexcept
Compares the pointer with another pointer.
HeapBlock(SizeType numElements, bool initialiseToZero)
Creates a HeapBlock containing a number of elements.
ElementType * operator+(IndexType index) const noexcept
Returns a pointer to a data element at an offset from the start of the array.
void malloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
Allocates a specified amount of memory.
HeapBlock(HeapBlock &&other) noexcept
Move constructor.
ElementType * get() const noexcept
Returns a raw pointer to the allocated data.
ElementType * getData() const noexcept
Returns a raw pointer to the allocated data.
ElementType & operator[](IndexType index) const noexcept
Returns a reference to one of the data elements.
HeapBlock()=default
Creates a HeapBlock which is initially just a null pointer.
void allocate(SizeType newNumElements, bool initialiseToZero)
Allocates a specified amount of memory and optionally clears it.
HeapBlock(SizeType numElements)
Creates a HeapBlock containing a number of elements.
bool operator==(const ElementType *otherPointer) const noexcept
Compares the pointer with another pointer.
void realloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
Re-allocates a specified amount of memory.
void free() noexcept
Frees any currently-allocated data.
ElementType * operator->() const noexcept
Lets you use indirect calls to the first element in the array.
void calloc(SizeType newNumElements, const size_t elementSize=sizeof(ElementType))
Allocates a specified amount of memory and clears it.