26#if ! (defined (DOXYGEN) || JUCE_EXCEPTIONS_DISABLED)
27namespace HeapBlockHelper
29 template <
bool shouldThrow>
30 struct ThrowOnFail {
static void checkPointer (
void*) {} };
33 struct ThrowOnFail<true> {
static void checkPointer (
void* data) {
if (data ==
nullptr)
throw std::bad_alloc(); } };
85template <
class ElementType,
bool throwOnFailure = false>
89 template <
class OtherElementType>
90 using AllowConversion =
typename std::enable_if<std::is_base_of<typename std::remove_pointer<ElementType>::type,
91 typename std::remove_pointer<OtherElementType>::type>::value>::type;
110 template <
typename SizeType>
114 throwOnAllocationFailure();
122 template <
typename SizeType>
128 throwOnAllocationFailure();
157 template <
class OtherElementType,
bool otherThrowOnFailure,
typename = AllowConversion<OtherElementType>>
159 : data (
reinterpret_cast<ElementType*
> (
other.
data))
168 template <
class OtherElementType,
bool otherThrowOnFailure,
typename = AllowConversion<OtherElementType>>
172 data =
reinterpret_cast<ElementType*
> (
other.
data);
182 inline operator ElementType*()
const noexcept {
return data; }
200 inline operator void*()
const noexcept {
return static_cast<void*
> (data); }
206 inline operator const void*()
const noexcept {
return static_cast<const void*
> (data); }
218 template <
typename IndexType>
224 template <
typename IndexType>
251 template <
typename SizeType>
256 throwOnAllocationFailure();
262 template <
typename SizeType>
267 throwOnAllocationFailure();
274 template <
typename SizeType>
279 ? std::calloc (
static_cast<size_t> (
newNumElements),
sizeof (ElementType))
280 : std::malloc (
static_cast<size_t> (
newNumElements) *
sizeof (ElementType)));
281 throwOnAllocationFailure();
289 template <
typename SizeType>
294 throwOnAllocationFailure();
309 template <
bool otherBlockThrows>
319 template <
typename SizeType>
322 zeromem (data,
sizeof (ElementType) *
static_cast<size_t> (
numElements));
330 ElementType* data =
nullptr;
332 void throwOnAllocationFailure()
const
334 #if JUCE_EXCEPTIONS_DISABLED
335 jassert (data !=
nullptr);
337 HeapBlockHelper::ThrowOnFail<throwOnFailure>::checkPointer (data);
341 template <
class OtherElementType,
bool otherThrowOnFailure>
344 #if ! (defined (JUCE_DLL) || defined (JUCE_DLL_BUILD))
346 JUCE_PREVENT_HEAP_ALLOCATION
ElementType * data() noexcept
HeapBlock(HeapBlock< OtherElementType, otherThrowOnFailure > &&other) noexcept
HeapBlock & operator=(HeapBlock &&other) noexcept
void clear(SizeType numElements) noexcept
void swapWith(HeapBlock< ElementType, otherBlockThrows > &other) noexcept
bool operator!=(const ElementType *otherPointer) const noexcept
HeapBlock(SizeType numElements, bool initialiseToZero)
ElementType * operator+(IndexType index) const noexcept
void malloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
HeapBlock(HeapBlock &&other) noexcept
ElementType * get() const noexcept
ElementType * getData() const noexcept
ElementType & operator[](IndexType index) const noexcept
void allocate(SizeType newNumElements, bool initialiseToZero)
HeapBlock(SizeType numElements)
bool operator==(const ElementType *otherPointer) const noexcept
void realloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
ElementType * operator->() const noexcept
void calloc(SizeType newNumElements, const size_t elementSize=sizeof(ElementType))