feat(eventsub): use custom String type (#5968)
This commit is contained in:
@@ -1,67 +1,227 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/json.hpp>
|
||||
#include <QAnyStringView>
|
||||
#include <QString>
|
||||
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <string_view>
|
||||
|
||||
// NOLINTBEGIN(cppcoreguidelines-pro-type-union-access)
|
||||
// NOLINTBEGIN(bugprone-undefined-memory-manipulation)
|
||||
// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays)
|
||||
namespace chatterino::eventsub::lib {
|
||||
|
||||
template <class>
|
||||
constexpr bool DEPENDENT_FALSE = false; // workaround before CWG2518/P2593R1
|
||||
|
||||
/// String is a struct that holds either an std::string or a QString
|
||||
/// String is a struct that holds either an UTF-8 string or a QString
|
||||
///
|
||||
/// The intended use is for it to receive an std::string that has been built by
|
||||
/// The intended use is for it to receive an UTF-8 string that has been built by
|
||||
/// boost::json, and once it's been passed into a GUI appliciaton, it can use
|
||||
/// the `qt` function to convert the backing string to a QString,
|
||||
/// while we ensure the conversion is only done once.
|
||||
/// The string can always be viewed using \ref view() in Qt6.
|
||||
///
|
||||
/// It uses the small-string-optimization and has `sizeof(QString)` bytes of
|
||||
/// storage it can use without allocating (Qt6: 24B, Qt5: 8B). The stored string
|
||||
/// is **not** null-terminated.
|
||||
struct String {
|
||||
explicit String(std::string &&v)
|
||||
: backingString(std::move(v))
|
||||
constexpr String() noexcept = default;
|
||||
constexpr String(std::string_view sv)
|
||||
: flags(sv.length() & LENGTH_MASK)
|
||||
{
|
||||
if (sv.length() <= SSO_CAPACITY)
|
||||
{
|
||||
std::memcpy(this->storage.inPlace, sv.data(), sv.length());
|
||||
return;
|
||||
}
|
||||
|
||||
this->flags |= ALLOC_BIT;
|
||||
auto *data = new char[sv.length()];
|
||||
std::memcpy(data, sv.data(), sv.length());
|
||||
this->storage.data = data;
|
||||
}
|
||||
|
||||
~String() = default;
|
||||
~String()
|
||||
{
|
||||
if (this->isQt())
|
||||
{
|
||||
this->storage.qt.~QString();
|
||||
}
|
||||
else if (this->isAlloc())
|
||||
{
|
||||
delete[] this->storage.data;
|
||||
}
|
||||
// else: inPlace
|
||||
}
|
||||
|
||||
String(const String &s) = delete;
|
||||
String(String &&s) = default;
|
||||
|
||||
String &operator=(const String &) = delete;
|
||||
String &operator=(String &&) = default;
|
||||
|
||||
String(String &&other) noexcept
|
||||
: storage(std::move(other.storage))
|
||||
, flags(other.flags)
|
||||
{
|
||||
other.flags = 0;
|
||||
}
|
||||
|
||||
String &operator=(String &&other) noexcept
|
||||
{
|
||||
// clear our data (if needed)
|
||||
if (this->isQt())
|
||||
{
|
||||
this->storage.qt.~QString();
|
||||
}
|
||||
else if (this->isAlloc())
|
||||
{
|
||||
delete[] this->storage.data;
|
||||
}
|
||||
|
||||
this->storage = std::move(other.storage);
|
||||
this->flags = other.flags;
|
||||
other.flags = 0;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the string as a QString, modifying the backing string to ensure
|
||||
/// the copy only happens once.
|
||||
QString qt() const
|
||||
{
|
||||
return std::visit(
|
||||
[this](auto &&arg) -> QString {
|
||||
using T = std::decay_t<std::remove_cvref_t<decltype(arg)>>;
|
||||
if constexpr (std::is_same_v<T, std::string>)
|
||||
{
|
||||
auto qtString = QString::fromStdString(arg);
|
||||
this->backingString = qtString;
|
||||
return qtString;
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, QString>)
|
||||
{
|
||||
return arg;
|
||||
}
|
||||
else
|
||||
{
|
||||
static_assert(DEPENDENT_FALSE<T>,
|
||||
"unknown type in variant");
|
||||
}
|
||||
},
|
||||
this->backingString);
|
||||
if (this->isEmpty())
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
if (this->isQt())
|
||||
{
|
||||
return this->storage.qt;
|
||||
}
|
||||
|
||||
// not a QString yet
|
||||
if (this->isAlloc())
|
||||
{
|
||||
auto s = QString::fromUtf8(
|
||||
this->storage.data,
|
||||
static_cast<qsizetype>(this->flags & LENGTH_MASK));
|
||||
delete[] this->storage.data;
|
||||
new (&this->storage.qt) QString(std::move(s));
|
||||
this->flags &= ~ALLOC_BIT;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto s = QString::fromUtf8(
|
||||
this->storage.inPlace,
|
||||
static_cast<qsizetype>(this->flags & LENGTH_MASK));
|
||||
new (&this->storage.qt) QString(std::move(s));
|
||||
}
|
||||
this->flags |= QT_BIT;
|
||||
|
||||
return this->storage.qt;
|
||||
}
|
||||
|
||||
QAnyStringView view() const noexcept
|
||||
{
|
||||
if (this->isQt())
|
||||
{
|
||||
return this->storage.qt;
|
||||
}
|
||||
return {
|
||||
this->isAlloc() ? this->storage.data : this->storage.inPlace,
|
||||
static_cast<qsizetype>(this->flags & LENGTH_MASK),
|
||||
};
|
||||
}
|
||||
|
||||
constexpr bool isEmpty() const noexcept
|
||||
{
|
||||
return this->flags == 0;
|
||||
}
|
||||
|
||||
constexpr bool isQt() const noexcept
|
||||
{
|
||||
return (this->flags & QT_BIT) != 0;
|
||||
}
|
||||
|
||||
constexpr bool isAlloc() const noexcept
|
||||
{
|
||||
return (this->flags & ALLOC_BIT) != 0;
|
||||
}
|
||||
|
||||
constexpr bool isInPlace() const noexcept
|
||||
{
|
||||
return (this->flags & (ALLOC_BIT | QT_BIT)) == 0;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::variant<std::string, QString> backingString;
|
||||
static constexpr size_t QT_BIT = 1ULL << (sizeof(size_t) * 8 - 1);
|
||||
static constexpr size_t ALLOC_BIT = 1ULL << (sizeof(size_t) * 8 - 2);
|
||||
static constexpr size_t LENGTH_MASK = std::min(ALLOC_BIT, QT_BIT) - 1;
|
||||
static constexpr size_t SSO_CAPACITY = sizeof(QString);
|
||||
|
||||
static_assert((LENGTH_MASK & ALLOC_BIT) == 0);
|
||||
static_assert((LENGTH_MASK & QT_BIT) == 0);
|
||||
|
||||
constexpr size_t length() const noexcept
|
||||
{
|
||||
if ((this->flags & QT_BIT) != 0)
|
||||
{
|
||||
return this->storage.qt.length();
|
||||
}
|
||||
return this->flags & LENGTH_MASK;
|
||||
}
|
||||
|
||||
mutable union Storage {
|
||||
constexpr Storage() noexcept
|
||||
{
|
||||
}
|
||||
~Storage() noexcept
|
||||
{
|
||||
}
|
||||
|
||||
Storage(const Storage &) = delete;
|
||||
Storage &operator=(const Storage &) = delete;
|
||||
|
||||
// we can memcpy QStrings as they're relocatable
|
||||
static_assert(QTypeInfo<QString>::isRelocatable != 0);
|
||||
|
||||
Storage(Storage &&other) noexcept
|
||||
{
|
||||
Storage::move(std::addressof(other), this);
|
||||
};
|
||||
Storage &operator=(Storage &&other) noexcept
|
||||
{
|
||||
Storage::move(std::addressof(other), this);
|
||||
return *this;
|
||||
};
|
||||
|
||||
char inPlace[SSO_CAPACITY]{};
|
||||
const char *data;
|
||||
QString qt;
|
||||
|
||||
private:
|
||||
static void move(Storage *from, Storage *to)
|
||||
{
|
||||
// copy `from` -> `to`
|
||||
std::memcpy(to, from, sizeof(Storage));
|
||||
// clear `from`
|
||||
std::memset(from, 0, sizeof(Storage));
|
||||
}
|
||||
} storage;
|
||||
static_assert(sizeof(Storage) == sizeof(QString));
|
||||
|
||||
/// Flags both store the length as well as the current state of the string.
|
||||
/// They're defined as follows (big endian, MSB is first, 64bit):
|
||||
/// ```
|
||||
/// ┌──────┬──────┬─────────┬─────────────────────────────────────────┐
|
||||
/// │Bit(s)│ 63 │ 62 │ 61..0 │
|
||||
/// ├──────┼──────┼─────────┼─────────────────────────────────────────┤
|
||||
/// │Value │ isQt │ isAlloc │ length │
|
||||
/// └──────┴──────┴─────────┴─────────────────────────────────────────┘
|
||||
/// ```
|
||||
mutable size_t flags = 0;
|
||||
};
|
||||
|
||||
boost::json::result_for<String, boost::json::value>::type tag_invoke(
|
||||
boost::json::try_value_to_tag<String>, const boost::json::value &jvRoot);
|
||||
|
||||
} // namespace chatterino::eventsub::lib
|
||||
// NOLINTEND(cppcoreguidelines-avoid-c-arrays)
|
||||
// NOLINTEND(bugprone-undefined-memory-manipulation)
|
||||
// NOLINTEND(cppcoreguidelines-pro-type-union-access)
|
||||
|
||||
Reference in New Issue
Block a user