1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Use more concise stdlib type aliases

This commit is contained in:
reuk 2022-09-08 12:41:54 +01:00
parent 21d87c02c2
commit 7c14c1fcd7
No known key found for this signature in database
GPG key ID: FCB43929F012EE5C
36 changed files with 438 additions and 494 deletions

View file

@ -649,7 +649,7 @@ public:
@see add
*/
template <class OtherArrayType>
typename std::enable_if<! std::is_pointer<OtherArrayType>::value, void>::type
std::enable_if_t<! std::is_pointer_v<OtherArrayType>, void>
addArray (const OtherArrayType& arrayToAddFrom,
int startIndex,
int numElementsToAdd = -1)

View file

@ -105,9 +105,9 @@ class ArrayBaseTests : public UnitTest
using NoncopyableType = ArrayBaseTestsHelpers::NonTriviallyCopyableType;
#if ! (defined(__GNUC__) && __GNUC__ < 5 && ! defined(__clang__))
static_assert (std::is_trivially_copyable<CopyableType>::value,
static_assert (std::is_trivially_copyable_v<CopyableType>,
"Test TriviallyCopyableType is not trivially copyable");
static_assert (! std::is_trivially_copyable<NoncopyableType>::value,
static_assert (! std::is_trivially_copyable_v<NoncopyableType>,
"Test NonTriviallyCopyableType is trivially copyable");
#endif

View file

@ -43,8 +43,8 @@ private:
using ParameterType = typename TypeHelpers::ParameterType<ElementType>::type;
template <class OtherElementType, class OtherCriticalSection>
using AllowConversion = typename std::enable_if<! std::is_same<std::tuple<ElementType, TypeOfCriticalSectionToUse>,
std::tuple<OtherElementType, OtherCriticalSection>>::value>::type;
using AllowConversion = std::enable_if_t<! std::is_same_v<std::tuple<ElementType, TypeOfCriticalSectionToUse>,
std::tuple<OtherElementType, OtherCriticalSection>>>;
public:
//==============================================================================
@ -304,7 +304,7 @@ public:
}
template <class OtherArrayType>
typename std::enable_if<! std::is_pointer<OtherArrayType>::value, int>::type
std::enable_if_t<! std::is_pointer_v<OtherArrayType>, int>
addArray (const OtherArrayType& arrayToAddFrom,
int startIndex, int numElementsToAdd = -1)
{
@ -385,64 +385,49 @@ public:
private:
//==============================================================================
template <typename T>
#if defined(__GNUC__) && __GNUC__ < 5 && ! defined(__clang__)
using IsTriviallyCopyable = std::is_scalar<T>;
static constexpr auto isTriviallyCopyable = std::is_scalar_v<ElementType>;
#else
using IsTriviallyCopyable = std::is_trivially_copyable<T>;
static constexpr auto isTriviallyCopyable = std::is_trivially_copyable_v<ElementType>;
#endif
template <typename T>
using TriviallyCopyableVoid = typename std::enable_if<IsTriviallyCopyable<T>::value, void>::type;
template <typename T>
using NonTriviallyCopyableVoid = typename std::enable_if<! IsTriviallyCopyable<T>::value, void>::type;
//==============================================================================
template <typename T = ElementType>
TriviallyCopyableVoid<T> addArrayInternal (const ElementType* otherElements, int numElements)
template <typename Type>
void addArrayInternal (const Type* otherElements, int numElements)
{
if (numElements > 0)
memcpy (elements + numUsed, otherElements, (size_t) numElements * sizeof (ElementType));
}
template <typename Type, typename T = ElementType>
TriviallyCopyableVoid<T> addArrayInternal (const Type* otherElements, int numElements)
{
auto* start = elements + numUsed;
while (--numElements >= 0)
new (start++) ElementType (*(otherElements++));
}
template <typename Type, typename T = ElementType>
NonTriviallyCopyableVoid<T> addArrayInternal (const Type* otherElements, int numElements)
{
auto* start = elements + numUsed;
while (--numElements >= 0)
new (start++) ElementType (*(otherElements++));
}
//==============================================================================
template <typename T = ElementType>
TriviallyCopyableVoid<T> setAllocatedSizeInternal (int numElements)
{
elements.realloc ((size_t) numElements);
}
template <typename T = ElementType>
NonTriviallyCopyableVoid<T> setAllocatedSizeInternal (int numElements)
{
HeapBlock<ElementType> newElements (numElements);
for (int i = 0; i < numUsed; ++i)
if constexpr (isTriviallyCopyable && std::is_same_v<Type, ElementType>)
{
new (newElements + i) ElementType (std::move (elements[i]));
elements[i].~ElementType();
if (numElements > 0)
memcpy (elements + numUsed, otherElements, (size_t) numElements * sizeof (ElementType));
}
else
{
auto* start = elements + numUsed;
elements = std::move (newElements);
while (--numElements >= 0)
new (start++) ElementType (*(otherElements++));
}
}
//==============================================================================
void setAllocatedSizeInternal (int numElements)
{
if constexpr (isTriviallyCopyable)
{
elements.realloc ((size_t) numElements);
}
else
{
HeapBlock<ElementType> newElements (numElements);
for (int i = 0; i < numUsed; ++i)
{
new (newElements + i) ElementType (std::move (elements[i]));
elements[i].~ElementType();
}
elements = std::move (newElements);
}
}
//==============================================================================
@ -458,99 +443,99 @@ private:
return elements + indexToInsertAt;
}
template <typename T = ElementType>
TriviallyCopyableVoid<T> createInsertSpaceInternal (int indexToInsertAt, int numElements)
void createInsertSpaceInternal (int indexToInsertAt, int numElements)
{
auto* start = elements + indexToInsertAt;
auto numElementsToShift = numUsed - indexToInsertAt;
memmove (start + numElements, start, (size_t) numElementsToShift * sizeof (ElementType));
}
template <typename T = ElementType>
NonTriviallyCopyableVoid<T> createInsertSpaceInternal (int indexToInsertAt, int numElements)
{
auto* end = elements + numUsed;
auto* newEnd = end + numElements;
auto numElementsToShift = numUsed - indexToInsertAt;
for (int i = 0; i < numElementsToShift; ++i)
if constexpr (isTriviallyCopyable)
{
new (--newEnd) ElementType (std::move (*(--end)));
end->~ElementType();
auto* start = elements + indexToInsertAt;
auto numElementsToShift = numUsed - indexToInsertAt;
memmove (start + numElements, start, (size_t) numElementsToShift * sizeof (ElementType));
}
else
{
auto* end = elements + numUsed;
auto* newEnd = end + numElements;
auto numElementsToShift = numUsed - indexToInsertAt;
for (int i = 0; i < numElementsToShift; ++i)
{
new (--newEnd) ElementType (std::move (*(--end)));
end->~ElementType();
}
}
}
//==============================================================================
template <typename T = ElementType>
TriviallyCopyableVoid<T> removeElementsInternal (int indexToRemoveAt, int numElementsToRemove)
void removeElementsInternal (int indexToRemoveAt, int numElementsToRemove)
{
auto* start = elements + indexToRemoveAt;
auto numElementsToShift = numUsed - (indexToRemoveAt + numElementsToRemove);
memmove (start, start + numElementsToRemove, (size_t) numElementsToShift * sizeof (ElementType));
}
if constexpr (isTriviallyCopyable)
{
auto* start = elements + indexToRemoveAt;
auto numElementsToShift = numUsed - (indexToRemoveAt + numElementsToRemove);
memmove (start, start + numElementsToRemove, (size_t) numElementsToShift * sizeof (ElementType));
}
else
{
auto numElementsToShift = numUsed - (indexToRemoveAt + numElementsToRemove);
auto* destination = elements + indexToRemoveAt;
auto* source = destination + numElementsToRemove;
template <typename T = ElementType>
NonTriviallyCopyableVoid<T> removeElementsInternal (int indexToRemoveAt, int numElementsToRemove)
{
auto numElementsToShift = numUsed - (indexToRemoveAt + numElementsToRemove);
auto* destination = elements + indexToRemoveAt;
auto* source = destination + numElementsToRemove;
for (int i = 0; i < numElementsToShift; ++i)
moveAssignElement (destination++, std::move (*(source++)));
for (int i = 0; i < numElementsToShift; ++i)
moveAssignElement (destination++, std::move (*(source++)));
for (int i = 0; i < numElementsToRemove; ++i)
(destination++)->~ElementType();
for (int i = 0; i < numElementsToRemove; ++i)
(destination++)->~ElementType();
}
}
//==============================================================================
template <typename T = ElementType>
TriviallyCopyableVoid<T> moveInternal (int currentIndex, int newIndex) noexcept
void moveInternal (int currentIndex, int newIndex) noexcept
{
char tempCopy[sizeof (ElementType)];
memcpy (tempCopy, elements + currentIndex, sizeof (ElementType));
if (newIndex > currentIndex)
if constexpr (isTriviallyCopyable)
{
memmove (elements + currentIndex,
elements + currentIndex + 1,
(size_t) (newIndex - currentIndex) * sizeof (ElementType));
char tempCopy[sizeof (ElementType)];
memcpy (tempCopy, elements + currentIndex, sizeof (ElementType));
if (newIndex > currentIndex)
{
memmove (elements + currentIndex,
elements + currentIndex + 1,
(size_t) (newIndex - currentIndex) * sizeof (ElementType));
}
else
{
memmove (elements + newIndex + 1,
elements + newIndex,
(size_t) (currentIndex - newIndex) * sizeof (ElementType));
}
memcpy (elements + newIndex, tempCopy, sizeof (ElementType));
}
else
{
memmove (elements + newIndex + 1,
elements + newIndex,
(size_t) (currentIndex - newIndex) * sizeof (ElementType));
}
auto* e = elements + currentIndex;
ElementType tempCopy (std::move (*e));
auto delta = newIndex - currentIndex;
memcpy (elements + newIndex, tempCopy, sizeof (ElementType));
}
template <typename T = ElementType>
NonTriviallyCopyableVoid<T> moveInternal (int currentIndex, int newIndex) noexcept
{
auto* e = elements + currentIndex;
ElementType tempCopy (std::move (*e));
auto delta = newIndex - currentIndex;
if (delta > 0)
{
for (int i = 0; i < delta; ++i)
if (delta > 0)
{
moveAssignElement (e, std::move (*(e + 1)));
++e;
for (int i = 0; i < delta; ++i)
{
moveAssignElement (e, std::move (*(e + 1)));
++e;
}
}
}
else
{
for (int i = 0; i < -delta; ++i)
else
{
moveAssignElement (e, std::move (*(e - 1)));
--e;
for (int i = 0; i < -delta; ++i)
{
moveAssignElement (e, std::move (*(e - 1)));
--e;
}
}
}
moveAssignElement (e, std::move (tempCopy));
moveAssignElement (e, std::move (tempCopy));
}
}
//==============================================================================
@ -569,19 +554,17 @@ private:
}
//==============================================================================
template <typename T = ElementType>
typename std::enable_if<std::is_move_assignable<T>::value, void>::type
moveAssignElement (ElementType* destination, ElementType&& source)
void moveAssignElement (ElementType* destination, ElementType&& source)
{
*destination = std::move (source);
}
template <typename T = ElementType>
typename std::enable_if<! std::is_move_assignable<T>::value, void>::type
moveAssignElement (ElementType* destination, ElementType&& source)
{
destination->~ElementType();
new (destination) ElementType (std::move (source));
if constexpr (std::is_move_assignable_v<ElementType>)
{
*destination = std::move (source);
}
else
{
destination->~ElementType();
new (destination) ElementType (std::move (source));
}
}
void checkSourceIsNotAMember (const ElementType& element)

View file

@ -53,8 +53,8 @@ JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4702)
template <typename Value>
class Optional
{
template <typename> struct IsOptional : std::false_type {};
template <typename T> struct IsOptional<Optional<T>> : std::true_type {};
template <typename> struct IsOptional : std::false_type {};
template <typename T> struct IsOptional<Optional<T>> : std::true_type {};
public:
Optional() = default;

View file

@ -654,11 +654,8 @@ namespace TypeHelpers
@tags{Core}
*/
template <typename Type> struct SmallestFloatType { using type = float; };
#ifndef DOXYGEN
template <> struct SmallestFloatType <double> { using type = double; };
#endif
template <typename Type>
using SmallestFloatType = std::conditional_t<std::is_same_v<Type, double>, double, float>;
/** These templates are designed to take an integer type, and return an unsigned int
version with the same size.

View file

@ -270,7 +270,7 @@ public:
}
/** Scans an array of values for its min and max, and returns these as a Range. */
template <typename Integral, std::enable_if_t<std::is_integral<Integral>::value, int> = 0>
template <typename Integral, std::enable_if_t<std::is_integral_v<Integral>, int> = 0>
static Range findMinAndMax (const ValueType* values, Integral numValues) noexcept
{
if (numValues <= 0)

View file

@ -87,8 +87,8 @@ class HeapBlock
{
private:
template <class OtherElementType>
using AllowConversion = typename std::enable_if<std::is_base_of<typename std::remove_pointer<ElementType>::type,
typename std::remove_pointer<OtherElementType>::type>::value>::type;
using AllowConversion = std::enable_if_t<std::is_base_of_v<std::remove_pointer_t<ElementType>,
std::remove_pointer_t<OtherElementType>>>;
public:
//==============================================================================
@ -107,7 +107,7 @@ public:
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<SizeType, int>::value, int> = 0>
template <typename SizeType, std::enable_if_t<std::is_convertible_v<SizeType, int>, int> = 0>
explicit HeapBlock (SizeType numElements)
: data (static_cast<ElementType*> (std::malloc (static_cast<size_t> (numElements) * sizeof (ElementType))))
{
@ -119,7 +119,7 @@ public:
The initialiseToZero parameter determines whether the new memory should be cleared,
or left uninitialised.
*/
template <typename SizeType, std::enable_if_t<std::is_convertible<SizeType, int>::value, int> = 0>
template <typename SizeType, std::enable_if_t<std::is_convertible_v<SizeType, int>, int> = 0>
HeapBlock (SizeType numElements, bool initialiseToZero)
: data (static_cast<ElementType*> (initialiseToZero
? std::calloc (static_cast<size_t> (numElements), sizeof (ElementType))
@ -152,7 +152,7 @@ public:
/** Converting move constructor.
Only enabled if this is a HeapBlock<Base*> and the other object is a HeapBlock<Derived*>,
where std::is_base_of<Base, Derived>::value == true.
where std::is_base_of_v<Base, Derived> == true.
*/
template <class OtherElementType, bool otherThrowOnFailure, typename = AllowConversion<OtherElementType>>
HeapBlock (HeapBlock<OtherElementType, otherThrowOnFailure>&& other) noexcept
@ -163,7 +163,7 @@ public:
/** 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<Base, Derived>::value == true.
where std::is_base_of_v<Base, Derived> == true.
*/
template <class OtherElementType, bool otherThrowOnFailure, typename = AllowConversion<OtherElementType>>
HeapBlock& operator= (HeapBlock<OtherElementType, otherThrowOnFailure>&& other) noexcept

View file

@ -84,9 +84,10 @@ inline void writeUnaligned (void* dstPtr, Type value) noexcept
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, typename std::enable_if<std::is_pointer<Type>::value, int>::type = 0>
template <typename Type>
inline Type unalignedPointerCast (void* ptr) noexcept
{
static_assert (std::is_pointer_v<Type>);
return reinterpret_cast<Type> (ptr);
}
@ -97,9 +98,10 @@ inline Type unalignedPointerCast (void* ptr) noexcept
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, typename std::enable_if<std::is_pointer<Type>::value, int>::type = 0>
template <typename Type>
inline Type unalignedPointerCast (const void* ptr) noexcept
{
static_assert (std::is_pointer_v<Type>);
return reinterpret_cast<Type> (ptr);
}

View file

@ -30,15 +30,10 @@ namespace detail
using Void = void;
template <typename, typename = void>
struct EqualityComparableToNullptr
: std::false_type {};
constexpr auto equalityComparableToNullptr = false;
template <typename T>
struct EqualityComparableToNullptr<T, Void<decltype (std::declval<T>() != nullptr)>>
: std::true_type {};
template <typename T>
constexpr bool shouldCheckAgainstNullptr = EqualityComparableToNullptr<T>::value;
constexpr auto equalityComparableToNullptr<T, Void<decltype (std::declval<T>() != nullptr)>> = true;
} // namespace detail
#endif
@ -53,23 +48,22 @@ namespace detail
*/
struct NullCheckedInvocation
{
template <typename Callable, typename... Args,
std::enable_if_t<detail::shouldCheckAgainstNullptr<Callable>, int> = 0>
template <typename Callable, typename... Args>
static void invoke (Callable&& fn, Args&&... args)
{
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Waddress")
if constexpr (detail::equalityComparableToNullptr<Callable>)
{
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Waddress")
if (fn != nullptr)
if (fn != nullptr)
fn (std::forward<Args> (args)...);
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
}
else
{
fn (std::forward<Args> (args)...);
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
}
template <typename Callable, typename... Args,
std::enable_if_t<! detail::shouldCheckAgainstNullptr<Callable>, int> = 0>
static void invoke (Callable&& fn, Args&&... args)
{
fn (std::forward<Args> (args)...);
}
}
template <typename... Args>
@ -82,7 +76,7 @@ struct NullCheckedInvocation
Adapted from https://ericniebler.com/2013/08/07/universal-references-and-the-copy-constructo/
*/
template <typename A, typename B>
using DisableIfSameOrDerived = typename std::enable_if_t<! std::is_base_of<A, std::remove_reference_t<B>>::value>;
using DisableIfSameOrDerived = std::enable_if_t<! std::is_base_of_v<A, std::remove_reference_t<B>>>;
/** Copies an object, sets one of the copy's members to the specified value, and then returns the copy. */
template <typename Object, typename OtherObject, typename Member>

View file

@ -37,7 +37,7 @@ struct CFObjectDeleter
};
template <typename CFType>
using CFUniquePtr = std::unique_ptr<typename std::remove_pointer<CFType>::type, CFObjectDeleter<CFType>>;
using CFUniquePtr = std::unique_ptr<std::remove_pointer_t<CFType>, CFObjectDeleter<CFType>>;
template <typename CFType>
struct CFObjectHolder

View file

@ -490,8 +490,8 @@ public:
template <typename ResultType>
struct HexParser
{
static_assert (std::is_unsigned<ResultType>::value, "ResultType must be unsigned because "
"left-shifting a negative value is UB");
static_assert (std::is_unsigned_v<ResultType>, "ResultType must be unsigned because "
"left-shifting a negative value is UB");
template <typename CharPointerType>
static ResultType parse (CharPointerType t) noexcept

View file

@ -106,9 +106,9 @@ private:
// a block of memory here that's big enough to be used internally as a windows
// CRITICAL_SECTION structure.
#if JUCE_64BIT
std::aligned_storage<44, 8>::type lock;
std::aligned_storage_t<44, 8> lock;
#else
std::aligned_storage<24, 8>::type lock;
std::aligned_storage_t<24, 8> lock;
#endif
#else
mutable pthread_mutex_t lock;