Utils: Adapt SmallString to C++ 17

if constexpr makes the code a little bit more readable.

Change-Id: I7bd508cbc81cf7213dd4ac627f3d4e489ab523c9
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Marco Bubke
2022-07-30 04:45:41 +02:00
parent 2a22e81e72
commit a91e6bd46c

View File

@@ -134,6 +134,12 @@ struct ReferenceLayout
template <uint MaximumShortStringDataAreaSize> template <uint MaximumShortStringDataAreaSize>
struct ShortStringLayout struct ShortStringLayout
{ {
constexpr ShortStringLayout() noexcept = default;
constexpr ShortStringLayout(
typename ControlBlock<MaximumShortStringDataAreaSize>::SizeType shortStringSize) noexcept
: control(shortStringSize, false, false)
{}
ControlBlock<MaximumShortStringDataAreaSize> control; ControlBlock<MaximumShortStringDataAreaSize> control;
char string[MaximumShortStringDataAreaSize]; char string[MaximumShortStringDataAreaSize];
}; };
@@ -154,27 +160,18 @@ struct StringDataLayout {
constexpr StringDataLayout(const char *string, constexpr StringDataLayout(const char *string,
size_type size) noexcept size_type size) noexcept
: reference(string, size, 0) : reference(string, size, 0)
{ {}
}
template<size_type Size, template<size_type Size>
typename std::enable_if_t<Size <= MaximumShortStringDataAreaSize, int> = 0>
constexpr StringDataLayout(const char (&string)[Size]) noexcept constexpr StringDataLayout(const char (&string)[Size]) noexcept
: shortString(ShortStringLayout<MaximumShortStringDataAreaSize>{})
{ {
if constexpr (Size <= MaximumShortStringDataAreaSize) {
shortString = {Size - 1};
for (size_type i = 0; i < Size; ++i) for (size_type i = 0; i < Size; ++i)
shortString.string[i] = string[i]; shortString.string[i] = string[i];
} else {
shortString.control.setShortStringSize(Size - 1); reference = {string, Size - 1, 0};
shortString.control.setIsShortString(true);
shortString.control.setIsReadOnlyReference(false);
} }
template<size_type Size,
typename std::enable_if_t<!(Size <= MaximumShortStringDataAreaSize), int> = 1>
constexpr StringDataLayout(const char(&string)[Size]) noexcept
: reference(string, Size - 1, 0)
{
} }
constexpr static constexpr static