адаптация s13bby/osc под vst и gui. (вайбкод)
вайбкод потому что мне в моменте стало понятно. что проще было сразу писать на фреймворке JUCE, чем адаптировать консольное приложение на MiniAudio. обновляться данный репозиторий не будет. СmakeLists адаптирован онли под linux.
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
#if JUCE_ENABLE_ALLOCATION_HOOKS
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
static AllocationHooks& getAllocationHooksForThread()
|
||||
{
|
||||
thread_local AllocationHooks hooks;
|
||||
return hooks;
|
||||
}
|
||||
|
||||
void notifyAllocationHooksForThread()
|
||||
{
|
||||
getAllocationHooksForThread().listenerList.call ([] (AllocationHooks::Listener& l)
|
||||
{
|
||||
l.newOrDeleteCalled();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void* operator new (size_t s)
|
||||
{
|
||||
juce::notifyAllocationHooksForThread();
|
||||
return std::malloc (s);
|
||||
}
|
||||
|
||||
void* operator new[] (size_t s)
|
||||
{
|
||||
juce::notifyAllocationHooksForThread();
|
||||
return std::malloc (s);
|
||||
}
|
||||
|
||||
void operator delete (void* p) noexcept
|
||||
{
|
||||
juce::notifyAllocationHooksForThread();
|
||||
std::free (p);
|
||||
}
|
||||
|
||||
void operator delete[] (void* p) noexcept
|
||||
{
|
||||
juce::notifyAllocationHooksForThread();
|
||||
std::free (p);
|
||||
}
|
||||
|
||||
void operator delete (void* p, size_t) noexcept
|
||||
{
|
||||
juce::notifyAllocationHooksForThread();
|
||||
std::free (p);
|
||||
}
|
||||
|
||||
void operator delete[] (void* p, size_t) noexcept
|
||||
{
|
||||
juce::notifyAllocationHooksForThread();
|
||||
std::free (p);
|
||||
}
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
UnitTestAllocationChecker::UnitTestAllocationChecker (UnitTest& test)
|
||||
: unitTest (test)
|
||||
{
|
||||
getAllocationHooksForThread().addListener (this);
|
||||
}
|
||||
|
||||
UnitTestAllocationChecker::~UnitTestAllocationChecker() noexcept
|
||||
{
|
||||
getAllocationHooksForThread().removeListener (this);
|
||||
unitTest.expectEquals ((int) calls, 0, "new or delete was incorrectly called while allocation checker was active");
|
||||
}
|
||||
|
||||
void UnitTestAllocationChecker::newOrDeleteCalled() noexcept { ++calls; }
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
#if JUCE_ENABLE_ALLOCATION_HOOKS
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class AllocationHooks
|
||||
{
|
||||
public:
|
||||
struct Listener
|
||||
{
|
||||
virtual ~Listener() noexcept = default;
|
||||
virtual void newOrDeleteCalled() noexcept = 0;
|
||||
};
|
||||
|
||||
void addListener (Listener* l) { listenerList.add (l); }
|
||||
void removeListener (Listener* l) noexcept { listenerList.remove (l); }
|
||||
|
||||
private:
|
||||
friend void notifyAllocationHooksForThread();
|
||||
LightweightListenerList<Listener> listenerList;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/** Scoped checker which will cause a unit test failure if any new/delete calls
|
||||
are made during the lifetime of the UnitTestAllocationChecker.
|
||||
*/
|
||||
class UnitTestAllocationChecker : private AllocationHooks::Listener
|
||||
{
|
||||
public:
|
||||
/** Create a checker which will log a failure to the passed test if
|
||||
any calls to new/delete are made.
|
||||
|
||||
Remember to call `UnitTest::beginTest` before constructing this checker!
|
||||
*/
|
||||
explicit UnitTestAllocationChecker (UnitTest& test);
|
||||
|
||||
/** Will add a failure to the test if the number of new/delete calls during
|
||||
this object's lifetime was greater than zero.
|
||||
*/
|
||||
~UnitTestAllocationChecker() noexcept override;
|
||||
|
||||
private:
|
||||
void newOrDeleteCalled() noexcept override;
|
||||
|
||||
UnitTest& unitTest;
|
||||
size_t calls = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
/** @cond */
|
||||
namespace AtomicHelpers
|
||||
{
|
||||
template <typename T> struct DiffTypeHelper { using Type = T; };
|
||||
template <typename T> struct DiffTypeHelper<T*> { using Type = std::ptrdiff_t; };
|
||||
}
|
||||
/** @endcond */
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A simple wrapper around std::atomic.
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
template <typename Type>
|
||||
struct Atomic final
|
||||
{
|
||||
using DiffType = typename AtomicHelpers::DiffTypeHelper<Type>::Type;
|
||||
|
||||
/** Creates a new value, initialised to zero. */
|
||||
Atomic() noexcept : value (Type()) {}
|
||||
|
||||
/** Creates a new value, with a given initial value. */
|
||||
Atomic (Type initialValue) noexcept : value (initialValue) {}
|
||||
|
||||
/** Copies another value (atomically). */
|
||||
Atomic (const Atomic& other) noexcept : value (other.get()) {}
|
||||
|
||||
/** Destructor. */
|
||||
~Atomic() noexcept
|
||||
{
|
||||
#if __cpp_lib_atomic_is_always_lock_free
|
||||
static_assert (std::atomic<Type>::is_always_lock_free,
|
||||
"This class can only be used for lock-free types");
|
||||
#endif
|
||||
}
|
||||
|
||||
/** Atomically reads and returns the current value. */
|
||||
Type get() const noexcept { return value.load(); }
|
||||
|
||||
/** Atomically sets the current value. */
|
||||
void set (Type newValue) noexcept { value = newValue; }
|
||||
|
||||
/** Atomically sets the current value, returning the value that was replaced. */
|
||||
Type exchange (Type newValue) noexcept { return value.exchange (newValue); }
|
||||
|
||||
/** Atomically compares this value with a target value, and if it is equal, sets
|
||||
this to be equal to a new value.
|
||||
|
||||
This operation is the atomic equivalent of doing this:
|
||||
@code
|
||||
bool compareAndSetBool (Type newValue, Type valueToCompare)
|
||||
{
|
||||
if (get() == valueToCompare)
|
||||
{
|
||||
set (newValue);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@endcode
|
||||
|
||||
Internally, this method calls std::atomic::compare_exchange_strong with
|
||||
memory_order_seq_cst (the strictest std::memory_order).
|
||||
|
||||
@returns true if the comparison was true and the value was replaced; false if
|
||||
the comparison failed and the value was left unchanged.
|
||||
@see compareAndSetValue
|
||||
*/
|
||||
bool compareAndSetBool (Type newValue, Type valueToCompare) noexcept
|
||||
{
|
||||
return value.compare_exchange_strong (valueToCompare, newValue);
|
||||
}
|
||||
|
||||
/** Copies another value into this one (atomically). */
|
||||
Atomic<Type>& operator= (const Atomic& other) noexcept
|
||||
{
|
||||
value = other.value.load();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Copies another value into this one (atomically). */
|
||||
Atomic<Type>& operator= (Type newValue) noexcept
|
||||
{
|
||||
value = newValue;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Atomically adds a number to this value, returning the new value. */
|
||||
Type operator+= (DiffType amountToAdd) noexcept { return value += amountToAdd; }
|
||||
|
||||
/** Atomically subtracts a number from this value, returning the new value. */
|
||||
Type operator-= (DiffType amountToSubtract) noexcept { return value -= amountToSubtract; }
|
||||
|
||||
/** Atomically increments this value, returning the new value. */
|
||||
Type operator++() noexcept { return ++value; }
|
||||
|
||||
/** Atomically decrements this value, returning the new value. */
|
||||
Type operator--() noexcept { return --value; }
|
||||
|
||||
/** Implements a memory read/write barrier.
|
||||
|
||||
Internally this calls std::atomic_thread_fence with
|
||||
memory_order_seq_cst (the strictest std::memory_order).
|
||||
*/
|
||||
void memoryBarrier() noexcept { atomic_thread_fence (std::memory_order_seq_cst); }
|
||||
|
||||
/** The std::atomic object that this class operates on. */
|
||||
std::atomic<Type> value;
|
||||
|
||||
//==============================================================================
|
||||
/** @cond */
|
||||
[[deprecated ("This method has been deprecated as there is no equivalent method in "
|
||||
"std::atomic. Use compareAndSetBool instead.")]]
|
||||
Type compareAndSetValue (Type, Type) noexcept;
|
||||
/** @endcond */
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,232 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/** Contains static methods for converting the byte order between different
|
||||
endiannesses.
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
class JUCE_API ByteOrder
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Swaps the upper and lower bytes of a 16-bit integer. */
|
||||
constexpr static uint16 swap (uint16 value) noexcept;
|
||||
|
||||
/** Swaps the upper and lower bytes of a 16-bit integer. */
|
||||
constexpr static int16 swap (int16 value) noexcept;
|
||||
|
||||
/** Reverses the order of the 4 bytes in a 32-bit integer. */
|
||||
static uint32 swap (uint32 value) noexcept;
|
||||
|
||||
/** Reverses the order of the 4 bytes in a 32-bit integer. */
|
||||
static int32 swap (int32 value) noexcept;
|
||||
|
||||
/** Reverses the order of the 8 bytes in a 64-bit integer. */
|
||||
static uint64 swap (uint64 value) noexcept;
|
||||
|
||||
/** Reverses the order of the 8 bytes in a 64-bit integer. */
|
||||
static int64 swap (int64 value) noexcept;
|
||||
|
||||
/** Returns a garbled float which has the reverse byte-order of the original. */
|
||||
static float swap (float value) noexcept;
|
||||
|
||||
/** Returns a garbled double which has the reverse byte-order of the original. */
|
||||
static double swap (double value) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Swaps the byte order of a signed or unsigned integer if the CPU is big-endian */
|
||||
template <typename Type>
|
||||
static Type swapIfBigEndian (Type value) noexcept
|
||||
{
|
||||
#if JUCE_LITTLE_ENDIAN
|
||||
return value;
|
||||
#else
|
||||
return swap (value);
|
||||
#endif
|
||||
}
|
||||
|
||||
/** Swaps the byte order of a signed or unsigned integer if the CPU is little-endian */
|
||||
template <typename Type>
|
||||
static Type swapIfLittleEndian (Type value) noexcept
|
||||
{
|
||||
#if JUCE_LITTLE_ENDIAN
|
||||
return swap (value);
|
||||
#else
|
||||
return value;
|
||||
#endif
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
/** Turns 4 bytes into a little-endian integer. */
|
||||
constexpr static uint32 littleEndianInt (const void* bytes) noexcept;
|
||||
|
||||
/** Turns 8 bytes into a little-endian integer. */
|
||||
constexpr static uint64 littleEndianInt64 (const void* bytes) noexcept;
|
||||
|
||||
/** Turns 2 bytes into a little-endian integer. */
|
||||
constexpr static uint16 littleEndianShort (const void* bytes) noexcept;
|
||||
|
||||
/** Converts 3 little-endian bytes into a signed 24-bit value (which is sign-extended to 32 bits). */
|
||||
constexpr static int littleEndian24Bit (const void* bytes) noexcept;
|
||||
|
||||
/** Copies a 24-bit number to 3 little-endian bytes. */
|
||||
static void littleEndian24BitToChars (int32 value, void* destBytes) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Turns 4 bytes into a big-endian integer. */
|
||||
constexpr static uint32 bigEndianInt (const void* bytes) noexcept;
|
||||
|
||||
/** Turns 8 bytes into a big-endian integer. */
|
||||
constexpr static uint64 bigEndianInt64 (const void* bytes) noexcept;
|
||||
|
||||
/** Turns 2 bytes into a big-endian integer. */
|
||||
constexpr static uint16 bigEndianShort (const void* bytes) noexcept;
|
||||
|
||||
/** Converts 3 big-endian bytes into a signed 24-bit value (which is sign-extended to 32 bits). */
|
||||
constexpr static int bigEndian24Bit (const void* bytes) noexcept;
|
||||
|
||||
/** Copies a 24-bit number to 3 big-endian bytes. */
|
||||
static void bigEndian24BitToChars (int32 value, void* destBytes) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Constructs a 16-bit integer from its constituent bytes, in order of significance. */
|
||||
constexpr static uint16 makeInt (uint8 leastSig, uint8 mostSig) noexcept;
|
||||
|
||||
/** Constructs a 32-bit integer from its constituent bytes, in order of significance. */
|
||||
constexpr static uint32 makeInt (uint8 leastSig, uint8 byte1, uint8 byte2, uint8 mostSig) noexcept;
|
||||
|
||||
/** Constructs a 64-bit integer from its constituent bytes, in order of significance. */
|
||||
constexpr static uint64 makeInt (uint8 leastSig, uint8 byte1, uint8 byte2, uint8 byte3,
|
||||
uint8 byte4, uint8 byte5, uint8 byte6, uint8 mostSig) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Returns true if the current CPU is big-endian. */
|
||||
constexpr static bool isBigEndian() noexcept
|
||||
{
|
||||
#if JUCE_LITTLE_ENDIAN
|
||||
return false;
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
ByteOrder() = delete;
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
constexpr inline uint16 ByteOrder::swap (uint16 v) noexcept { return static_cast<uint16> ((v << 8) | (v >> 8)); }
|
||||
constexpr inline int16 ByteOrder::swap (int16 v) noexcept { return static_cast<int16> (swap (static_cast<uint16> (v))); }
|
||||
inline int32 ByteOrder::swap (int32 v) noexcept { return static_cast<int32> (swap (static_cast<uint32> (v))); }
|
||||
inline int64 ByteOrder::swap (int64 v) noexcept { return static_cast<int64> (swap (static_cast<uint64> (v))); }
|
||||
inline float ByteOrder::swap (float v) noexcept { union { uint32 asUInt; float asFloat; } n; n.asFloat = v; n.asUInt = swap (n.asUInt); return n.asFloat; }
|
||||
inline double ByteOrder::swap (double v) noexcept { union { uint64 asUInt; double asFloat; } n; n.asFloat = v; n.asUInt = swap (n.asUInt); return n.asFloat; }
|
||||
|
||||
#if JUCE_MSVC && ! defined (__INTEL_COMPILER)
|
||||
#pragma intrinsic (_byteswap_ulong)
|
||||
#endif
|
||||
|
||||
inline uint32 ByteOrder::swap (uint32 n) noexcept
|
||||
{
|
||||
#if JUCE_MAC || JUCE_IOS
|
||||
return OSSwapInt32 (n);
|
||||
#elif (JUCE_GCC || JUCE_CLANG) && JUCE_INTEL && ! JUCE_NO_INLINE_ASM
|
||||
asm("bswap %%eax" : "=a"(n) : "a"(n));
|
||||
return n;
|
||||
#elif JUCE_MSVC
|
||||
return _byteswap_ulong (n);
|
||||
#elif JUCE_ANDROID
|
||||
return bswap_32 (n);
|
||||
#else
|
||||
return (n << 24) | (n >> 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline uint64 ByteOrder::swap (uint64 value) noexcept
|
||||
{
|
||||
#if JUCE_MAC || JUCE_IOS
|
||||
return OSSwapInt64 (value);
|
||||
#elif JUCE_MSVC
|
||||
return _byteswap_uint64 (value);
|
||||
#else
|
||||
return (((uint64) swap ((uint32) value)) << 32) | swap ((uint32) (value >> 32));
|
||||
#endif
|
||||
}
|
||||
|
||||
constexpr inline uint16 ByteOrder::makeInt (uint8 b0, uint8 b1) noexcept
|
||||
{
|
||||
return static_cast<uint16> (static_cast<uint16> (b0) | (static_cast<uint16> (b1) << 8));
|
||||
}
|
||||
|
||||
constexpr inline uint32 ByteOrder::makeInt (uint8 b0, uint8 b1, uint8 b2, uint8 b3) noexcept
|
||||
{
|
||||
return static_cast<uint32> (b0) | (static_cast<uint32> (b1) << 8)
|
||||
| (static_cast<uint32> (b2) << 16) | (static_cast<uint32> (b3) << 24);
|
||||
}
|
||||
|
||||
constexpr inline uint64 ByteOrder::makeInt (uint8 b0, uint8 b1, uint8 b2, uint8 b3, uint8 b4, uint8 b5, uint8 b6, uint8 b7) noexcept
|
||||
{
|
||||
return static_cast<uint64> (b0) | (static_cast<uint64> (b1) << 8) | (static_cast<uint64> (b2) << 16) | (static_cast<uint64> (b3) << 24)
|
||||
| (static_cast<uint64> (b4) << 32) | (static_cast<uint64> (b5) << 40) | (static_cast<uint64> (b6) << 48) | (static_cast<uint64> (b7) << 56);
|
||||
}
|
||||
|
||||
constexpr inline uint16 ByteOrder::littleEndianShort (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[0], static_cast<const uint8*> (bytes)[1]); }
|
||||
constexpr inline uint32 ByteOrder::littleEndianInt (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[0], static_cast<const uint8*> (bytes)[1],
|
||||
static_cast<const uint8*> (bytes)[2], static_cast<const uint8*> (bytes)[3]); }
|
||||
constexpr inline uint64 ByteOrder::littleEndianInt64 (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[0], static_cast<const uint8*> (bytes)[1],
|
||||
static_cast<const uint8*> (bytes)[2], static_cast<const uint8*> (bytes)[3],
|
||||
static_cast<const uint8*> (bytes)[4], static_cast<const uint8*> (bytes)[5],
|
||||
static_cast<const uint8*> (bytes)[6], static_cast<const uint8*> (bytes)[7]); }
|
||||
|
||||
constexpr inline uint16 ByteOrder::bigEndianShort (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[1], static_cast<const uint8*> (bytes)[0]); }
|
||||
constexpr inline uint32 ByteOrder::bigEndianInt (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[3], static_cast<const uint8*> (bytes)[2],
|
||||
static_cast<const uint8*> (bytes)[1], static_cast<const uint8*> (bytes)[0]); }
|
||||
constexpr inline uint64 ByteOrder::bigEndianInt64 (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[7], static_cast<const uint8*> (bytes)[6],
|
||||
static_cast<const uint8*> (bytes)[5], static_cast<const uint8*> (bytes)[4],
|
||||
static_cast<const uint8*> (bytes)[3], static_cast<const uint8*> (bytes)[2],
|
||||
static_cast<const uint8*> (bytes)[1], static_cast<const uint8*> (bytes)[0]); }
|
||||
|
||||
constexpr inline int32 ByteOrder::littleEndian24Bit (const void* bytes) noexcept { return (int32) ((((uint32) static_cast<const int8*> (bytes)[2]) << 16) | (((uint32) static_cast<const uint8*> (bytes)[1]) << 8) | ((uint32) static_cast<const uint8*> (bytes)[0])); }
|
||||
constexpr inline int32 ByteOrder::bigEndian24Bit (const void* bytes) noexcept { return (int32) ((((uint32) static_cast<const int8*> (bytes)[0]) << 16) | (((uint32) static_cast<const uint8*> (bytes)[1]) << 8) | ((uint32) static_cast<const uint8*> (bytes)[2])); }
|
||||
|
||||
inline void ByteOrder::littleEndian24BitToChars (int32 value, void* destBytes) noexcept { static_cast<uint8*> (destBytes)[0] = (uint8) value; static_cast<uint8*> (destBytes)[1] = (uint8) (value >> 8); static_cast<uint8*> (destBytes)[2] = (uint8) (value >> 16); }
|
||||
inline void ByteOrder::bigEndian24BitToChars (int32 value, void* destBytes) noexcept { static_cast<uint8*> (destBytes)[0] = (uint8) (value >> 16); static_cast<uint8*> (destBytes)[1] = (uint8) (value >> 8); static_cast<uint8*> (destBytes)[2] = (uint8) value; }
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Used by container classes as an indirect way to delete an object of a
|
||||
particular type.
|
||||
|
||||
The generic implementation of this class simply calls 'delete', but you can
|
||||
create a specialised version of it for a particular class if you need to
|
||||
delete that type of object in a more appropriate way.
|
||||
|
||||
@see OwnedArray
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
template <typename ObjectType>
|
||||
struct ContainerDeletePolicy
|
||||
{
|
||||
static void destroy (ObjectType* object)
|
||||
{
|
||||
// If the line below triggers a compiler error, it means that you are using
|
||||
// an incomplete type for ObjectType (for example, a type that is declared
|
||||
// but not defined). This is a problem because then the following delete is
|
||||
// undefined behaviour. The purpose of the sizeof is to capture this situation.
|
||||
// If this was caused by a OwnedArray of a forward-declared type, move the
|
||||
// implementation of all methods trying to use the OwnedArray (e.g. the destructor
|
||||
// of the class owning it) into cpp files where they can see to the definition
|
||||
// of ObjectType. This should fix the error.
|
||||
[[maybe_unused]] constexpr auto size = sizeof (ObjectType);
|
||||
|
||||
delete object;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
/** Wraps a HeapBlock, but additionally provides a copy constructor and remembers its size.
|
||||
|
||||
This class is not intended for use as a general-purpose container - in most cases, a std::vector
|
||||
or juce::Array is a better choice. However, CopyableHeapBlock may be handy for optimisation in
|
||||
situations where the container elements don't need to be value-initialised and the container
|
||||
doesn't need to be resized.
|
||||
|
||||
@see Array, OwnedArray, MemoryBlock, HeapBlock
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
template <typename ElementType, bool throwOnFailure = false>
|
||||
class CopyableHeapBlock
|
||||
{
|
||||
public:
|
||||
static_assert (std::is_trivially_copyable_v<ElementType>);
|
||||
|
||||
/** Creates an empty block. */
|
||||
CopyableHeapBlock() = default;
|
||||
|
||||
/** Creates a block large enough to hold the specified number of elements.
|
||||
|
||||
The contents of the block are unspecified, and *must* be written before they are first read.
|
||||
*/
|
||||
explicit CopyableHeapBlock (size_t numElementsIn)
|
||||
: block (numElementsIn),
|
||||
numElements (numElementsIn)
|
||||
{
|
||||
}
|
||||
|
||||
/** Moves the contents of another block into this one. */
|
||||
CopyableHeapBlock (CopyableHeapBlock&& other) noexcept
|
||||
: block (std::exchange (other.block, {})),
|
||||
numElements (std::exchange (other.numElements, {}))
|
||||
{
|
||||
}
|
||||
|
||||
/** Copies the contents of another block into this one. */
|
||||
CopyableHeapBlock (const CopyableHeapBlock& other)
|
||||
: block (other.numElements),
|
||||
numElements (other.numElements)
|
||||
{
|
||||
std::copy (other.block.getData(), other.block.getData() + other.numElements, block.getData());
|
||||
}
|
||||
|
||||
/** Moves the contents of another block into this one. */
|
||||
CopyableHeapBlock& operator= (CopyableHeapBlock&& other) noexcept
|
||||
{
|
||||
CopyableHeapBlock { std::move (other) }.swap (*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Copies the contents of another block into this one. */
|
||||
CopyableHeapBlock& operator= (const CopyableHeapBlock& other) noexcept
|
||||
{
|
||||
CopyableHeapBlock { other }.swap (*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Returns a pointer to the first element in the block. */
|
||||
ElementType* data()
|
||||
{
|
||||
return block.getData();
|
||||
}
|
||||
|
||||
/** Returns a pointer to the first element in the block. */
|
||||
const ElementType* data() const
|
||||
{
|
||||
return block.getData();
|
||||
}
|
||||
|
||||
/** Returns the number of elements in the block. */
|
||||
size_t size() const
|
||||
{
|
||||
return numElements;
|
||||
}
|
||||
|
||||
/** Returns true if the size of the block is zero. */
|
||||
bool isEmpty() const
|
||||
{
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/** Returns a pointer to the first element in the block. */
|
||||
ElementType* begin()
|
||||
{
|
||||
return data();
|
||||
}
|
||||
|
||||
/** Returns a pointer to the first element in the block. */
|
||||
const ElementType* begin() const
|
||||
{
|
||||
return data();
|
||||
}
|
||||
|
||||
/** Returns a pointer to the first element in the block. */
|
||||
const ElementType* cbegin() const
|
||||
{
|
||||
return data();
|
||||
}
|
||||
|
||||
/** Returns a pointer immediately after the last element in the block. */
|
||||
ElementType* end()
|
||||
{
|
||||
return data() + size();
|
||||
}
|
||||
|
||||
/** Returns a pointer immediately after the last element in the block. */
|
||||
const ElementType* end() const
|
||||
{
|
||||
return data() + size();
|
||||
}
|
||||
|
||||
/** Returns a pointer immediately after the last element in the block. */
|
||||
const ElementType* cend() const
|
||||
{
|
||||
return data() + size();
|
||||
}
|
||||
|
||||
/** Returns a reference to the element at the provided index. */
|
||||
ElementType& operator[] (size_t index)
|
||||
{
|
||||
return data()[index];
|
||||
}
|
||||
|
||||
/** Returns a reference to the element at the provided index. */
|
||||
const ElementType& operator[] (size_t index) const
|
||||
{
|
||||
return data()[index];
|
||||
}
|
||||
|
||||
private:
|
||||
void swap (CopyableHeapBlock& other) noexcept
|
||||
{
|
||||
std::swap (other.block, block);
|
||||
std::swap (other.numElements, numElements);
|
||||
}
|
||||
|
||||
HeapBlock<ElementType, throwOnFailure> block;
|
||||
size_t numElements{};
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,381 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
#if ! JUCE_EXCEPTIONS_DISABLED
|
||||
/** @cond */
|
||||
namespace HeapBlockHelper
|
||||
{
|
||||
template <bool shouldThrow>
|
||||
struct ThrowOnFail { static void checkPointer (void*) {} };
|
||||
|
||||
template <>
|
||||
struct ThrowOnFail<true> { static void checkPointer (void* data) { if (data == nullptr) throw std::bad_alloc(); } };
|
||||
}
|
||||
/** @endcond */
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Very simple container class to hold a pointer to some data on the heap.
|
||||
|
||||
When you need to allocate some heap storage for something, always try to use
|
||||
this class instead of allocating the memory directly using malloc/free.
|
||||
|
||||
A HeapBlock<char> object can be treated in pretty much exactly the same way
|
||||
as an char*, but as long as you allocate it on the stack or as a class member,
|
||||
it's almost impossible for it to leak memory.
|
||||
|
||||
It also makes your code much more concise and readable than doing the same thing
|
||||
using direct allocations,
|
||||
|
||||
E.g. instead of this:
|
||||
@code
|
||||
int* temp = (int*) malloc (1024 * sizeof (int));
|
||||
memcpy (temp, xyz, 1024 * sizeof (int));
|
||||
free (temp);
|
||||
temp = (int*) calloc (2048 * sizeof (int));
|
||||
temp[0] = 1234;
|
||||
memcpy (foobar, temp, 2048 * sizeof (int));
|
||||
free (temp);
|
||||
@endcode
|
||||
|
||||
...you could just write this:
|
||||
@code
|
||||
HeapBlock<int> temp (1024);
|
||||
memcpy (temp, xyz, 1024 * sizeof (int));
|
||||
temp.calloc (2048);
|
||||
temp[0] = 1234;
|
||||
memcpy (foobar, temp, 2048 * sizeof (int));
|
||||
@endcode
|
||||
|
||||
The class is extremely lightweight, containing only a pointer to the
|
||||
data, and exposes malloc/realloc/calloc/free methods that do the same jobs
|
||||
as their less object-oriented counterparts. Despite adding safety, you probably
|
||||
won't sacrifice any performance by using this in place of normal pointers.
|
||||
|
||||
The throwOnFailure template parameter can be set to true if you'd like the class
|
||||
to throw a std::bad_alloc exception when an allocation fails. If this is false,
|
||||
then a failed allocation will just leave the heapblock with a null pointer (assuming
|
||||
that the system's malloc() function doesn't throw).
|
||||
|
||||
@see Array, OwnedArray, MemoryBlock
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
template <class ElementType, bool throwOnFailure = false>
|
||||
class HeapBlock
|
||||
{
|
||||
private:
|
||||
template <class OtherElementType>
|
||||
using AllowConversion = std::enable_if_t<std::is_base_of_v<std::remove_pointer_t<ElementType>,
|
||||
std::remove_pointer_t<OtherElementType>>>;
|
||||
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates a HeapBlock which is initially just a null pointer.
|
||||
|
||||
After creation, you can resize the array using the malloc(), calloc(),
|
||||
or realloc() methods.
|
||||
*/
|
||||
HeapBlock() noexcept = default;
|
||||
|
||||
/** Creates a HeapBlock containing a number of elements.
|
||||
|
||||
The contents of the block are undefined, as it will have been created by a
|
||||
malloc call.
|
||||
|
||||
If you want an array of zero values, you can use the calloc() method or the
|
||||
other constructor that takes an InitialisationState parameter.
|
||||
*/
|
||||
template <typename SizeType, std::enable_if_t<std::is_convertible_v<SizeType, int>, int> = 0>
|
||||
explicit HeapBlock (SizeType numElements)
|
||||
: data (mallocWrapper (static_cast<size_t> (numElements) * sizeof (ElementType)))
|
||||
{
|
||||
}
|
||||
|
||||
/** Creates a HeapBlock containing a number of elements.
|
||||
|
||||
The initialiseToZero parameter determines whether the new memory should be cleared,
|
||||
or left uninitialised.
|
||||
*/
|
||||
template <typename SizeType, std::enable_if_t<std::is_convertible_v<SizeType, int>, int> = 0>
|
||||
HeapBlock (SizeType numElements, bool initialiseToZero)
|
||||
: data (initialiseToZero ? callocWrapper (static_cast<size_t> (numElements), sizeof (ElementType))
|
||||
: mallocWrapper (static_cast<size_t> (numElements) * sizeof (ElementType)))
|
||||
{
|
||||
}
|
||||
|
||||
/** Destructor.
|
||||
This will free the data, if any has been allocated.
|
||||
*/
|
||||
~HeapBlock()
|
||||
{
|
||||
std::free (data);
|
||||
}
|
||||
|
||||
/** Move constructor */
|
||||
HeapBlock (HeapBlock&& other) noexcept
|
||||
: data (other.data)
|
||||
{
|
||||
other.data = nullptr;
|
||||
}
|
||||
|
||||
/** Move assignment operator */
|
||||
HeapBlock& operator= (HeapBlock&& other) noexcept
|
||||
{
|
||||
std::swap (data, other.data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Converting move constructor.
|
||||
Only enabled if this is a HeapBlock<Base*> and the other object is a HeapBlock<Derived*>,
|
||||
where std::is_base_of_v<Base, Derived> == true.
|
||||
*/
|
||||
template <class OtherElementType, bool otherThrowOnFailure, typename = AllowConversion<OtherElementType>>
|
||||
HeapBlock (HeapBlock<OtherElementType, otherThrowOnFailure>&& other) noexcept
|
||||
: data (reinterpret_cast<ElementType*> (other.data))
|
||||
{
|
||||
other.data = nullptr;
|
||||
}
|
||||
|
||||
/** Converting move assignment operator.
|
||||
Only enabled if this is a HeapBlock<Base*> and the other object is a HeapBlock<Derived*>,
|
||||
where std::is_base_of_v<Base, Derived> == true.
|
||||
*/
|
||||
template <class OtherElementType, bool otherThrowOnFailure, typename = AllowConversion<OtherElementType>>
|
||||
HeapBlock& operator= (HeapBlock<OtherElementType, otherThrowOnFailure>&& other) noexcept
|
||||
{
|
||||
free();
|
||||
data = reinterpret_cast<ElementType*> (other.data);
|
||||
other.data = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
/** Returns a raw pointer to the allocated data.
|
||||
This may be a null pointer if the data hasn't yet been allocated, or if it has been
|
||||
freed by calling the free() method.
|
||||
*/
|
||||
inline operator ElementType*() const noexcept { return data; }
|
||||
|
||||
/** Returns a raw pointer to the allocated data.
|
||||
This may be a null pointer if the data hasn't yet been allocated, or if it has been
|
||||
freed by calling the free() method.
|
||||
*/
|
||||
inline ElementType* get() const noexcept { return data; }
|
||||
|
||||
/** Returns a raw pointer to the allocated data.
|
||||
This may be a null pointer if the data hasn't yet been allocated, or if it has been
|
||||
freed by calling the free() method.
|
||||
*/
|
||||
inline ElementType* getData() const noexcept { return data; }
|
||||
|
||||
/** Returns a void pointer to the allocated data.
|
||||
This may be a null pointer if the data hasn't yet been allocated, or if it has been
|
||||
freed by calling the free() method.
|
||||
*/
|
||||
inline operator void*() const noexcept { return static_cast<void*> (data); }
|
||||
|
||||
/** Returns a void pointer to the allocated data.
|
||||
This may be a null pointer if the data hasn't yet been allocated, or if it has been
|
||||
freed by calling the free() method.
|
||||
*/
|
||||
inline operator const void*() const noexcept { return static_cast<const void*> (data); }
|
||||
|
||||
/** Lets you use indirect calls to the first element in the array.
|
||||
Obviously this will cause problems if the array hasn't been initialised, because it'll
|
||||
be referencing a null pointer.
|
||||
*/
|
||||
inline ElementType* operator->() const noexcept { return data; }
|
||||
|
||||
/** Returns a reference to one of the data elements.
|
||||
Obviously there's no bounds-checking here, as this object is just a dumb pointer and
|
||||
has no idea of the size it currently has allocated.
|
||||
*/
|
||||
template <typename IndexType>
|
||||
ElementType& operator[] (IndexType index) const noexcept { return data [index]; }
|
||||
|
||||
/** Returns a pointer to a data element at an offset from the start of the array.
|
||||
This is the same as doing pointer arithmetic on the raw pointer itself.
|
||||
*/
|
||||
template <typename IndexType>
|
||||
ElementType* operator+ (IndexType index) const noexcept { return data + index; }
|
||||
|
||||
//==============================================================================
|
||||
/** Compares the pointer with another pointer.
|
||||
This can be handy for checking whether this is a null pointer.
|
||||
*/
|
||||
inline bool operator== (const ElementType* otherPointer) const noexcept { return otherPointer == data; }
|
||||
|
||||
/** Compares the pointer with another pointer.
|
||||
This can be handy for checking whether this is a null pointer.
|
||||
*/
|
||||
inline bool operator!= (const ElementType* otherPointer) const noexcept { return otherPointer != data; }
|
||||
|
||||
//==============================================================================
|
||||
/** Allocates a specified amount of memory.
|
||||
|
||||
This uses the normal malloc to allocate an amount of memory for this object.
|
||||
Any previously allocated memory will be freed by this method.
|
||||
|
||||
The number of bytes allocated will be (newNumElements * elementSize). Normally
|
||||
you wouldn't need to specify the second parameter, but it can be handy if you need
|
||||
to allocate a size in bytes rather than in terms of the number of elements.
|
||||
|
||||
The data that is allocated will be freed when this object is deleted, or when you
|
||||
call free() or any of the allocation methods.
|
||||
*/
|
||||
template <typename SizeType>
|
||||
void malloc (SizeType newNumElements, size_t elementSize = sizeof (ElementType))
|
||||
{
|
||||
std::free (data);
|
||||
data = mallocWrapper (static_cast<size_t> (newNumElements) * elementSize);
|
||||
}
|
||||
|
||||
/** Allocates a specified amount of memory and clears it.
|
||||
This does the same job as the malloc() method, but clears the memory that it allocates.
|
||||
*/
|
||||
template <typename SizeType>
|
||||
void calloc (SizeType newNumElements, const size_t elementSize = sizeof (ElementType))
|
||||
{
|
||||
std::free (data);
|
||||
data = callocWrapper (static_cast<size_t> (newNumElements), elementSize);
|
||||
}
|
||||
|
||||
/** Allocates a specified amount of memory and optionally clears it.
|
||||
This does the same job as either malloc() or calloc(), depending on the
|
||||
initialiseToZero parameter.
|
||||
*/
|
||||
template <typename SizeType>
|
||||
void allocate (SizeType newNumElements, bool initialiseToZero)
|
||||
{
|
||||
std::free (data);
|
||||
data = initialiseToZero ? callocWrapper (static_cast<size_t> (newNumElements), sizeof (ElementType))
|
||||
: mallocWrapper (static_cast<size_t> (newNumElements) * sizeof (ElementType));
|
||||
}
|
||||
|
||||
/** Re-allocates a specified amount of memory.
|
||||
|
||||
The semantics of this method are the same as malloc() and calloc(), but it
|
||||
uses realloc() to keep as much of the existing data as possible.
|
||||
*/
|
||||
template <typename SizeType>
|
||||
void realloc (SizeType newNumElements, size_t elementSize = sizeof (ElementType))
|
||||
{
|
||||
data = reallocWrapper (data, static_cast<size_t> (newNumElements) * elementSize);
|
||||
}
|
||||
|
||||
/** Frees any currently-allocated data.
|
||||
This will free the data and reset this object to be a null pointer.
|
||||
*/
|
||||
void free() noexcept
|
||||
{
|
||||
std::free (data);
|
||||
data = nullptr;
|
||||
}
|
||||
|
||||
/** Swaps this object's data with the data of another HeapBlock.
|
||||
The two objects simply exchange their data pointers.
|
||||
*/
|
||||
template <bool otherBlockThrows>
|
||||
void swapWith (HeapBlock<ElementType, otherBlockThrows>& other) noexcept
|
||||
{
|
||||
std::swap (data, other.data);
|
||||
}
|
||||
|
||||
/** This fills the block with zeros, up to the number of elements specified.
|
||||
Since the block has no way of knowing its own size, you must make sure that the number of
|
||||
elements you specify doesn't exceed the allocated size.
|
||||
*/
|
||||
template <typename SizeType>
|
||||
void clear (SizeType numElements) noexcept
|
||||
{
|
||||
zeromem (data, sizeof (ElementType) * static_cast<size_t> (numElements));
|
||||
}
|
||||
|
||||
/** This typedef can be used to get the type of the heapblock's elements. */
|
||||
using Type = ElementType;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
// Calls to malloc, calloc and realloc with zero size have implementation-defined
|
||||
// behaviour where either nullptr or a non-null pointer is returned.
|
||||
template <typename Functor>
|
||||
static ElementType* wrapper (size_t size, Functor&& f)
|
||||
{
|
||||
if (size == 0)
|
||||
return nullptr;
|
||||
|
||||
auto* memory = static_cast<ElementType*> (f());
|
||||
|
||||
#if JUCE_EXCEPTIONS_DISABLED
|
||||
jassert (memory != nullptr); // without exceptions, you'll need to find a better way to handle this failure case
|
||||
#else
|
||||
HeapBlockHelper::ThrowOnFail<throwOnFailure>::checkPointer (memory);
|
||||
#endif
|
||||
|
||||
return memory;
|
||||
}
|
||||
|
||||
static ElementType* mallocWrapper (size_t size)
|
||||
{
|
||||
return wrapper (size, [size] { return std::malloc (size); });
|
||||
}
|
||||
|
||||
static ElementType* callocWrapper (size_t num, size_t size)
|
||||
{
|
||||
return wrapper (num * size, [num, size] { return std::calloc (num, size); });
|
||||
}
|
||||
|
||||
static ElementType* reallocWrapper (void* ptr, size_t newSize)
|
||||
{
|
||||
return wrapper (newSize, [ptr, newSize] { return std::realloc (ptr, newSize); });
|
||||
}
|
||||
|
||||
template <class OtherElementType, bool otherThrowOnFailure>
|
||||
friend class HeapBlock;
|
||||
|
||||
//==============================================================================
|
||||
ElementType* data = nullptr;
|
||||
|
||||
#if ! (defined (JUCE_DLL) || defined (JUCE_DLL_BUILD))
|
||||
JUCE_DECLARE_NON_COPYABLE (HeapBlock)
|
||||
JUCE_PREVENT_HEAP_ALLOCATION // Creating a 'new HeapBlock' would be missing the point!
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
This class is a useful way of tracking down hard to find memory leaks when the
|
||||
regular LeakedObjectDetector isn't enough.
|
||||
|
||||
As well as firing when any instances of the OwnerClass type are leaked, it will
|
||||
print out a stack trace showing where the leaked object was created. This is obviously
|
||||
quite a heavyweight task so, unlike the LeakedObjectDetector which should be always
|
||||
be added to your classes, you should only use this object temporarily when you are
|
||||
debugging and remove it when finished.
|
||||
|
||||
To use it, use the JUCE_HEAVYWEIGHT_LEAK_DETECTOR macro as a simple way to put
|
||||
one in your class declaration.
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
template <class OwnerClass>
|
||||
class HeavyweightLeakedObjectDetector
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
HeavyweightLeakedObjectDetector() noexcept { getBacktraceMap()[this] = SystemStats::getStackBacktrace(); }
|
||||
HeavyweightLeakedObjectDetector (const HeavyweightLeakedObjectDetector&) noexcept { getBacktraceMap()[this] = SystemStats::getStackBacktrace(); }
|
||||
|
||||
~HeavyweightLeakedObjectDetector() { getBacktraceMap().erase (this); }
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
typedef std::map<HeavyweightLeakedObjectDetector<OwnerClass>*, String> BacktraceMap;
|
||||
|
||||
//==============================================================================
|
||||
struct BacktraceMapHolder
|
||||
{
|
||||
BacktraceMapHolder() = default;
|
||||
|
||||
~BacktraceMapHolder()
|
||||
{
|
||||
if (map.size() > 0)
|
||||
{
|
||||
DBG ("*** Leaked objects detected: " << map.size() << " instance(s) of class " << getLeakedObjectClassName());
|
||||
DBG (getFormattedBacktracesString());
|
||||
|
||||
/** If you hit this, then you've leaked one or more objects of the type specified by
|
||||
the 'OwnerClass' template parameter - the name and stack trace of its creation should
|
||||
have been printed by the lines above.
|
||||
|
||||
Many errors like this are caused by using old-fashioned, non-RAII techniques for
|
||||
object management. If you're seeing this, it's a good idea to double-check
|
||||
that you're using std::unique_ptrs, OwnedArrays,
|
||||
ReferenceCountedObjects, etc, and avoiding manual calls to the 'delete'
|
||||
operator at all costs!
|
||||
|
||||
You may also see this error if JUCE's shutdown code is called while an object
|
||||
containing a leak detector is still alive (perhaps declared as a namespace-scope
|
||||
static).
|
||||
*/
|
||||
jassertfalse;
|
||||
}
|
||||
}
|
||||
|
||||
String getFormattedBacktracesString() const
|
||||
{
|
||||
String str;
|
||||
|
||||
int counter = 1;
|
||||
for (auto& bt : map)
|
||||
{
|
||||
str << "\nBacktrace " << String (counter++) << "\n"
|
||||
<< "-----------------------------------------------------------------" << "\n"
|
||||
<< bt.second;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
BacktraceMap map;
|
||||
};
|
||||
|
||||
static BacktraceMap& getBacktraceMap()
|
||||
{
|
||||
static BacktraceMapHolder holder;
|
||||
return holder.map;
|
||||
}
|
||||
|
||||
static const char* getLeakedObjectClassName()
|
||||
{
|
||||
return OwnerClass::getLeakedObjectClassName();
|
||||
}
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
#if DOXYGEN || ! defined (JUCE_HEAVYWEIGHT_LEAK_DETECTOR)
|
||||
#if (DOXYGEN || JUCE_CHECK_MEMORY_LEAKS)
|
||||
/** This macro lets you embed a heavyweight leak-detecting object inside a class.
|
||||
|
||||
To use it, simply declare a JUCE_HEAVYWEIGHT_LEAK_DETECTOR (YourClassName) inside a private section
|
||||
of the class declaration. E.g.
|
||||
|
||||
@code
|
||||
class MyClass
|
||||
{
|
||||
public:
|
||||
MyClass();
|
||||
void blahBlah();
|
||||
|
||||
private:
|
||||
JUCE_HEAVYWEIGHT_LEAK_DETECTOR (MyClass)
|
||||
};
|
||||
@endcode
|
||||
|
||||
NB: you should only use this when you really need to track down a tricky memory leak, and
|
||||
should never leave one of these inside a class!
|
||||
|
||||
@see HeavyweightLeakedObjectDetector, JUCE_LEAK_DETECTOR, LeakedObjectDetector
|
||||
*/
|
||||
#define JUCE_HEAVYWEIGHT_LEAK_DETECTOR(OwnerClass) \
|
||||
friend class juce::HeavyweightLeakedObjectDetector<OwnerClass>; \
|
||||
static const char* getLeakedObjectClassName() noexcept { return #OwnerClass; } \
|
||||
juce::HeavyweightLeakedObjectDetector<OwnerClass> JUCE_JOIN_MACRO (leakDetector, __LINE__);
|
||||
#else
|
||||
#define JUCE_HEAVYWEIGHT_LEAK_DETECTOR(OwnerClass)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Embedding an instance of this class inside another class can be used as a low-overhead
|
||||
way of detecting leaked instances.
|
||||
|
||||
This class keeps an internal static count of the number of instances that are
|
||||
active, so that when the app is shutdown and the static destructors are called,
|
||||
it can check whether there are any left-over instances that may have been leaked.
|
||||
|
||||
To use it, use the JUCE_LEAK_DETECTOR macro as a simple way to put one in your
|
||||
class declaration. Have a look through the juce codebase for examples, it's used
|
||||
in most of the classes.
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
template <class OwnerClass>
|
||||
class LeakedObjectDetector
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
LeakedObjectDetector() noexcept { ++(getCounter().numObjects); }
|
||||
LeakedObjectDetector (const LeakedObjectDetector&) noexcept { ++(getCounter().numObjects); }
|
||||
|
||||
LeakedObjectDetector& operator= (const LeakedObjectDetector&) noexcept = default;
|
||||
|
||||
~LeakedObjectDetector()
|
||||
{
|
||||
if (--(getCounter().numObjects) < 0)
|
||||
{
|
||||
DBG ("*** Dangling pointer deletion! Class: " << getLeakedObjectClassName());
|
||||
|
||||
/** If you hit this, then you've managed to delete more instances of this class than you've
|
||||
created. That indicates that you're deleting some dangling pointers.
|
||||
|
||||
Note that although this assertion will have been triggered during a destructor, it might
|
||||
not be this particular deletion that's at fault - the incorrect one may have happened
|
||||
at an earlier point in the program, and simply not been detected until now.
|
||||
|
||||
Many errors like this are caused by using old-fashioned, non-RAII techniques for
|
||||
object management. If you're seeing this, it's a good idea to double-check
|
||||
that you're using std::unique_ptrs, OwnedArrays,
|
||||
ReferenceCountedObjects, etc, and avoiding manual calls to the 'delete'
|
||||
operator at all costs!
|
||||
|
||||
You may also see this error if JUCE's shutdown code is called while an object
|
||||
containing a leak detector is still alive (perhaps declared as a namespace-scope
|
||||
static).
|
||||
*/
|
||||
jassertfalse;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
class LeakCounter
|
||||
{
|
||||
public:
|
||||
LeakCounter() = default;
|
||||
|
||||
~LeakCounter()
|
||||
{
|
||||
if (numObjects.value > 0)
|
||||
{
|
||||
DBG ("*** Leaked objects detected: " << numObjects.value << " instance(s) of class " << getLeakedObjectClassName());
|
||||
|
||||
/** If you hit this, then you've leaked one or more objects of the type specified by
|
||||
the 'OwnerClass' template parameter - the name should have been printed by the line above.
|
||||
|
||||
Many errors like this are caused by using old-fashioned, non-RAII techniques for
|
||||
object management. If you're seeing this, it's a good idea to double-check
|
||||
that you're using std::unique_ptrs, OwnedArrays,
|
||||
ReferenceCountedObjects, etc, and avoiding manual calls to the 'delete'
|
||||
operator at all costs!
|
||||
|
||||
You may also see this error if JUCE's shutdown code is called while an object
|
||||
containing a leak detector is still alive (perhaps declared as a namespace-scope
|
||||
static).
|
||||
*/
|
||||
jassertfalse;
|
||||
}
|
||||
}
|
||||
|
||||
Atomic<int> numObjects;
|
||||
};
|
||||
|
||||
static const char* getLeakedObjectClassName()
|
||||
{
|
||||
return OwnerClass::getLeakedObjectClassName();
|
||||
}
|
||||
|
||||
static LeakCounter& getCounter() noexcept
|
||||
{
|
||||
static LeakCounter counter;
|
||||
return counter;
|
||||
}
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
#if DOXYGEN || ! defined (JUCE_LEAK_DETECTOR)
|
||||
#if (DOXYGEN || JUCE_CHECK_MEMORY_LEAKS)
|
||||
/** This macro lets you embed a leak-detecting object inside a class.
|
||||
|
||||
To use it, simply declare a JUCE_LEAK_DETECTOR (YourClassName) inside a private section
|
||||
of the class declaration. E.g.
|
||||
|
||||
@code
|
||||
class MyClass
|
||||
{
|
||||
public:
|
||||
MyClass();
|
||||
void blahBlah();
|
||||
|
||||
private:
|
||||
JUCE_LEAK_DETECTOR (MyClass)
|
||||
};
|
||||
@endcode
|
||||
|
||||
@see JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR, LeakedObjectDetector
|
||||
*/
|
||||
#define JUCE_LEAK_DETECTOR(OwnerClass) \
|
||||
friend class juce::LeakedObjectDetector<OwnerClass>; \
|
||||
static const char* getLeakedObjectClassName() noexcept { return #OwnerClass; } \
|
||||
juce::LeakedObjectDetector<OwnerClass> JUCE_JOIN_MACRO (leakDetector, __LINE__);
|
||||
#else
|
||||
#define JUCE_LEAK_DETECTOR(OwnerClass)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/** Fills a block of memory with zeros. */
|
||||
inline void zeromem (void* memory, size_t numBytes) noexcept
|
||||
{
|
||||
if (memory != nullptr)
|
||||
memset (memory, 0, numBytes);
|
||||
else
|
||||
jassert (numBytes == 0);
|
||||
}
|
||||
|
||||
/** Overwrites a structure or object with zeros. */
|
||||
template <typename Type>
|
||||
inline void zerostruct (Type& structure) noexcept { memset ((void*) &structure, 0, sizeof (structure)); }
|
||||
|
||||
/** Delete an object pointer, and sets the pointer to null.
|
||||
|
||||
Remember that it's not good c++ practice to use delete directly - always try to use a std::unique_ptr
|
||||
or other automatic lifetime-management system rather than resorting to deleting raw pointers!
|
||||
*/
|
||||
template <typename Type>
|
||||
inline void deleteAndZero (Type& pointer) { delete pointer; pointer = nullptr; }
|
||||
|
||||
/** A handy function to round up a pointer to the nearest multiple of a given number of bytes.
|
||||
alignmentBytes must be a power of two. */
|
||||
template <typename Type, typename IntegerType>
|
||||
inline Type* snapPointerToAlignment (Type* basePointer, IntegerType alignmentBytes) noexcept
|
||||
{
|
||||
return (Type*) ((((size_t) basePointer) + (alignmentBytes - 1)) & ~(alignmentBytes - 1));
|
||||
}
|
||||
|
||||
/** A handy function which returns the difference between any two pointers, in bytes.
|
||||
The address of the second pointer is subtracted from the first, and the difference in bytes is returned.
|
||||
*/
|
||||
template <typename Type1, typename Type2>
|
||||
inline int getAddressDifference (Type1* pointer1, Type2* pointer2) noexcept { return (int) (((const char*) pointer1) - (const char*) pointer2); }
|
||||
|
||||
/** If a pointer is non-null, this returns a new copy of the object that it points to, or safely returns
|
||||
nullptr if the pointer is null.
|
||||
*/
|
||||
template <class Type>
|
||||
inline Type* createCopyIfNotNull (const Type* objectToCopy) { return objectToCopy != nullptr ? new Type (*objectToCopy) : nullptr; }
|
||||
|
||||
//==============================================================================
|
||||
/** A handy function to read un-aligned memory without a performance penalty or bus-error. */
|
||||
template <typename Type>
|
||||
inline Type readUnaligned (const void* srcPtr) noexcept
|
||||
{
|
||||
Type value;
|
||||
memcpy (&value, srcPtr, sizeof (Type));
|
||||
return value;
|
||||
}
|
||||
|
||||
/** A handy function to write un-aligned memory without a performance penalty or bus-error. */
|
||||
template <typename Type>
|
||||
inline void writeUnaligned (void* dstPtr, Type value) noexcept
|
||||
{
|
||||
memcpy (dstPtr, &value, sizeof (Type));
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
/** Casts a pointer to another type via `void*`, which suppresses the cast-align
|
||||
warning which sometimes arises when casting pointers to types with different
|
||||
alignment.
|
||||
You should only use this when you know for a fact that the input pointer points
|
||||
to a region that has suitable alignment for `Type`, e.g. regions returned from
|
||||
malloc/calloc that should be suitable for any non-over-aligned type.
|
||||
*/
|
||||
template <typename Type>
|
||||
inline Type unalignedPointerCast (void* ptr) noexcept
|
||||
{
|
||||
static_assert (std::is_pointer_v<Type>);
|
||||
return reinterpret_cast<Type> (ptr);
|
||||
}
|
||||
|
||||
/** Casts a pointer to another type via `void*`, which suppresses the cast-align
|
||||
warning which sometimes arises when casting pointers to types with different
|
||||
alignment.
|
||||
You should only use this when you know for a fact that the input pointer points
|
||||
to a region that has suitable alignment for `Type`, e.g. regions returned from
|
||||
malloc/calloc that should be suitable for any non-over-aligned type.
|
||||
*/
|
||||
template <typename Type>
|
||||
inline Type unalignedPointerCast (const void* ptr) noexcept
|
||||
{
|
||||
static_assert (std::is_pointer_v<Type>);
|
||||
return reinterpret_cast<Type> (ptr);
|
||||
}
|
||||
|
||||
/** A handy function which adds a number of bytes to any type of pointer and returns the result.
|
||||
This can be useful to avoid casting pointers to a char* and back when you want to move them by
|
||||
a specific number of bytes,
|
||||
*/
|
||||
template <typename Type, typename IntegerType>
|
||||
inline Type* addBytesToPointer (Type* basePointer, IntegerType bytes) noexcept
|
||||
{
|
||||
return unalignedPointerCast<Type*> (reinterpret_cast<char*> (basePointer) + bytes);
|
||||
}
|
||||
|
||||
/** A handy function which adds a number of bytes to any type of pointer and returns the result.
|
||||
This can be useful to avoid casting pointers to a char* and back when you want to move them by
|
||||
a specific number of bytes,
|
||||
*/
|
||||
template <typename Type, typename IntegerType>
|
||||
inline const Type* addBytesToPointer (const Type* basePointer, IntegerType bytes) noexcept
|
||||
{
|
||||
return unalignedPointerCast<const Type*> (reinterpret_cast<const char*> (basePointer) + bytes);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
#if JUCE_MAC || JUCE_IOS || DOXYGEN
|
||||
|
||||
/** A handy C++ wrapper that creates and deletes an NSAutoreleasePool object using RAII.
|
||||
You should use the JUCE_AUTORELEASEPOOL macro to create a local auto-release pool on the stack.
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
class JUCE_API ScopedAutoReleasePool
|
||||
{
|
||||
public:
|
||||
ScopedAutoReleasePool();
|
||||
~ScopedAutoReleasePool();
|
||||
|
||||
private:
|
||||
void* pool;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (ScopedAutoReleasePool)
|
||||
};
|
||||
|
||||
/** A macro that can be used to easily declare a local ScopedAutoReleasePool
|
||||
object for RAII-based obj-C autoreleasing.
|
||||
Because this may use the \@autoreleasepool syntax, you must follow the macro with
|
||||
a set of braces to mark the scope of the pool.
|
||||
*/
|
||||
#if (JUCE_COMPILER_SUPPORTS_ARC && defined (__OBJC__)) || DOXYGEN
|
||||
#define JUCE_AUTORELEASEPOOL @autoreleasepool
|
||||
#else
|
||||
#define JUCE_AUTORELEASEPOOL const juce::ScopedAutoReleasePool JUCE_JOIN_MACRO (autoReleasePool_, __LINE__);
|
||||
#endif
|
||||
|
||||
#else
|
||||
#define JUCE_AUTORELEASEPOOL
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
/* In a Windows DLL build, we'll expose some malloc/free functions that live inside the DLL, and use these for
|
||||
allocating all the objects - that way all juce objects in the DLL and in the host will live in the same heap,
|
||||
avoiding problems when an object is created in one module and passed across to another where it is deleted.
|
||||
By piggy-backing on the JUCE_LEAK_DETECTOR macro, these allocators can be injected into most juce classes.
|
||||
*/
|
||||
#if JUCE_MSVC && (defined (JUCE_DLL) || defined (JUCE_DLL_BUILD)) && ! JUCE_DISABLE_DLL_ALLOCATORS
|
||||
/** @cond */
|
||||
extern JUCE_API void* juceDLL_malloc (size_t);
|
||||
extern JUCE_API void juceDLL_free (void*);
|
||||
|
||||
#define JUCE_LEAK_DETECTOR(OwnerClass) public:\
|
||||
static void* operator new (size_t sz) { return juce::juceDLL_malloc (sz); } \
|
||||
static void* operator new (size_t, void* p) { return p; } \
|
||||
static void operator delete (void* p) { juce::juceDLL_free (p); } \
|
||||
static void operator delete (void*, void*) {}
|
||||
/** @endcond */
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
/** (Deprecated) This was a Windows-specific way of checking for object leaks - now please
|
||||
use the JUCE_LEAK_DETECTOR instead.
|
||||
*/
|
||||
#ifndef juce_UseDebuggingNewOperator
|
||||
#define juce_UseDebuggingNewOperator
|
||||
#endif
|
||||
|
||||
/** Converts an owning raw pointer into a unique_ptr, deriving the
|
||||
type of the unique_ptr automatically.
|
||||
|
||||
This should only be used with pointers to single objects.
|
||||
Do NOT pass a pointer to an array to this function, as the
|
||||
destructor of the unique_ptr will incorrectly call `delete`
|
||||
instead of `delete[]` on the pointer.
|
||||
*/
|
||||
template <typename T>
|
||||
std::unique_ptr<T> rawToUniquePtr (T* ptr)
|
||||
{
|
||||
return std::unique_ptr<T> (ptr);
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,485 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
MemoryBlock::MemoryBlock() noexcept {}
|
||||
|
||||
MemoryBlock::MemoryBlock (size_t initialSize, bool initialiseToZero)
|
||||
{
|
||||
if (initialSize > 0)
|
||||
{
|
||||
size = initialSize;
|
||||
data.allocate (initialSize, initialiseToZero);
|
||||
}
|
||||
else
|
||||
{
|
||||
size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
MemoryBlock::MemoryBlock (const MemoryBlock& other)
|
||||
: size (other.size)
|
||||
{
|
||||
if (size > 0)
|
||||
{
|
||||
jassert (other.data != nullptr);
|
||||
data.malloc (size);
|
||||
memcpy (data, other.data, size);
|
||||
}
|
||||
}
|
||||
|
||||
MemoryBlock::MemoryBlock (const void* const dataToInitialiseFrom, const size_t sizeInBytes)
|
||||
: size (sizeInBytes)
|
||||
{
|
||||
jassert (((ssize_t) sizeInBytes) >= 0);
|
||||
|
||||
if (size > 0)
|
||||
{
|
||||
jassert (dataToInitialiseFrom != nullptr); // non-zero size, but a zero pointer passed-in?
|
||||
|
||||
data.malloc (size);
|
||||
|
||||
if (dataToInitialiseFrom != nullptr)
|
||||
memcpy (data, dataToInitialiseFrom, size);
|
||||
}
|
||||
}
|
||||
|
||||
MemoryBlock::~MemoryBlock() noexcept
|
||||
{
|
||||
}
|
||||
|
||||
MemoryBlock& MemoryBlock::operator= (const MemoryBlock& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
setSize (other.size, false);
|
||||
|
||||
if (size > 0)
|
||||
memcpy (data, other.data, size);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
MemoryBlock::MemoryBlock (MemoryBlock&& other) noexcept
|
||||
: data (std::move (other.data)),
|
||||
size (std::exchange (other.size, 0))
|
||||
{
|
||||
}
|
||||
|
||||
MemoryBlock& MemoryBlock::operator= (MemoryBlock&& other) noexcept
|
||||
{
|
||||
MemoryBlock tmp { std::move (other) };
|
||||
swapWith (tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool MemoryBlock::operator== (const MemoryBlock& other) const noexcept
|
||||
{
|
||||
return matches (other.data, other.size);
|
||||
}
|
||||
|
||||
bool MemoryBlock::operator!= (const MemoryBlock& other) const noexcept
|
||||
{
|
||||
return ! operator== (other);
|
||||
}
|
||||
|
||||
bool MemoryBlock::matches (const void* dataToCompare, size_t dataSize) const noexcept
|
||||
{
|
||||
jassert (dataToCompare != nullptr || dataSize == 0);
|
||||
|
||||
if (size != dataSize)
|
||||
return false;
|
||||
|
||||
if (size == 0)
|
||||
return true;
|
||||
|
||||
if (dataToCompare == nullptr)
|
||||
return false;
|
||||
|
||||
return memcmp (data, dataToCompare, size) == 0;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
// this will resize the block to this size
|
||||
void MemoryBlock::setSize (const size_t newSize, const bool initialiseToZero)
|
||||
{
|
||||
if (size != newSize)
|
||||
{
|
||||
if (newSize <= 0)
|
||||
{
|
||||
reset();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (data != nullptr)
|
||||
{
|
||||
data.realloc (newSize);
|
||||
|
||||
if (initialiseToZero && (newSize > size))
|
||||
zeromem (data + size, newSize - size);
|
||||
}
|
||||
else
|
||||
{
|
||||
data.allocate (newSize, initialiseToZero);
|
||||
}
|
||||
|
||||
size = newSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MemoryBlock::reset()
|
||||
{
|
||||
data.free();
|
||||
size = 0;
|
||||
}
|
||||
|
||||
void MemoryBlock::ensureSize (size_t minimumSize, bool initialiseToZero)
|
||||
{
|
||||
if (size < minimumSize)
|
||||
setSize (minimumSize, initialiseToZero);
|
||||
}
|
||||
|
||||
void MemoryBlock::swapWith (MemoryBlock& other) noexcept
|
||||
{
|
||||
std::swap (size, other.size);
|
||||
data.swapWith (other.data);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void MemoryBlock::fillWith (uint8 value) noexcept
|
||||
{
|
||||
if (data != nullptr)
|
||||
memset (data, (int) value, size);
|
||||
}
|
||||
|
||||
void MemoryBlock::append (const void* srcData, size_t numBytes)
|
||||
{
|
||||
if (numBytes > 0)
|
||||
{
|
||||
jassert (srcData != nullptr); // this must not be null!
|
||||
auto oldSize = size;
|
||||
setSize (size + numBytes);
|
||||
memcpy (data + oldSize, srcData, numBytes);
|
||||
}
|
||||
}
|
||||
|
||||
void MemoryBlock::replaceAll (const void* srcData, size_t numBytes)
|
||||
{
|
||||
if (numBytes <= 0)
|
||||
{
|
||||
reset();
|
||||
return;
|
||||
}
|
||||
|
||||
jassert (srcData != nullptr); // this must not be null!
|
||||
setSize (numBytes);
|
||||
memcpy (data, srcData, numBytes);
|
||||
}
|
||||
|
||||
void MemoryBlock::insert (const void* srcData, size_t numBytes, size_t insertPosition)
|
||||
{
|
||||
if (numBytes > 0)
|
||||
{
|
||||
jassert (srcData != nullptr); // this must not be null!
|
||||
insertPosition = jmin (size, insertPosition);
|
||||
auto trailingDataSize = size - insertPosition;
|
||||
setSize (size + numBytes, false);
|
||||
|
||||
if (trailingDataSize > 0)
|
||||
memmove (data + insertPosition + numBytes,
|
||||
data + insertPosition,
|
||||
trailingDataSize);
|
||||
|
||||
memcpy (data + insertPosition, srcData, numBytes);
|
||||
}
|
||||
}
|
||||
|
||||
void MemoryBlock::removeSection (size_t startByte, size_t numBytesToRemove)
|
||||
{
|
||||
if (startByte + numBytesToRemove >= size)
|
||||
{
|
||||
setSize (startByte);
|
||||
}
|
||||
else if (numBytesToRemove > 0)
|
||||
{
|
||||
memmove (data + startByte,
|
||||
data + startByte + numBytesToRemove,
|
||||
size - (startByte + numBytesToRemove));
|
||||
|
||||
setSize (size - numBytesToRemove);
|
||||
}
|
||||
}
|
||||
|
||||
void MemoryBlock::copyFrom (const void* const src, int offset, size_t num) noexcept
|
||||
{
|
||||
auto* d = static_cast<const char*> (src);
|
||||
|
||||
if (offset < 0)
|
||||
{
|
||||
d -= offset;
|
||||
num += (size_t) -offset;
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
if ((size_t) offset + num > size)
|
||||
num = size - (size_t) offset;
|
||||
|
||||
if (num > 0)
|
||||
memcpy (data + offset, d, num);
|
||||
}
|
||||
|
||||
void MemoryBlock::copyTo (void* const dst, int offset, size_t num) const noexcept
|
||||
{
|
||||
auto* d = static_cast<char*> (dst);
|
||||
|
||||
if (offset < 0)
|
||||
{
|
||||
zeromem (d, (size_t) -offset);
|
||||
d -= offset;
|
||||
num -= (size_t) -offset;
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
if ((size_t) offset + num > size)
|
||||
{
|
||||
auto newNum = (size_t) size - (size_t) offset;
|
||||
zeromem (d + newNum, num - newNum);
|
||||
num = newNum;
|
||||
}
|
||||
|
||||
if (num > 0)
|
||||
memcpy (d, data + offset, num);
|
||||
}
|
||||
|
||||
String MemoryBlock::toString() const
|
||||
{
|
||||
return String::fromUTF8 (data, (int) size);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
int MemoryBlock::getBitRange (size_t bitRangeStart, size_t numBits) const noexcept
|
||||
{
|
||||
int res = 0;
|
||||
|
||||
auto byte = bitRangeStart >> 3;
|
||||
auto offsetInByte = bitRangeStart & 7;
|
||||
size_t bitsSoFar = 0;
|
||||
|
||||
while (numBits > 0 && (size_t) byte < size)
|
||||
{
|
||||
auto bitsThisTime = jmin (numBits, 8 - offsetInByte);
|
||||
const int mask = (0xff >> (8 - bitsThisTime)) << offsetInByte;
|
||||
|
||||
res |= (((data[byte] & mask) >> offsetInByte) << bitsSoFar);
|
||||
|
||||
bitsSoFar += bitsThisTime;
|
||||
numBits -= bitsThisTime;
|
||||
++byte;
|
||||
offsetInByte = 0;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void MemoryBlock::setBitRange (const size_t bitRangeStart, size_t numBits, int bitsToSet) noexcept
|
||||
{
|
||||
auto byte = bitRangeStart >> 3;
|
||||
auto offsetInByte = bitRangeStart & 7;
|
||||
uint32 mask = ~((((uint32) 0xffffffff) << (32 - numBits)) >> (32 - numBits));
|
||||
|
||||
while (numBits > 0 && (size_t) byte < size)
|
||||
{
|
||||
auto bitsThisTime = jmin (numBits, 8 - offsetInByte);
|
||||
|
||||
const uint32 tempMask = (mask << offsetInByte) | ~((((uint32) 0xffffffff) >> offsetInByte) << offsetInByte);
|
||||
const uint32 tempBits = (uint32) bitsToSet << offsetInByte;
|
||||
|
||||
data[byte] = (char) (((uint32) data[byte] & tempMask) | tempBits);
|
||||
|
||||
++byte;
|
||||
numBits -= bitsThisTime;
|
||||
bitsToSet >>= bitsThisTime;
|
||||
mask >>= bitsThisTime;
|
||||
offsetInByte = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void MemoryBlock::loadFromHexString (StringRef hex)
|
||||
{
|
||||
ensureSize ((size_t) hex.length() >> 1);
|
||||
char* dest = data;
|
||||
auto t = hex.text;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
juce_wchar byte = 0;
|
||||
|
||||
for (int loop = 2; --loop >= 0;)
|
||||
{
|
||||
byte <<= 4;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
auto c = t.getAndAdvance();
|
||||
|
||||
if ('0' <= c && c <= '9') { byte |= c - '0'; break; }
|
||||
if ('a' <= c && c <= 'f') { byte |= c - ('a' - 10); break; }
|
||||
if ('A' <= c && c <= 'F') { byte |= c - ('A' - 10); break; }
|
||||
|
||||
if (c == 0)
|
||||
{
|
||||
setSize (static_cast<size_t> (dest - data));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*dest++ = (char) byte;
|
||||
}
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
static const char base64EncodingTable[] = ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+";
|
||||
|
||||
String MemoryBlock::toBase64Encoding() const
|
||||
{
|
||||
auto numChars = ((size << 3) + 5) / 6;
|
||||
|
||||
String destString ((unsigned int) size); // store the length, followed by a '.', and then the data
|
||||
auto initialLen = destString.length();
|
||||
destString.preallocateBytes ((size_t) initialLen * sizeof (String::CharPointerType::CharType) + 2 + numChars);
|
||||
|
||||
auto d = destString.getCharPointer();
|
||||
d += initialLen;
|
||||
d.write ('.');
|
||||
|
||||
for (size_t i = 0; i < numChars; ++i)
|
||||
d.write ((juce_wchar) (uint8) base64EncodingTable[getBitRange (i * 6, 6)]);
|
||||
|
||||
d.writeNull();
|
||||
return destString;
|
||||
}
|
||||
|
||||
static const char base64DecodingTable[] =
|
||||
{
|
||||
63, 0, 0, 0, 0, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
|
||||
0, 0, 0, 0, 0, 0, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52
|
||||
};
|
||||
|
||||
bool MemoryBlock::fromBase64Encoding (StringRef s)
|
||||
{
|
||||
auto dot = CharacterFunctions::find (s.text, (juce_wchar) '.');
|
||||
|
||||
if (dot.isEmpty())
|
||||
return false;
|
||||
|
||||
auto numBytesNeeded = String (s.text, dot).getIntValue();
|
||||
|
||||
setSize ((size_t) numBytesNeeded, true);
|
||||
|
||||
auto srcChars = dot + 1;
|
||||
size_t pos = 0;
|
||||
|
||||
while (auto c = (int) srcChars.getAndAdvance())
|
||||
{
|
||||
c -= 43;
|
||||
|
||||
if (isPositiveAndBelow (c, numElementsInArray (base64DecodingTable)))
|
||||
{
|
||||
setBitRange (pos * 6, 6, base64DecodingTable[c]);
|
||||
pos += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
#if JUCE_UNIT_TESTS
|
||||
|
||||
class MemoryBlockTest final : public UnitTest
|
||||
{
|
||||
public:
|
||||
MemoryBlockTest()
|
||||
: UnitTest ("MemoryBlock", UnitTestCategories::memory) {}
|
||||
|
||||
void runTest() final
|
||||
{
|
||||
testCase ("Empty blocks compare equal", [&]
|
||||
{
|
||||
const MemoryBlock a, b;
|
||||
|
||||
expect (a == b);
|
||||
expect (! (a != b));
|
||||
expect (a.matches (nullptr, 0));
|
||||
expect (a.matches (b.getData(), b.getSize()));
|
||||
});
|
||||
|
||||
testCase ("An empty block does not match a populated one", [&]
|
||||
{
|
||||
const MemoryBlock empty;
|
||||
const MemoryBlock populated { "abc", 3 };
|
||||
|
||||
expect (empty != populated);
|
||||
expect (populated != empty);
|
||||
expect (! empty.matches (populated.getData(), populated.getSize()));
|
||||
expect (! populated.matches (nullptr, 0));
|
||||
});
|
||||
|
||||
testCase ("Blocks with matching contents compare equal", [&]
|
||||
{
|
||||
const MemoryBlock a { "abc", 3 };
|
||||
const MemoryBlock b { "abc", 3 };
|
||||
const MemoryBlock c { "abd", 3 };
|
||||
|
||||
expect (a == b);
|
||||
expect (a != c);
|
||||
expect (a.matches (b.getData(), b.getSize()));
|
||||
expect (! a.matches (c.getData(), c.getSize()));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
static MemoryBlockTest memoryBlockTest;
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,302 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A class to hold a resizable block of raw data.
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
class JUCE_API MemoryBlock
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Create an uninitialised block with 0 size. */
|
||||
MemoryBlock() noexcept;
|
||||
|
||||
/** Creates a memory block with a given initial size.
|
||||
|
||||
@param initialSize the size of block to create
|
||||
@param initialiseToZero whether to clear the memory or just leave it uninitialised
|
||||
*/
|
||||
MemoryBlock (size_t initialSize,
|
||||
bool initialiseToZero = false);
|
||||
|
||||
/** Creates a copy of another memory block. */
|
||||
MemoryBlock (const MemoryBlock&);
|
||||
|
||||
/** Creates a memory block using a copy of a block of data.
|
||||
|
||||
@param dataToInitialiseFrom some data to copy into this block
|
||||
@param sizeInBytes how much space to use
|
||||
*/
|
||||
MemoryBlock (const void* dataToInitialiseFrom, size_t sizeInBytes);
|
||||
|
||||
/** Destructor. */
|
||||
~MemoryBlock() noexcept;
|
||||
|
||||
/** Copies another memory block onto this one.
|
||||
This block will be resized and copied to exactly match the other one.
|
||||
*/
|
||||
MemoryBlock& operator= (const MemoryBlock&);
|
||||
|
||||
/** Move constructor */
|
||||
MemoryBlock (MemoryBlock&&) noexcept;
|
||||
|
||||
/** Move assignment operator */
|
||||
MemoryBlock& operator= (MemoryBlock&&) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Compares two memory blocks.
|
||||
@returns true only if the two blocks are the same size and have identical contents.
|
||||
*/
|
||||
bool operator== (const MemoryBlock& other) const noexcept;
|
||||
|
||||
/** Compares two memory blocks.
|
||||
@returns true if the two blocks are different sizes or have different contents.
|
||||
*/
|
||||
bool operator!= (const MemoryBlock& other) const noexcept;
|
||||
|
||||
/** Returns true if the data in this MemoryBlock matches the raw bytes passed-in. */
|
||||
bool matches (const void* data, size_t dataSize) const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Returns a void pointer to the data.
|
||||
|
||||
Note that the pointer returned will probably become invalid when the
|
||||
block is resized.
|
||||
*/
|
||||
void* getData() noexcept { return data; }
|
||||
|
||||
/** Returns a void pointer to the data.
|
||||
|
||||
Note that the pointer returned will probably become invalid when the
|
||||
block is resized.
|
||||
*/
|
||||
const void* getData() const noexcept { return data; }
|
||||
|
||||
/** Returns a byte from the memory block.
|
||||
This returns a reference, so you can also use it to set a byte.
|
||||
*/
|
||||
template <typename Type>
|
||||
char& operator[] (const Type offset) noexcept { return data [offset]; }
|
||||
|
||||
/** Returns a byte from the memory block. */
|
||||
template <typename Type>
|
||||
const char& operator[] (const Type offset) const noexcept { return data [offset]; }
|
||||
|
||||
/** Returns an iterator for the data. */
|
||||
char* begin() noexcept { return data; }
|
||||
|
||||
/** Returns an iterator for the data. */
|
||||
const char* begin() const noexcept { return data; }
|
||||
|
||||
/** Returns an end-iterator for the data. */
|
||||
char* end() noexcept { return begin() + getSize(); }
|
||||
|
||||
/** Returns an end-iterator for the data. */
|
||||
const char* end() const noexcept { return begin() + getSize(); }
|
||||
|
||||
//==============================================================================
|
||||
/** Returns true if the memory block has zero size. */
|
||||
bool isEmpty() const noexcept { return getSize() == 0; }
|
||||
|
||||
/** Returns the block's current allocated size, in bytes. */
|
||||
size_t getSize() const noexcept { return size; }
|
||||
|
||||
/** Resizes the memory block.
|
||||
|
||||
Any data that is present in both the old and new sizes will be retained.
|
||||
When enlarging the block, the new space that is allocated at the end can either be
|
||||
cleared, or left uninitialised.
|
||||
|
||||
@param newSize the new desired size for the block
|
||||
@param initialiseNewSpaceToZero if the block gets enlarged, this determines
|
||||
whether to clear the new section or just leave it
|
||||
uninitialised
|
||||
@see ensureSize
|
||||
*/
|
||||
void setSize (size_t newSize,
|
||||
bool initialiseNewSpaceToZero = false);
|
||||
|
||||
/** Increases the block's size only if it's smaller than a given size.
|
||||
|
||||
@param minimumSize if the block is already bigger than this size, no action
|
||||
will be taken; otherwise it will be increased to this size
|
||||
@param initialiseNewSpaceToZero if the block gets enlarged, this determines
|
||||
whether to clear the new section or just leave it
|
||||
uninitialised
|
||||
@see setSize
|
||||
*/
|
||||
void ensureSize (size_t minimumSize,
|
||||
bool initialiseNewSpaceToZero = false);
|
||||
|
||||
/** Frees all the blocks data, setting its size to 0. */
|
||||
void reset();
|
||||
|
||||
//==============================================================================
|
||||
/** Fills the entire memory block with a repeated byte value.
|
||||
This is handy for clearing a block of memory to zero.
|
||||
*/
|
||||
void fillWith (uint8 valueToUse) noexcept;
|
||||
|
||||
/** Adds another block of data to the end of this one.
|
||||
The data pointer must not be null. This block's size will be increased accordingly.
|
||||
*/
|
||||
void append (const void* data, size_t numBytes);
|
||||
|
||||
/** Resizes this block to the given size and fills its contents from the supplied buffer.
|
||||
The data pointer must not be null.
|
||||
*/
|
||||
void replaceAll (const void* data, size_t numBytes);
|
||||
|
||||
/** Inserts some data into the block.
|
||||
The dataToInsert pointer must not be null. This block's size will be increased accordingly.
|
||||
If the insert position lies outside the valid range of the block, it will be clipped to
|
||||
within the range before being used.
|
||||
*/
|
||||
void insert (const void* dataToInsert, size_t numBytesToInsert, size_t insertPosition);
|
||||
|
||||
/** Chops out a section of the block.
|
||||
|
||||
This will remove a section of the memory block and close the gap around it,
|
||||
shifting any subsequent data downwards and reducing the size of the block.
|
||||
|
||||
If the range specified goes beyond the size of the block, it will be clipped.
|
||||
*/
|
||||
void removeSection (size_t startByte, size_t numBytesToRemove);
|
||||
|
||||
//==============================================================================
|
||||
/** Copies data into this MemoryBlock from a memory address.
|
||||
|
||||
@param srcData the memory location of the data to copy into this block
|
||||
@param destinationOffset the offset in this block at which the data being copied should begin
|
||||
@param numBytes how much to copy in (if this goes beyond the size of the memory block,
|
||||
it will be clipped so not to do anything nasty)
|
||||
*/
|
||||
void copyFrom (const void* srcData,
|
||||
int destinationOffset,
|
||||
size_t numBytes) noexcept;
|
||||
|
||||
/** Copies data from this MemoryBlock to a memory address.
|
||||
|
||||
@param destData the memory location to write to
|
||||
@param sourceOffset the offset within this block from which the copied data will be read
|
||||
@param numBytes how much to copy (if this extends beyond the limits of the memory block,
|
||||
zeros will be used for that portion of the data)
|
||||
*/
|
||||
void copyTo (void* destData,
|
||||
int sourceOffset,
|
||||
size_t numBytes) const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Exchanges the contents of this and another memory block.
|
||||
No actual copying is required for this, so it's very fast.
|
||||
*/
|
||||
void swapWith (MemoryBlock& other) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Attempts to parse the contents of the block as a zero-terminated UTF8 string. */
|
||||
String toString() const;
|
||||
|
||||
//==============================================================================
|
||||
/** Parses a string of hexadecimal numbers and writes this data into the memory block.
|
||||
|
||||
The block will be resized to the number of valid bytes read from the string.
|
||||
Non-hex characters in the string will be ignored.
|
||||
|
||||
@see String::toHexString()
|
||||
*/
|
||||
void loadFromHexString (StringRef sourceHexString);
|
||||
|
||||
//==============================================================================
|
||||
/** Sets a number of bits in the memory block, treating it as a long binary sequence. */
|
||||
void setBitRange (size_t bitRangeStart,
|
||||
size_t numBits,
|
||||
int binaryNumberToApply) noexcept;
|
||||
|
||||
/** Reads a number of bits from the memory block, treating it as one long binary sequence */
|
||||
int getBitRange (size_t bitRangeStart,
|
||||
size_t numBitsToRead) const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Returns a string of characters in a JUCE-specific text encoding that represents the
|
||||
binary contents of this block.
|
||||
|
||||
This uses a JUCE-specific (i.e. not standard!) 64-bit encoding system to convert binary
|
||||
data into a string of ASCII characters for purposes like storage in XML.
|
||||
Note that this proprietary format is mainly kept here for backwards-compatibility, and
|
||||
you may prefer to use the Base64::toBase64() method if you want to use the standard
|
||||
base-64 encoding.
|
||||
|
||||
@see fromBase64Encoding, Base64::toBase64, Base64::convertToBase64
|
||||
*/
|
||||
String toBase64Encoding() const;
|
||||
|
||||
/** Takes a string created by MemoryBlock::toBase64Encoding() and extracts the original data.
|
||||
|
||||
The string passed in must have been created by to64BitEncoding(), and this
|
||||
block will be resized to recreate the original data block.
|
||||
|
||||
Note that these methods use a JUCE-specific (i.e. not standard!) 64-bit encoding system.
|
||||
You may prefer to use the Base64::convertFromBase64() method if you want to use the
|
||||
standard base-64 encoding.
|
||||
|
||||
@see toBase64Encoding, Base64::convertFromBase64
|
||||
*/
|
||||
bool fromBase64Encoding (StringRef encodedString);
|
||||
|
||||
//==============================================================================
|
||||
/** @cond */
|
||||
[[deprecated ("Use the replaceAll method instead, which will also replace the data when numBytes == 0.")]]
|
||||
void replaceWith (const void* srcData, size_t numBytes)
|
||||
{
|
||||
if (numBytes > 0)
|
||||
replaceAll (srcData, numBytes);
|
||||
}
|
||||
/** @endcond */
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
using HeapBlockType = HeapBlock<char, true>;
|
||||
HeapBlockType data;
|
||||
size_t size = 0;
|
||||
|
||||
JUCE_LEAK_DETECTOR (MemoryBlock)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,203 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Holds a pointer to an object which can optionally be deleted when this pointer
|
||||
goes out of scope.
|
||||
|
||||
This acts in many ways like a std::unique_ptr, but allows you to specify whether or
|
||||
not the object is deleted.
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
template <class ObjectType>
|
||||
class OptionalScopedPointer
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates an empty OptionalScopedPointer. */
|
||||
OptionalScopedPointer() = default;
|
||||
|
||||
/** Creates an OptionalScopedPointer to point to a given object, and specifying whether
|
||||
the OptionalScopedPointer will delete it.
|
||||
|
||||
If takeOwnership is true, then the OptionalScopedPointer will act like a std::unique_ptr,
|
||||
deleting the object when it is itself deleted. If this parameter is false, then the
|
||||
OptionalScopedPointer just holds a normal pointer to the object, and won't delete it.
|
||||
*/
|
||||
OptionalScopedPointer (ObjectType* objectToHold, bool takeOwnership)
|
||||
: object (objectToHold),
|
||||
shouldDelete (takeOwnership)
|
||||
{
|
||||
}
|
||||
|
||||
/** Takes ownership of the object that another OptionalScopedPointer holds.
|
||||
|
||||
Like a normal std::unique_ptr, the objectToTransferFrom object will become null,
|
||||
as ownership of the managed object is transferred to this object.
|
||||
|
||||
The flag to indicate whether or not to delete the managed object is also
|
||||
copied from the source object.
|
||||
*/
|
||||
OptionalScopedPointer (OptionalScopedPointer&& other) noexcept
|
||||
: object (std::move (other.object)),
|
||||
shouldDelete (std::move (other.shouldDelete))
|
||||
{
|
||||
}
|
||||
|
||||
/** Takes ownership of the object owned by `ptr`. */
|
||||
explicit OptionalScopedPointer (std::unique_ptr<ObjectType>&& ptr) noexcept
|
||||
: OptionalScopedPointer (ptr.release(), true)
|
||||
{
|
||||
}
|
||||
|
||||
/** Points to the same object as `ref`, but does not take ownership. */
|
||||
explicit OptionalScopedPointer (ObjectType& ref) noexcept
|
||||
: OptionalScopedPointer (std::addressof (ref), false)
|
||||
{
|
||||
}
|
||||
|
||||
/** Takes ownership of the object that another OptionalScopedPointer holds.
|
||||
|
||||
Like a normal std::unique_ptr, the objectToTransferFrom object will become null,
|
||||
as ownership of the managed object is transferred to this object.
|
||||
|
||||
The ownership flag that says whether or not to delete the managed object is also
|
||||
copied from the source object.
|
||||
*/
|
||||
OptionalScopedPointer& operator= (OptionalScopedPointer&& other) noexcept
|
||||
{
|
||||
swapWith (other);
|
||||
other.reset();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** The destructor may or may not delete the object that is being held, depending on the
|
||||
takeOwnership flag that was specified when the object was first passed into an
|
||||
OptionalScopedPointer constructor.
|
||||
*/
|
||||
~OptionalScopedPointer() noexcept
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
/** Returns the object that this pointer is managing. */
|
||||
operator ObjectType*() const noexcept { return object.get(); }
|
||||
|
||||
/** Returns the object that this pointer is managing. */
|
||||
ObjectType* get() const noexcept { return object.get(); }
|
||||
|
||||
/** Returns the object that this pointer is managing. */
|
||||
ObjectType& operator*() const noexcept { return *object; }
|
||||
|
||||
/** Lets you access methods and properties of the object that this pointer is holding. */
|
||||
ObjectType* operator->() const noexcept { return object.get(); }
|
||||
|
||||
//==============================================================================
|
||||
/** Removes the current object from this OptionalScopedPointer without deleting it.
|
||||
This will return the current object, and set this OptionalScopedPointer to a null pointer.
|
||||
*/
|
||||
ObjectType* release() noexcept { return object.release(); }
|
||||
|
||||
/** Resets this pointer to null, possibly deleting the object that it holds, if it has
|
||||
ownership of it.
|
||||
*/
|
||||
void reset() noexcept
|
||||
{
|
||||
if (! shouldDelete)
|
||||
object.release();
|
||||
else
|
||||
object.reset();
|
||||
}
|
||||
|
||||
/** Does the same thing as reset(). */
|
||||
void clear() { reset(); }
|
||||
|
||||
/** Makes this OptionalScopedPointer point at a new object, specifying whether the
|
||||
OptionalScopedPointer will take ownership of the object.
|
||||
|
||||
If takeOwnership is true, then the OptionalScopedPointer will act like a std::unique_ptr,
|
||||
deleting the object when it is itself deleted. If this parameter is false, then the
|
||||
OptionalScopedPointer just holds a normal pointer to the object, and won't delete it.
|
||||
*/
|
||||
void set (ObjectType* newObject, bool takeOwnership)
|
||||
{
|
||||
if (object.get() != newObject)
|
||||
{
|
||||
reset();
|
||||
object.reset (newObject);
|
||||
}
|
||||
|
||||
shouldDelete = takeOwnership;
|
||||
}
|
||||
|
||||
/** Makes this OptionalScopedPointer point at a new object, and take ownership of that object. */
|
||||
void setOwned (ObjectType* newObject)
|
||||
{
|
||||
set (newObject, true);
|
||||
}
|
||||
|
||||
/** Makes this OptionalScopedPointer point at a new object, but will not take ownership of that object. */
|
||||
void setNonOwned (ObjectType* newObject)
|
||||
{
|
||||
set (newObject, false);
|
||||
}
|
||||
|
||||
/** Returns true if the target object will be deleted when this pointer
|
||||
object is deleted.
|
||||
*/
|
||||
bool willDeleteObject() const noexcept { return shouldDelete; }
|
||||
|
||||
//==============================================================================
|
||||
/** Swaps this object with another OptionalScopedPointer.
|
||||
The two objects simply exchange their states.
|
||||
*/
|
||||
void swapWith (OptionalScopedPointer<ObjectType>& other) noexcept
|
||||
{
|
||||
std::swap (other.object, object);
|
||||
std::swap (other.shouldDelete, shouldDelete);
|
||||
}
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
std::unique_ptr<ObjectType> object;
|
||||
bool shouldDelete = false;
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,481 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A base class which provides methods for reference-counting.
|
||||
|
||||
To add reference-counting to a class, derive it from this class, and
|
||||
use the ReferenceCountedObjectPtr class to point to it.
|
||||
|
||||
e.g. @code
|
||||
class MyClass : public ReferenceCountedObject
|
||||
{
|
||||
void foo();
|
||||
|
||||
// This is a neat way of declaring a typedef for a pointer class,
|
||||
// rather than typing out the full templated name each time.
|
||||
using Ptr = ReferenceCountedObjectPtr<MyClass>;
|
||||
};
|
||||
|
||||
MyClass::Ptr p = new MyClass();
|
||||
MyClass::Ptr p2 = p;
|
||||
p = nullptr;
|
||||
p2->foo();
|
||||
@endcode
|
||||
|
||||
Once a new ReferenceCountedObject has been assigned to a pointer, be
|
||||
careful not to delete the object manually.
|
||||
|
||||
This class uses an Atomic<int> value to hold the reference count, so
|
||||
the reference count can be updated on multiple threads. Note that
|
||||
whilst it's thread-safe to create and delete a ReferenceCountedObjectPtr
|
||||
to a ReferenceCountedObject shared between threads, it's not thread-safe
|
||||
to modify or swap the ReferenceCountedObject.
|
||||
|
||||
For a faster but non-thread-safe version, use SingleThreadedReferenceCountedObject
|
||||
instead.
|
||||
|
||||
@see ReferenceCountedObjectPtr, ReferenceCountedArray, SingleThreadedReferenceCountedObject
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
class JUCE_API ReferenceCountedObject
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Increments the object's reference count.
|
||||
|
||||
This is done automatically by the smart pointer, but is public just
|
||||
in case it's needed for nefarious purposes.
|
||||
*/
|
||||
void incReferenceCount() noexcept
|
||||
{
|
||||
++refCount;
|
||||
}
|
||||
|
||||
/** Decreases the object's reference count.
|
||||
If the count gets to zero, the object will be deleted.
|
||||
*/
|
||||
void decReferenceCount() noexcept
|
||||
{
|
||||
jassert (getReferenceCount() > 0);
|
||||
|
||||
if (--refCount == 0)
|
||||
delete this;
|
||||
}
|
||||
|
||||
/** Decreases the object's reference count.
|
||||
If the count gets to zero, the object will not be deleted, but this method
|
||||
will return true, allowing the caller to take care of deletion.
|
||||
*/
|
||||
bool decReferenceCountWithoutDeleting() noexcept
|
||||
{
|
||||
jassert (getReferenceCount() > 0);
|
||||
return --refCount == 0;
|
||||
}
|
||||
|
||||
/** Returns the object's current reference count. */
|
||||
int getReferenceCount() const noexcept { return refCount.get(); }
|
||||
|
||||
|
||||
protected:
|
||||
//==============================================================================
|
||||
/** Creates the reference-counted object (with an initial ref count of zero). */
|
||||
ReferenceCountedObject() = default;
|
||||
|
||||
/** Copying from another object does not affect this one's reference-count. */
|
||||
ReferenceCountedObject (const ReferenceCountedObject&) noexcept {}
|
||||
/** Copying from another object does not affect this one's reference-count. */
|
||||
ReferenceCountedObject (ReferenceCountedObject&&) noexcept {}
|
||||
/** Copying from another object does not affect this one's reference-count. */
|
||||
ReferenceCountedObject& operator= (const ReferenceCountedObject&) noexcept { return *this; }
|
||||
/** Copying from another object does not affect this one's reference-count. */
|
||||
ReferenceCountedObject& operator= (ReferenceCountedObject&&) noexcept { return *this; }
|
||||
|
||||
/** Destructor. */
|
||||
virtual ~ReferenceCountedObject()
|
||||
{
|
||||
// it's dangerous to delete an object that's still referenced by something else!
|
||||
jassert (getReferenceCount() == 0);
|
||||
}
|
||||
|
||||
/** Resets the reference count to zero without deleting the object.
|
||||
You should probably never need to use this!
|
||||
*/
|
||||
void resetReferenceCount() noexcept
|
||||
{
|
||||
refCount = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
Atomic<int> refCount { 0 };
|
||||
friend struct ContainerDeletePolicy<ReferenceCountedObject>;
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Adds reference-counting to an object.
|
||||
|
||||
This is effectively a version of the ReferenceCountedObject class, but which
|
||||
uses a non-atomic counter, and so is not thread-safe (but which will be more
|
||||
efficient).
|
||||
For more details on how to use it, see the ReferenceCountedObject class notes.
|
||||
|
||||
@see ReferenceCountedObject, ReferenceCountedObjectPtr, ReferenceCountedArray
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
class JUCE_API SingleThreadedReferenceCountedObject
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Increments the object's reference count.
|
||||
|
||||
This is done automatically by the smart pointer, but is public just
|
||||
in case it's needed for nefarious purposes.
|
||||
*/
|
||||
void incReferenceCount() noexcept
|
||||
{
|
||||
++refCount;
|
||||
}
|
||||
|
||||
/** Decreases the object's reference count.
|
||||
If the count gets to zero, the object will be deleted.
|
||||
*/
|
||||
void decReferenceCount() noexcept
|
||||
{
|
||||
jassert (getReferenceCount() > 0);
|
||||
|
||||
if (--refCount == 0)
|
||||
delete this;
|
||||
}
|
||||
|
||||
/** Decreases the object's reference count.
|
||||
If the count gets to zero, the object will not be deleted, but this method
|
||||
will return true, allowing the caller to take care of deletion.
|
||||
*/
|
||||
bool decReferenceCountWithoutDeleting() noexcept
|
||||
{
|
||||
jassert (getReferenceCount() > 0);
|
||||
return --refCount == 0;
|
||||
}
|
||||
|
||||
/** Returns the object's current reference count. */
|
||||
int getReferenceCount() const noexcept { return refCount; }
|
||||
|
||||
|
||||
protected:
|
||||
//==============================================================================
|
||||
/** Creates the reference-counted object (with an initial ref count of zero). */
|
||||
SingleThreadedReferenceCountedObject() = default;
|
||||
|
||||
/** Copying from another object does not affect this one's reference-count. */
|
||||
SingleThreadedReferenceCountedObject (const SingleThreadedReferenceCountedObject&) {}
|
||||
/** Copying from another object does not affect this one's reference-count. */
|
||||
SingleThreadedReferenceCountedObject (SingleThreadedReferenceCountedObject&&) {}
|
||||
/** Copying from another object does not affect this one's reference-count. */
|
||||
SingleThreadedReferenceCountedObject& operator= (const SingleThreadedReferenceCountedObject&) { return *this; }
|
||||
/** Copying from another object does not affect this one's reference-count. */
|
||||
SingleThreadedReferenceCountedObject& operator= (SingleThreadedReferenceCountedObject&&) { return *this; }
|
||||
|
||||
/** Destructor. */
|
||||
virtual ~SingleThreadedReferenceCountedObject()
|
||||
{
|
||||
// it's dangerous to delete an object that's still referenced by something else!
|
||||
jassert (getReferenceCount() == 0);
|
||||
}
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
int refCount = 0;
|
||||
friend struct ContainerDeletePolicy<ReferenceCountedObject>;
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A smart-pointer class which points to a reference-counted object.
|
||||
|
||||
The template parameter specifies the class of the object you want to point to - the easiest
|
||||
way to make a class reference-countable is to simply make it inherit from ReferenceCountedObject
|
||||
or SingleThreadedReferenceCountedObject, but if you need to, you can roll your own reference-countable
|
||||
class by implementing a set of methods called incReferenceCount(), decReferenceCount(), and
|
||||
decReferenceCountWithoutDeleting(). See ReferenceCountedObject for examples of how these methods
|
||||
should behave.
|
||||
|
||||
When using this class, you'll probably want to create a typedef to abbreviate the full
|
||||
templated name - e.g.
|
||||
@code
|
||||
struct MyClass : public ReferenceCountedObject
|
||||
{
|
||||
using Ptr = ReferenceCountedObjectPtr<MyClass>;
|
||||
...
|
||||
}
|
||||
@endcode
|
||||
|
||||
@see ReferenceCountedObject, ReferenceCountedObjectArray
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
template <class ObjectType>
|
||||
class ReferenceCountedObjectPtr
|
||||
{
|
||||
public:
|
||||
/** The class being referenced by this pointer. */
|
||||
using ReferencedType = ObjectType;
|
||||
|
||||
//==============================================================================
|
||||
/** Creates a pointer to a null object. */
|
||||
ReferenceCountedObjectPtr() = default;
|
||||
|
||||
/** Creates a pointer to a null object. */
|
||||
ReferenceCountedObjectPtr (decltype (nullptr)) noexcept {}
|
||||
|
||||
/** Creates a pointer to an object.
|
||||
This will increment the object's reference-count.
|
||||
*/
|
||||
ReferenceCountedObjectPtr (ReferencedType* refCountedObject) noexcept
|
||||
: referencedObject (refCountedObject)
|
||||
{
|
||||
incIfNotNull (refCountedObject);
|
||||
}
|
||||
|
||||
/** Creates a pointer to an object.
|
||||
This will increment the object's reference-count.
|
||||
*/
|
||||
ReferenceCountedObjectPtr (ReferencedType& refCountedObject) noexcept
|
||||
: referencedObject (&refCountedObject)
|
||||
{
|
||||
refCountedObject.incReferenceCount();
|
||||
}
|
||||
|
||||
/** Copies another pointer.
|
||||
This will increment the object's reference-count.
|
||||
*/
|
||||
ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr& other) noexcept
|
||||
: referencedObject (other.referencedObject)
|
||||
{
|
||||
incIfNotNull (referencedObject);
|
||||
}
|
||||
|
||||
/** Takes-over the object from another pointer. */
|
||||
ReferenceCountedObjectPtr (ReferenceCountedObjectPtr&& other) noexcept
|
||||
: referencedObject (other.referencedObject)
|
||||
{
|
||||
other.referencedObject = nullptr;
|
||||
}
|
||||
|
||||
/** Copies another pointer.
|
||||
This will increment the object's reference-count (if it is non-null).
|
||||
*/
|
||||
template <typename Convertible>
|
||||
ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr<Convertible>& other) noexcept
|
||||
: referencedObject (other.get())
|
||||
{
|
||||
incIfNotNull (referencedObject);
|
||||
}
|
||||
|
||||
/** Changes this pointer to point at a different object.
|
||||
The reference count of the old object is decremented, and it might be
|
||||
deleted if it hits zero. The new object's count is incremented.
|
||||
*/
|
||||
ReferenceCountedObjectPtr& operator= (const ReferenceCountedObjectPtr& other)
|
||||
{
|
||||
return operator= (other.referencedObject);
|
||||
}
|
||||
|
||||
/** Changes this pointer to point at a different object.
|
||||
The reference count of the old object is decremented, and it might be
|
||||
deleted if it hits zero. The new object's count is incremented.
|
||||
*/
|
||||
template <typename Convertible>
|
||||
ReferenceCountedObjectPtr& operator= (const ReferenceCountedObjectPtr<Convertible>& other)
|
||||
{
|
||||
return operator= (other.get());
|
||||
}
|
||||
|
||||
/** Changes this pointer to point at a different object.
|
||||
|
||||
The reference count of the old object is decremented, and it might be
|
||||
deleted if it hits zero. The new object's count is incremented.
|
||||
*/
|
||||
ReferenceCountedObjectPtr& operator= (ReferencedType* newObject)
|
||||
{
|
||||
if (newObject != nullptr)
|
||||
return operator= (*newObject);
|
||||
|
||||
reset();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Changes this pointer to point at a different object.
|
||||
|
||||
The reference count of the old object is decremented, and it might be
|
||||
deleted if it hits zero. The new object's count is incremented.
|
||||
*/
|
||||
ReferenceCountedObjectPtr& operator= (ReferencedType& newObject)
|
||||
{
|
||||
if (referencedObject != &newObject)
|
||||
{
|
||||
newObject.incReferenceCount();
|
||||
auto* oldObject = referencedObject;
|
||||
referencedObject = &newObject;
|
||||
decIfNotNull (oldObject);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Resets this pointer to a null pointer. */
|
||||
ReferenceCountedObjectPtr& operator= (decltype (nullptr))
|
||||
{
|
||||
reset();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Takes-over the object from another pointer. */
|
||||
ReferenceCountedObjectPtr& operator= (ReferenceCountedObjectPtr&& other) noexcept
|
||||
{
|
||||
std::swap (referencedObject, other.referencedObject);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Destructor.
|
||||
This will decrement the object's reference-count, which will cause the
|
||||
object to be deleted when the ref-count hits zero.
|
||||
*/
|
||||
~ReferenceCountedObjectPtr()
|
||||
{
|
||||
decIfNotNull (referencedObject);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
/** Returns the object that this pointer references.
|
||||
The pointer returned may be null, of course.
|
||||
*/
|
||||
ReferencedType* get() const noexcept { return referencedObject; }
|
||||
|
||||
/** Resets this object to a null pointer. */
|
||||
void reset() noexcept
|
||||
{
|
||||
auto oldObject = referencedObject; // need to null the pointer before deleting the object
|
||||
referencedObject = nullptr; // in case this ptr is itself deleted as a side-effect
|
||||
decIfNotNull (oldObject); // of the destructor
|
||||
}
|
||||
|
||||
// the -> operator is called on the referenced object
|
||||
ReferencedType* operator->() const noexcept
|
||||
{
|
||||
jassert (referencedObject != nullptr); // null pointer method call!
|
||||
return referencedObject;
|
||||
}
|
||||
|
||||
/** Dereferences the object that this pointer references.
|
||||
The pointer returned may be null, of course.
|
||||
*/
|
||||
ReferencedType& operator*() const noexcept { jassert (referencedObject != nullptr); return *referencedObject; }
|
||||
|
||||
/** Checks whether this pointer is null */
|
||||
bool operator== (decltype (nullptr)) const noexcept { return referencedObject == nullptr; }
|
||||
/** Checks whether this pointer is null */
|
||||
bool operator!= (decltype (nullptr)) const noexcept { return referencedObject != nullptr; }
|
||||
|
||||
/** Compares two ReferenceCountedObjectPtrs. */
|
||||
bool operator== (const ObjectType* other) const noexcept { return referencedObject == other; }
|
||||
/** Compares two ReferenceCountedObjectPtrs. */
|
||||
bool operator== (const ReferenceCountedObjectPtr& other) const noexcept { return referencedObject == other.get(); }
|
||||
/** Compares two ReferenceCountedObjectPtrs. */
|
||||
bool operator!= (const ObjectType* other) const noexcept { return referencedObject != other; }
|
||||
/** Compares two ReferenceCountedObjectPtrs. */
|
||||
bool operator!= (const ReferenceCountedObjectPtr& other) const noexcept { return referencedObject != other.get(); }
|
||||
|
||||
#if JUCE_STRICT_REFCOUNTEDPOINTER
|
||||
/** Checks whether this pointer is null */
|
||||
explicit operator bool() const noexcept { return referencedObject != nullptr; }
|
||||
|
||||
#else
|
||||
/** Returns the object that this pointer references.
|
||||
The pointer returned may be null, of course.
|
||||
Note that this methods allows the compiler to be very lenient with what it allows you to do
|
||||
with the pointer, it's safer to disable this by setting JUCE_STRICT_REFCOUNTEDPOINTER=1, which
|
||||
increased type safety and can prevent some common slip-ups.
|
||||
*/
|
||||
operator ReferencedType*() const noexcept { return referencedObject; }
|
||||
#endif
|
||||
|
||||
/** @cond */
|
||||
[[deprecated ("Use the get method instead.")]]
|
||||
ReferencedType* getObject() const { return get(); }
|
||||
/** @endcond */
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
ReferencedType* referencedObject = nullptr;
|
||||
|
||||
static void incIfNotNull (ReferencedType* o) noexcept
|
||||
{
|
||||
if (o != nullptr)
|
||||
o->incReferenceCount();
|
||||
}
|
||||
|
||||
static void decIfNotNull (ReferencedType* o) noexcept
|
||||
{
|
||||
if (o != nullptr && o->decReferenceCountWithoutDeleting())
|
||||
ContainerDeletePolicy<ReferencedType>::destroy (o);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/** Compares two ReferenceCountedObjectPtrs. */
|
||||
template <typename Type>
|
||||
bool operator== (const Type* object1, const ReferenceCountedObjectPtr<Type>& object2) noexcept
|
||||
{
|
||||
return object1 == object2.get();
|
||||
}
|
||||
|
||||
/** Compares two ReferenceCountedObjectPtrs. */
|
||||
template <typename Type>
|
||||
bool operator!= (const Type* object1, const ReferenceCountedObjectPtr<Type>& object2) noexcept
|
||||
{
|
||||
return object1 != object2.get();
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
/**
|
||||
Helper functions for managing buffered readers.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
struct Reservoir
|
||||
{
|
||||
/** Attempts to read the requested range from some kind of input stream,
|
||||
with intermediate buffering in a 'reservoir'.
|
||||
|
||||
While there are still samples in the requested range left to read, this
|
||||
function will check whether the next part of the requested range is
|
||||
already loaded into the reservoir. If the range is available, then
|
||||
doBufferedRead will call readFromReservoir with the range that should
|
||||
be copied to the output buffer. If the range is not available,
|
||||
doBufferedRead will call fillReservoir to request that a new region is
|
||||
loaded into the reservoir. It will repeat these steps until either the
|
||||
entire requested region has been read, or the stream ends.
|
||||
|
||||
This will return the range that could not be read successfully, if any.
|
||||
An empty range implies that the entire read was successful.
|
||||
|
||||
Note that all ranges, including those provided as arguments to the
|
||||
callbacks, are relative to the original unbuffered input. That is, if
|
||||
getBufferedRange returns the range [200, 300), then readFromReservoir
|
||||
might be passed the range [250, 300) in order to copy the final 50
|
||||
samples from the reservoir.
|
||||
|
||||
@param rangeToRead the absolute position of the range that should
|
||||
be read
|
||||
@param getBufferedRange a function void -> Range<Index> that returns
|
||||
the region currently held in the reservoir
|
||||
@param readFromReservoir a function Range<Index> -> void that can be
|
||||
used to copy samples from the region in the
|
||||
reservoir specified in the input range
|
||||
@param fillReservoir a function Index -> void that is given a
|
||||
requested read location, and that should
|
||||
attempt to fill the reservoir starting at this
|
||||
location. After this function,
|
||||
getBufferedRange should return the new region
|
||||
contained in the managed buffer
|
||||
*/
|
||||
template <typename Index, typename GetBufferedRange, typename ReadFromReservoir, typename FillReservoir>
|
||||
static Range<Index> doBufferedRead (Range<Index> rangeToRead,
|
||||
GetBufferedRange&& getBufferedRange,
|
||||
ReadFromReservoir&& readFromReservoir,
|
||||
FillReservoir&& fillReservoir)
|
||||
{
|
||||
while (! rangeToRead.isEmpty())
|
||||
{
|
||||
const auto bufferedRange = getBufferedRange();
|
||||
|
||||
if (bufferedRange.contains (rangeToRead.getStart()))
|
||||
{
|
||||
const auto rangeToReadInBuffer = rangeToRead.getIntersectionWith (getBufferedRange());
|
||||
readFromReservoir (rangeToReadInBuffer);
|
||||
rangeToRead.setStart (rangeToReadInBuffer.getEnd());
|
||||
}
|
||||
else
|
||||
{
|
||||
fillReservoir (rangeToRead.getStart());
|
||||
|
||||
const auto newRange = getBufferedRange();
|
||||
|
||||
if (newRange.isEmpty() || ! newRange.contains (rangeToRead.getStart()))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return rangeToRead;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,230 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
/** @cond */
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
This class is deprecated. You should use std::unique_ptr instead.
|
||||
*/
|
||||
template <class ObjectType>
|
||||
class [[deprecated]] ScopedPointer
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
JUCE_BEGIN_IGNORE_DEPRECATION_WARNINGS
|
||||
|
||||
inline ScopedPointer() {}
|
||||
|
||||
inline ScopedPointer (decltype (nullptr)) noexcept {}
|
||||
|
||||
inline ScopedPointer (ObjectType* objectToTakePossessionOf) noexcept
|
||||
: object (objectToTakePossessionOf)
|
||||
{
|
||||
}
|
||||
|
||||
ScopedPointer (ScopedPointer& objectToTransferFrom) noexcept
|
||||
: object (objectToTransferFrom.release())
|
||||
{
|
||||
}
|
||||
|
||||
inline ~ScopedPointer() { reset(); }
|
||||
|
||||
ScopedPointer& operator= (ScopedPointer& objectToTransferFrom)
|
||||
{
|
||||
if (this != objectToTransferFrom.getAddress())
|
||||
{
|
||||
// Two ScopedPointers should never be able to refer to the same object - if
|
||||
// this happens, you must have done something dodgy!
|
||||
jassert (object == nullptr || object != objectToTransferFrom.object);
|
||||
reset (objectToTransferFrom.release());
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
ScopedPointer& operator= (ObjectType* newObjectToTakePossessionOf)
|
||||
{
|
||||
reset (newObjectToTakePossessionOf);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ScopedPointer (ScopedPointer&& other) noexcept : object (other.object)
|
||||
{
|
||||
other.object = nullptr;
|
||||
}
|
||||
|
||||
ScopedPointer& operator= (ScopedPointer&& other) noexcept
|
||||
{
|
||||
reset (other.release());
|
||||
return *this;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
inline operator ObjectType*() const noexcept { return object; }
|
||||
inline ObjectType* get() const noexcept { return object; }
|
||||
inline ObjectType& operator*() const noexcept { return *object; }
|
||||
inline ObjectType* operator->() const noexcept { return object; }
|
||||
|
||||
void reset()
|
||||
{
|
||||
auto* oldObject = object;
|
||||
object = {};
|
||||
ContainerDeletePolicy<ObjectType>::destroy (oldObject);
|
||||
}
|
||||
|
||||
void reset (ObjectType* newObject)
|
||||
{
|
||||
if (object != newObject)
|
||||
{
|
||||
auto* oldObject = object;
|
||||
object = newObject;
|
||||
ContainerDeletePolicy<ObjectType>::destroy (oldObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
// You're trying to reset this ScopedPointer to itself! This will work here as ScopedPointer does an equality check
|
||||
// but be aware that std::unique_ptr won't do this and you could end up with some nasty, subtle bugs!
|
||||
jassert (newObject == nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void reset (ScopedPointer& newObject)
|
||||
{
|
||||
reset (newObject.release());
|
||||
}
|
||||
|
||||
ObjectType* release() noexcept { auto* o = object; object = {}; return o; }
|
||||
|
||||
//==============================================================================
|
||||
void swapWith (ScopedPointer<ObjectType>& other) noexcept
|
||||
{
|
||||
// Two ScopedPointers should never be able to refer to the same object - if
|
||||
// this happens, you must have done something dodgy!
|
||||
jassert (object != other.object || this == other.getAddress() || object == nullptr);
|
||||
|
||||
std::swap (object, other.object);
|
||||
}
|
||||
|
||||
inline ObjectType* createCopy() const { return createCopyIfNotNull (object); }
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
ObjectType* object = nullptr;
|
||||
|
||||
const ScopedPointer* getAddress() const noexcept { return this; } // Used internally to avoid the & operator
|
||||
|
||||
#if ! JUCE_MSVC // (MSVC can't deal with multiple copy constructors)
|
||||
ScopedPointer (const ScopedPointer&) = delete;
|
||||
ScopedPointer& operator= (const ScopedPointer&) = delete;
|
||||
#endif
|
||||
|
||||
JUCE_END_IGNORE_DEPRECATION_WARNINGS
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
JUCE_BEGIN_IGNORE_DEPRECATION_WARNINGS
|
||||
|
||||
template <typename ObjectType1, typename ObjectType2>
|
||||
bool operator== (ObjectType1* pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
|
||||
{
|
||||
return pointer1 == pointer2.get();
|
||||
}
|
||||
|
||||
template <typename ObjectType1, typename ObjectType2>
|
||||
bool operator!= (ObjectType1* pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
|
||||
{
|
||||
return pointer1 != pointer2.get();
|
||||
}
|
||||
|
||||
template <typename ObjectType1, typename ObjectType2>
|
||||
bool operator== (const ScopedPointer<ObjectType1>& pointer1, ObjectType2* pointer2) noexcept
|
||||
{
|
||||
return pointer1.get() == pointer2;
|
||||
}
|
||||
|
||||
template <typename ObjectType1, typename ObjectType2>
|
||||
bool operator!= (const ScopedPointer<ObjectType1>& pointer1, ObjectType2* pointer2) noexcept
|
||||
{
|
||||
return pointer1.get() != pointer2;
|
||||
}
|
||||
|
||||
template <typename ObjectType1, typename ObjectType2>
|
||||
bool operator== (const ScopedPointer<ObjectType1>& pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
|
||||
{
|
||||
return pointer1.get() == pointer2.get();
|
||||
}
|
||||
|
||||
template <typename ObjectType1, typename ObjectType2>
|
||||
bool operator!= (const ScopedPointer<ObjectType1>& pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
|
||||
{
|
||||
return pointer1.get() != pointer2.get();
|
||||
}
|
||||
|
||||
template <class ObjectType>
|
||||
bool operator== (decltype (nullptr), const ScopedPointer<ObjectType>& pointer) noexcept
|
||||
{
|
||||
return pointer.get() == nullptr;
|
||||
}
|
||||
|
||||
template <class ObjectType>
|
||||
bool operator!= (decltype (nullptr), const ScopedPointer<ObjectType>& pointer) noexcept
|
||||
{
|
||||
return pointer.get() != nullptr;
|
||||
}
|
||||
|
||||
template <class ObjectType>
|
||||
bool operator== (const ScopedPointer<ObjectType>& pointer, decltype (nullptr)) noexcept
|
||||
{
|
||||
return pointer.get() == nullptr;
|
||||
}
|
||||
|
||||
template <class ObjectType>
|
||||
bool operator!= (const ScopedPointer<ObjectType>& pointer, decltype (nullptr)) noexcept
|
||||
{
|
||||
return pointer.get() != nullptr;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
// NB: This is just here to prevent any silly attempts to call deleteAndZero() on a ScopedPointer.
|
||||
template <typename Type>
|
||||
void deleteAndZero (ScopedPointer<Type>&) { static_assert (sizeof (Type) == 12345,
|
||||
"Attempt to call deleteAndZero() on a ScopedPointer"); }
|
||||
|
||||
JUCE_END_IGNORE_DEPRECATION_WARNINGS
|
||||
|
||||
} // namespace juce
|
||||
/** @endcond */
|
||||
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A smart-pointer that automatically creates and manages the lifetime of a
|
||||
shared static instance of a class.
|
||||
|
||||
The SharedObjectType template type indicates the class to use for the shared
|
||||
object - the only requirements on this class are that it must have a public
|
||||
default constructor and destructor.
|
||||
|
||||
The SharedResourcePointer offers a pattern that differs from using a singleton or
|
||||
static instance of an object, because it uses reference-counting to make sure that
|
||||
the underlying shared object is automatically created/destroyed according to the
|
||||
number of SharedResourcePointer objects that exist. When the last one is deleted,
|
||||
the underlying object is also immediately destroyed. This allows you to use scoping
|
||||
to manage the lifetime of a shared resource.
|
||||
|
||||
Note: The construction/deletion of the shared object must not involve any
|
||||
code that makes recursive calls to a SharedResourcePointer, or you'll cause
|
||||
a deadlock.
|
||||
|
||||
Example:
|
||||
@code
|
||||
// An example of a class that contains the shared data you want to use.
|
||||
struct MySharedData
|
||||
{
|
||||
// There's no need to ever create an instance of this class directly yourself,
|
||||
// but it does need a public constructor that does the initialisation.
|
||||
MySharedData()
|
||||
{
|
||||
sharedStuff = generateHeavyweightStuff();
|
||||
}
|
||||
|
||||
Array<SomeKindOfData> sharedStuff;
|
||||
};
|
||||
|
||||
struct DataUserClass
|
||||
{
|
||||
DataUserClass()
|
||||
{
|
||||
// Multiple instances of the DataUserClass will all have the same
|
||||
// shared common instance of MySharedData referenced by their sharedData
|
||||
// member variables.
|
||||
useSharedStuff (sharedData->sharedStuff);
|
||||
}
|
||||
|
||||
// By keeping this pointer as a member variable, the shared resource
|
||||
// is guaranteed to be available for as long as the DataUserClass object.
|
||||
SharedResourcePointer<MySharedData> sharedData;
|
||||
};
|
||||
|
||||
@endcode
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
template <typename SharedObjectType>
|
||||
class SharedResourcePointer
|
||||
{
|
||||
public:
|
||||
/** Creates an instance of the shared object.
|
||||
If other SharedResourcePointer objects for this type already exist, then
|
||||
this one will simply point to the same shared object that they are already
|
||||
using. Otherwise, if this is the first SharedResourcePointer to be created,
|
||||
then a shared object will be created automatically.
|
||||
*/
|
||||
SharedResourcePointer() = default;
|
||||
|
||||
/** Copy constructor. */
|
||||
SharedResourcePointer (const SharedResourcePointer&) = default;
|
||||
|
||||
/** Move constructor. */
|
||||
SharedResourcePointer (SharedResourcePointer&&) noexcept = default;
|
||||
|
||||
/** Destructor.
|
||||
If no other SharedResourcePointer objects exist, this will also delete
|
||||
the shared object to which it refers.
|
||||
*/
|
||||
~SharedResourcePointer() = default;
|
||||
|
||||
/** Returns a pointer to the shared object. */
|
||||
operator SharedObjectType*() const noexcept { return sharedObject.get(); }
|
||||
|
||||
/** Returns a reference to the shared object. */
|
||||
SharedObjectType& get() const noexcept { return *sharedObject; }
|
||||
|
||||
/** Returns a reference to the shared object. */
|
||||
SharedObjectType& getObject() const noexcept { return *sharedObject; }
|
||||
|
||||
/** Returns a pointer to the shared object. */
|
||||
SharedObjectType* operator->() const noexcept { return sharedObject.get(); }
|
||||
|
||||
/** Returns a reference to the shared object. */
|
||||
SharedObjectType& operator*() const noexcept { return *sharedObject; }
|
||||
|
||||
/** @cond */
|
||||
[[deprecated ("If you are relying on this function please inform the JUCE team as we are planing on removing this in a subsequent release")]]
|
||||
int getReferenceCount() const noexcept { return (int) sharedObject.use_count(); }
|
||||
/** @endcond */
|
||||
|
||||
/** Returns the SharedResourcePointer if one already exists, or a null optional otherwise. */
|
||||
static std::optional<SharedResourcePointer> getSharedObjectWithoutCreating()
|
||||
{
|
||||
if (auto sharedPtr = weak().lock())
|
||||
return SharedResourcePointer { std::move (sharedPtr) };
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
explicit SharedResourcePointer (std::shared_ptr<SharedObjectType>&& other) noexcept
|
||||
: sharedObject (std::move (other))
|
||||
{
|
||||
jassert (sharedObject != nullptr);
|
||||
}
|
||||
|
||||
class Weak
|
||||
{
|
||||
public:
|
||||
std::shared_ptr<SharedObjectType> lock()
|
||||
{
|
||||
const SpinLock::ScopedLockType lock { mutex };
|
||||
return ptr.lock();
|
||||
}
|
||||
|
||||
std::shared_ptr<SharedObjectType> lockOrCreate()
|
||||
{
|
||||
const SpinLock::ScopedLockType lock { mutex };
|
||||
|
||||
if (auto locked = ptr.lock())
|
||||
return locked;
|
||||
|
||||
const std::shared_ptr<SharedObjectType> shared (new SharedObjectType());
|
||||
ptr = shared;
|
||||
return shared;
|
||||
}
|
||||
|
||||
private:
|
||||
SpinLock mutex;
|
||||
std::weak_ptr<SharedObjectType> ptr;
|
||||
};
|
||||
|
||||
static inline Weak& weak()
|
||||
{
|
||||
static Weak weak;
|
||||
return weak;
|
||||
}
|
||||
|
||||
std::shared_ptr<SharedObjectType> sharedObject = weak().lockOrCreate();
|
||||
|
||||
// There's no need to assign to a SharedResourcePointer because every
|
||||
// instance of the class is exactly the same!
|
||||
SharedResourcePointer& operator= (const SharedResourcePointer&) = delete;
|
||||
SharedResourcePointer& operator= (SharedResourcePointer&&) noexcept = delete;
|
||||
|
||||
JUCE_LEAK_DETECTOR (SharedResourcePointer)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class SharedResourcePointerTest final : public UnitTest
|
||||
{
|
||||
public:
|
||||
SharedResourcePointerTest()
|
||||
: UnitTest ("SharedResourcePointer", UnitTestCategories::memory) {}
|
||||
|
||||
void runTest() final
|
||||
{
|
||||
beginTest ("Only one instance is created");
|
||||
{
|
||||
static int count = 0;
|
||||
struct CountIncrementer { CountIncrementer() { ++count; } };
|
||||
expect (count == 0);
|
||||
|
||||
const SharedResourcePointer<CountIncrementer> instance1;
|
||||
expect (count == 1);
|
||||
|
||||
const SharedResourcePointer<CountIncrementer> instance2;
|
||||
expect (count == 1);
|
||||
|
||||
expect (&instance1.get() == &instance2.get());
|
||||
}
|
||||
|
||||
beginTest ("The shared object is destroyed when the reference count reaches 0");
|
||||
{
|
||||
static int count = 0;
|
||||
struct ReferenceCounter
|
||||
{
|
||||
ReferenceCounter() { ++count; }
|
||||
~ReferenceCounter() { --count; }
|
||||
};
|
||||
|
||||
expect (count == 0);
|
||||
|
||||
{
|
||||
const SharedResourcePointer<ReferenceCounter> instance1;
|
||||
const SharedResourcePointer<ReferenceCounter> instance2;
|
||||
expect (count == 1);
|
||||
}
|
||||
|
||||
expect (count == 0);
|
||||
}
|
||||
|
||||
beginTest ("getInstanceWithoutCreating()");
|
||||
{
|
||||
struct Object{};
|
||||
|
||||
expect (SharedResourcePointer<Object>::getSharedObjectWithoutCreating() == std::nullopt);
|
||||
|
||||
{
|
||||
const SharedResourcePointer<Object> instance;
|
||||
expect (&SharedResourcePointer<Object>::getSharedObjectWithoutCreating()->get() == &instance.get());
|
||||
}
|
||||
|
||||
expect (SharedResourcePointer<Object>::getSharedObjectWithoutCreating() == std::nullopt);
|
||||
}
|
||||
|
||||
beginTest ("Create objects with private constructors");
|
||||
{
|
||||
class ObjectWithPrivateConstructor
|
||||
{
|
||||
private:
|
||||
ObjectWithPrivateConstructor() = default;
|
||||
friend SharedResourcePointer<ObjectWithPrivateConstructor>;
|
||||
};
|
||||
|
||||
SharedResourcePointer<ObjectWithPrivateConstructor> instance;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static SharedResourcePointerTest sharedResourcePointerTest;
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,294 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Used by the JUCE_DECLARE_SINGLETON macros to manage a static pointer
|
||||
to a singleton instance.
|
||||
|
||||
You generally won't use this directly, but see the macros JUCE_DECLARE_SINGLETON,
|
||||
JUCE_DECLARE_SINGLETON_SINGLETHREADED, JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL,
|
||||
and JUCE_IMPLEMENT_SINGLETON for how it is intended to be used.
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
template <typename Type, typename MutexType, bool onlyCreateOncePerRun>
|
||||
struct SingletonHolder : private MutexType // (inherited so we can use the empty-base-class optimisation)
|
||||
{
|
||||
SingletonHolder() = default;
|
||||
|
||||
~SingletonHolder()
|
||||
{
|
||||
/* The static singleton holder is being deleted before the object that it holds
|
||||
has been deleted. This could mean that you've forgotten to call clearSingletonInstance()
|
||||
in the class's destructor, or have failed to delete it before your app shuts down.
|
||||
If you're having trouble cleaning up your singletons, perhaps consider using the
|
||||
SharedResourcePointer class instead.
|
||||
*/
|
||||
jassert (instance.load() == nullptr);
|
||||
}
|
||||
|
||||
/** Returns the current instance, or creates a new instance if there isn't one. */
|
||||
Type* get()
|
||||
{
|
||||
if (auto* ptr = instance.load())
|
||||
return ptr;
|
||||
|
||||
typename MutexType::ScopedLockType sl (*this);
|
||||
|
||||
if (auto* ptr = instance.load())
|
||||
return ptr;
|
||||
|
||||
auto once = onlyCreateOncePerRun; // (local copy avoids VS compiler warning about this being constant)
|
||||
|
||||
if (once)
|
||||
{
|
||||
static bool createdOnceAlready = false;
|
||||
|
||||
if (createdOnceAlready)
|
||||
{
|
||||
// This means that the doNotRecreateAfterDeletion flag was set
|
||||
// and you tried to create the singleton more than once.
|
||||
jassertfalse;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
createdOnceAlready = true;
|
||||
}
|
||||
|
||||
static bool alreadyInside = false;
|
||||
|
||||
if (alreadyInside)
|
||||
{
|
||||
// This means that your object's constructor has done something which has
|
||||
// ended up causing a recursive loop of singleton creation.
|
||||
jassertfalse;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const ScopedValueSetter<bool> scope (alreadyInside, true);
|
||||
return getWithoutChecking();
|
||||
}
|
||||
|
||||
/** Returns the current instance, or creates a new instance if there isn't one, but doesn't do
|
||||
any locking, or checking for recursion or error conditions.
|
||||
*/
|
||||
Type* getWithoutChecking()
|
||||
{
|
||||
if (auto* p = instance.load())
|
||||
return p;
|
||||
|
||||
auto* newObject = new Type(); // (create into a local so that instance is still null during construction)
|
||||
instance.store (newObject);
|
||||
return newObject;
|
||||
}
|
||||
|
||||
/** Deletes and resets the current instance, if there is one. */
|
||||
void deleteInstance()
|
||||
{
|
||||
typename MutexType::ScopedLockType sl (*this);
|
||||
delete instance.exchange (nullptr);
|
||||
}
|
||||
|
||||
/** Called by the class's destructor to clear the pointer if it is currently set to the given object. */
|
||||
void clear (Type* expectedObject) noexcept
|
||||
{
|
||||
instance.compare_exchange_strong (expectedObject, nullptr);
|
||||
}
|
||||
|
||||
// This must be atomic, otherwise a late call to get() may attempt to read instance while it is
|
||||
// being modified by the very first call to get().
|
||||
std::atomic<Type*> instance { nullptr };
|
||||
};
|
||||
|
||||
/** @cond */
|
||||
#define JUCE_PRIVATE_DECLARE_SINGLETON(Classname, mutex, doNotRecreate, inlineToken, getter) \
|
||||
static inlineToken juce::SingletonHolder<Classname, mutex, doNotRecreate> singletonHolder; \
|
||||
friend juce::SingletonHolder<Classname, mutex, doNotRecreate>; \
|
||||
static Classname* JUCE_CALLTYPE getInstance() { return singletonHolder.getter(); } \
|
||||
static Classname* JUCE_CALLTYPE getInstanceWithoutCreating() noexcept { return singletonHolder.instance; } \
|
||||
static void JUCE_CALLTYPE deleteInstance() noexcept { singletonHolder.deleteInstance(); } \
|
||||
void clearSingletonInstance() noexcept { singletonHolder.clear (this); }
|
||||
/** @endcond */
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Macro to generate the appropriate methods and boilerplate for a singleton class.
|
||||
|
||||
To use this, add the line JUCE_DECLARE_SINGLETON (MyClass, doNotRecreateAfterDeletion)
|
||||
to the class's definition.
|
||||
|
||||
Then put a macro JUCE_IMPLEMENT_SINGLETON (MyClass) along with the class's
|
||||
implementation code.
|
||||
|
||||
It's also a very good idea to also add the call clearSingletonInstance() in your class's
|
||||
destructor, in case it is deleted by other means than deleteInstance()
|
||||
|
||||
Clients can then call the static method MyClass::getInstance() to get a pointer
|
||||
to the singleton, or MyClass::getInstanceWithoutCreating() which will return nullptr if
|
||||
no instance currently exists.
|
||||
|
||||
e.g. @code
|
||||
|
||||
struct MySingleton
|
||||
{
|
||||
MySingleton() {}
|
||||
|
||||
~MySingleton()
|
||||
{
|
||||
// this ensures that no dangling pointers are left when the
|
||||
// singleton is deleted.
|
||||
clearSingletonInstance();
|
||||
}
|
||||
|
||||
JUCE_DECLARE_SINGLETON (MySingleton, false)
|
||||
};
|
||||
|
||||
// ...and this goes in a suitable .cpp file:
|
||||
JUCE_IMPLEMENT_SINGLETON (MySingleton)
|
||||
|
||||
|
||||
// example of usage:
|
||||
auto* m = MySingleton::getInstance(); // creates the singleton if there isn't already one.
|
||||
|
||||
...
|
||||
|
||||
MySingleton::deleteInstance(); // safely deletes the singleton (if it's been created)
|
||||
|
||||
@endcode
|
||||
|
||||
If doNotRecreateAfterDeletion = true, it won't allow the object to be created more
|
||||
than once during the process's lifetime - i.e. after you've created and deleted the
|
||||
object, getInstance() will refuse to create another one. This can be useful to stop
|
||||
objects being accidentally re-created during your app's shutdown code.
|
||||
|
||||
If you know that your object will only be created and deleted by a single thread, you
|
||||
can use the slightly more efficient JUCE_DECLARE_SINGLETON_SINGLETHREADED macro instead
|
||||
of this one.
|
||||
|
||||
@see JUCE_IMPLEMENT_SINGLETON, JUCE_DECLARE_SINGLETON_SINGLETHREADED
|
||||
*/
|
||||
#define JUCE_DECLARE_SINGLETON(Classname, doNotRecreateAfterDeletion) \
|
||||
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::CriticalSection, doNotRecreateAfterDeletion, , get)
|
||||
|
||||
/**
|
||||
The same as JUCE_DECLARE_SINGLETON, but does not require a matching
|
||||
JUCE_IMPLEMENT_SINGLETON definition.
|
||||
*/
|
||||
#define JUCE_DECLARE_SINGLETON_INLINE(Classname, doNotRecreateAfterDeletion) \
|
||||
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::CriticalSection, doNotRecreateAfterDeletion, inline, get)
|
||||
|
||||
//==============================================================================
|
||||
/** This is a counterpart to the JUCE_DECLARE_SINGLETON macros.
|
||||
|
||||
After adding the JUCE_DECLARE_SINGLETON to the class definition, this macro has
|
||||
to be used in the cpp file.
|
||||
|
||||
This macro is not required for singletons declared with the INLINE macros, specifically
|
||||
JUCE_DECLARE_SINGLETON_INLINE, JUCE_DECLARE_SINGLETON_SINGLETHREADED_INLINE, and
|
||||
JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL_INLINE.
|
||||
*/
|
||||
#define JUCE_IMPLEMENT_SINGLETON(Classname) \
|
||||
decltype (Classname::singletonHolder) Classname::singletonHolder;
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Macro to declare member variables and methods for a singleton class.
|
||||
|
||||
This is exactly the same as JUCE_DECLARE_SINGLETON, but doesn't use a critical
|
||||
section to make access to it thread-safe. If you know that your object will
|
||||
only ever be created or deleted by a single thread, then this is a
|
||||
more efficient version to use.
|
||||
|
||||
If doNotRecreateAfterDeletion = true, it won't allow the object to be created more
|
||||
than once during the process's lifetime - i.e. after you've created and deleted the
|
||||
object, getInstance() will refuse to create another one. This can be useful to stop
|
||||
objects being accidentally re-created during your app's shutdown code.
|
||||
|
||||
See the documentation for JUCE_DECLARE_SINGLETON for more information about
|
||||
how to use it. Just like JUCE_DECLARE_SINGLETON you need to also have a
|
||||
corresponding JUCE_IMPLEMENT_SINGLETON statement somewhere in your code.
|
||||
|
||||
@see JUCE_IMPLEMENT_SINGLETON, JUCE_DECLARE_SINGLETON, JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL
|
||||
*/
|
||||
#define JUCE_DECLARE_SINGLETON_SINGLETHREADED(Classname, doNotRecreateAfterDeletion) \
|
||||
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::DummyCriticalSection, doNotRecreateAfterDeletion, , get)
|
||||
|
||||
/**
|
||||
The same as JUCE_DECLARE_SINGLETON_SINGLETHREADED, but does not require a matching
|
||||
JUCE_IMPLEMENT_SINGLETON definition.
|
||||
*/
|
||||
#define JUCE_DECLARE_SINGLETON_SINGLETHREADED_INLINE(Classname, doNotRecreateAfterDeletion) \
|
||||
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::DummyCriticalSection, doNotRecreateAfterDeletion, inline, get)
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Macro to declare member variables and methods for a singleton class.
|
||||
|
||||
This is like JUCE_DECLARE_SINGLETON_SINGLETHREADED, but doesn't do any checking
|
||||
for recursion or repeated instantiation. It's intended for use as a lightweight
|
||||
version of a singleton, where you're using it in very straightforward
|
||||
circumstances and don't need the extra checking.
|
||||
|
||||
See the documentation for JUCE_DECLARE_SINGLETON for more information about
|
||||
how to use it. Just like JUCE_DECLARE_SINGLETON you need to also have a
|
||||
corresponding JUCE_IMPLEMENT_SINGLETON statement somewhere in your code.
|
||||
|
||||
@see JUCE_IMPLEMENT_SINGLETON, JUCE_DECLARE_SINGLETON
|
||||
*/
|
||||
#define JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL(Classname) \
|
||||
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::DummyCriticalSection, false, , getWithoutChecking)
|
||||
|
||||
/**
|
||||
The same as JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL, but does not require a matching
|
||||
JUCE_IMPLEMENT_SINGLETON definition.
|
||||
*/
|
||||
#define JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL_INLINE(Classname) \
|
||||
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::DummyCriticalSection, false, inline, getWithoutChecking)
|
||||
|
||||
//==============================================================================
|
||||
/** @cond */
|
||||
// These are ancient macros, and have now been updated with new names to match the JUCE style guide,
|
||||
// so please update your code to use the newer versions!
|
||||
#define juce_DeclareSingleton(Classname, doNotRecreate) JUCE_DECLARE_SINGLETON(Classname, doNotRecreate)
|
||||
#define juce_DeclareSingleton_SingleThreaded(Classname, doNotRecreate) JUCE_DECLARE_SINGLETON_SINGLETHREADED(Classname, doNotRecreate)
|
||||
#define juce_DeclareSingleton_SingleThreaded_Minimal(Classname) JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL(Classname)
|
||||
#define juce_ImplementSingleton(Classname) JUCE_IMPLEMENT_SINGLETON(Classname)
|
||||
#define juce_ImplementSingleton_SingleThreaded(Classname) JUCE_IMPLEMENT_SINGLETON(Classname)
|
||||
/** @endcond */
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-9-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
This class acts as a pointer which will automatically become null if the object
|
||||
to which it points is deleted.
|
||||
|
||||
To accomplish this, the source object needs to cooperate by performing a couple of simple tasks.
|
||||
It must embed a WeakReference::Master object, which stores a shared pointer object, and must clear
|
||||
this master pointer in its destructor.
|
||||
|
||||
Note that WeakReference is not designed to be thread-safe, so if you're accessing it from
|
||||
different threads, you'll need to do your own locking around all uses of the pointer and
|
||||
the object it refers to.
|
||||
|
||||
E.g.
|
||||
@code
|
||||
class MyObject
|
||||
{
|
||||
public:
|
||||
MyObject() {}
|
||||
|
||||
~MyObject()
|
||||
{
|
||||
// This will zero all the references - you need to call this in your destructor.
|
||||
masterReference.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
// You need to embed a variable of this type, with the name "masterReference" inside your object. If the
|
||||
// variable is not public, you should make your class a friend of WeakReference<MyObject> so that the
|
||||
// WeakReference class can access it.
|
||||
WeakReference<MyObject>::Master masterReference;
|
||||
friend class WeakReference<MyObject>;
|
||||
};
|
||||
|
||||
OR: just use the handy JUCE_DECLARE_WEAK_REFERENCEABLE macro to do all this for you.
|
||||
|
||||
// Here's an example of using a pointer.
|
||||
|
||||
auto* n = new MyObject();
|
||||
WeakReference<MyObject> myObjectRef = n;
|
||||
|
||||
auto pointer1 = myObjectRef.get(); // returns a valid pointer to 'n'
|
||||
delete n;
|
||||
auto pointer2 = myObjectRef.get(); // now returns nullptr
|
||||
@endcode
|
||||
|
||||
@see WeakReference::Master
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
template <class ObjectType, class ReferenceCountingType = ReferenceCountedObject>
|
||||
class WeakReference
|
||||
{
|
||||
public:
|
||||
/** Creates a null WeakReference. */
|
||||
inline WeakReference() = default;
|
||||
|
||||
/** Creates a WeakReference that points at the given object. */
|
||||
WeakReference (ObjectType* object) : holder (getRef (object)) {}
|
||||
|
||||
/** Creates a copy of another WeakReference. */
|
||||
WeakReference (const WeakReference& other) noexcept : holder (other.holder) {}
|
||||
|
||||
/** Move constructor */
|
||||
WeakReference (WeakReference&& other) noexcept : holder (std::move (other.holder)) {}
|
||||
|
||||
/** Copies another pointer to this one. */
|
||||
WeakReference& operator= (const WeakReference& other) { holder = other.holder; return *this; }
|
||||
|
||||
/** Copies another pointer to this one. */
|
||||
WeakReference& operator= (ObjectType* newObject) { holder = getRef (newObject); return *this; }
|
||||
|
||||
/** Move assignment operator */
|
||||
WeakReference& operator= (WeakReference&& other) noexcept { holder = std::move (other.holder); return *this; }
|
||||
|
||||
/** Returns the object that this pointer refers to, or null if the object no longer exists. */
|
||||
ObjectType* get() const noexcept { return holder != nullptr ? holder->get() : nullptr; }
|
||||
|
||||
/** Returns the object that this pointer refers to, or null if the object no longer exists. */
|
||||
operator ObjectType*() const noexcept { return get(); }
|
||||
|
||||
/** Returns the object that this pointer refers to, or null if the object no longer exists. */
|
||||
ObjectType* operator->() const noexcept { return get(); }
|
||||
|
||||
/** This returns true if this reference has been pointing at an object, but that object has
|
||||
since been deleted.
|
||||
|
||||
If this reference was only ever pointing at a null pointer, this will return false. Using
|
||||
operator=() to make this refer to a different object will reset this flag to match the status
|
||||
of the reference from which you're copying.
|
||||
*/
|
||||
bool wasObjectDeleted() const noexcept { return holder != nullptr && holder->get() == nullptr; }
|
||||
|
||||
//==============================================================================
|
||||
/** This class is used internally by the WeakReference class - don't use it directly
|
||||
in your code!
|
||||
@see WeakReference
|
||||
*/
|
||||
class SharedPointer : public ReferenceCountingType
|
||||
{
|
||||
public:
|
||||
explicit SharedPointer (ObjectType* obj) noexcept : owner (obj) {}
|
||||
|
||||
inline ObjectType* get() const noexcept { return owner; }
|
||||
void clearPointer() noexcept { owner = nullptr; }
|
||||
|
||||
private:
|
||||
ObjectType* owner;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (SharedPointer)
|
||||
};
|
||||
|
||||
using SharedRef = ReferenceCountedObjectPtr<SharedPointer>;
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
This class is embedded inside an object to which you want to attach WeakReference pointers.
|
||||
See the WeakReference class notes for an example of how to use this class.
|
||||
@see WeakReference
|
||||
*/
|
||||
class Master
|
||||
{
|
||||
public:
|
||||
Master() = default;
|
||||
|
||||
~Master() noexcept
|
||||
{
|
||||
// You must remember to call clear() in your source object's destructor! See the notes
|
||||
// for the WeakReference class for an example of how to do this.
|
||||
jassert (sharedPointer == nullptr || sharedPointer->get() == nullptr);
|
||||
}
|
||||
|
||||
/** The first call to this method will create an internal object that is shared by all weak
|
||||
references to the object.
|
||||
*/
|
||||
SharedRef getSharedPointer (ObjectType* object)
|
||||
{
|
||||
if (sharedPointer == nullptr)
|
||||
{
|
||||
sharedPointer = *new SharedPointer (object);
|
||||
}
|
||||
else
|
||||
{
|
||||
// You're trying to create a weak reference to an object that has already been deleted!!
|
||||
jassert (sharedPointer->get() != nullptr);
|
||||
}
|
||||
|
||||
return sharedPointer;
|
||||
}
|
||||
|
||||
/** The object that owns this master pointer should call this before it gets destroyed,
|
||||
to zero all the references to this object that may be out there. See the WeakReference
|
||||
class notes for an example of how to do this.
|
||||
*/
|
||||
void clear() noexcept
|
||||
{
|
||||
if (sharedPointer != nullptr)
|
||||
sharedPointer->clearPointer();
|
||||
}
|
||||
|
||||
/** Returns the number of WeakReferences that are out there pointing to this object. */
|
||||
int getNumActiveWeakReferences() const noexcept
|
||||
{
|
||||
return sharedPointer == nullptr ? 0 : (sharedPointer->getReferenceCount() - 1);
|
||||
}
|
||||
|
||||
private:
|
||||
SharedRef sharedPointer;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (Master)
|
||||
};
|
||||
|
||||
private:
|
||||
SharedRef holder;
|
||||
|
||||
static SharedRef getRef (ObjectType* o)
|
||||
{
|
||||
if (o != nullptr)
|
||||
return o->masterReference.getSharedPointer (o);
|
||||
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Macro to easily allow a class to be made weak-referenceable.
|
||||
This can be inserted in a class definition to add the requisite weak-ref boilerplate to that class.
|
||||
e.g.
|
||||
|
||||
@code
|
||||
class MyObject
|
||||
{
|
||||
public:
|
||||
MyObject();
|
||||
~MyObject();
|
||||
|
||||
private:
|
||||
JUCE_DECLARE_WEAK_REFERENCEABLE (MyObject)
|
||||
};
|
||||
@endcode
|
||||
|
||||
@see WeakReference, WeakReference::Master
|
||||
*/
|
||||
#define JUCE_DECLARE_WEAK_REFERENCEABLE(Class) \
|
||||
struct WeakRefMaster : public juce::WeakReference<Class>::Master { ~WeakRefMaster() { this->clear(); } }; \
|
||||
WeakRefMaster masterReference; \
|
||||
friend class juce::WeakReference<Class>; \
|
||||
|
||||
|
||||
} // namespace juce
|
||||
Reference in New Issue
Block a user