refactor: adapt magic_enum to Qt (#5258)

This commit is contained in:
nerix
2024-03-23 12:22:42 +01:00
committed by GitHub
parent 044d457d20
commit ed20e71db4
25 changed files with 603 additions and 80 deletions
+6 -3
View File
@@ -119,9 +119,12 @@ int c2_register_callback(lua_State *L)
return 0;
}
auto callbackSavedName = QString("c2cb-%1").arg(
magic_enum::enum_name<EventType>(evtType).data());
lua_setfield(L, LUA_REGISTRYINDEX, callbackSavedName.toStdString().c_str());
auto typeName = magic_enum::enum_name(evtType);
std::string callbackSavedName;
callbackSavedName.reserve(5 + typeName.size());
callbackSavedName += "c2cb-";
callbackSavedName += typeName;
lua_setfield(L, LUA_REGISTRYINDEX, callbackSavedName.c_str());
lua_pop(L, 2);
+12 -11
View File
@@ -3,6 +3,7 @@
# include "common/QLogging.hpp"
# include "controllers/commands/CommandController.hpp"
# include "util/QMagicEnum.hpp"
extern "C" {
# include <lua.h>
@@ -26,7 +27,7 @@ PluginMeta::PluginMeta(const QJsonObject &obj)
}
else if (!homepageObj.isUndefined())
{
QString type = magic_enum::enum_name(homepageObj.type()).data();
auto type = qmagicenum::enumName(homepageObj.type());
this->errors.emplace_back(
QString("homepage is defined but is not a string (its type is %1)")
.arg(type));
@@ -38,7 +39,7 @@ PluginMeta::PluginMeta(const QJsonObject &obj)
}
else
{
QString type = magic_enum::enum_name(nameObj.type()).data();
auto type = qmagicenum::enumName(nameObj.type());
this->errors.emplace_back(
QString("name is not a string (its type is %1)").arg(type));
}
@@ -50,7 +51,7 @@ PluginMeta::PluginMeta(const QJsonObject &obj)
}
else
{
QString type = magic_enum::enum_name(descrObj.type()).data();
auto type = qmagicenum::enumName(descrObj.type());
this->errors.emplace_back(
QString("description is not a string (its type is %1)").arg(type));
}
@@ -64,7 +65,7 @@ PluginMeta::PluginMeta(const QJsonObject &obj)
const auto &t = authorsArr.at(i);
if (!t.isString())
{
QString type = magic_enum::enum_name(t.type()).data();
auto type = qmagicenum::enumName(t.type());
this->errors.push_back(
QString("authors element #%1 is not a string (it is a %2)")
.arg(i)
@@ -76,7 +77,7 @@ PluginMeta::PluginMeta(const QJsonObject &obj)
}
else
{
QString type = magic_enum::enum_name(authorsObj.type()).data();
auto type = qmagicenum::enumName(authorsObj.type());
this->errors.emplace_back(
QString("authors is not an array (its type is %1)").arg(type));
}
@@ -88,7 +89,7 @@ PluginMeta::PluginMeta(const QJsonObject &obj)
}
else
{
QString type = magic_enum::enum_name(licenseObj.type()).data();
auto type = qmagicenum::enumName(licenseObj.type());
this->errors.emplace_back(
QString("license is not a string (its type is %1)").arg(type));
}
@@ -109,7 +110,7 @@ PluginMeta::PluginMeta(const QJsonObject &obj)
}
else
{
QString type = magic_enum::enum_name(verObj.type()).data();
auto type = qmagicenum::enumName(verObj.type());
this->errors.emplace_back(
QString("version is not a string (its type is %1)").arg(type));
this->version = semver::version(0, 0, 0);
@@ -119,7 +120,7 @@ PluginMeta::PluginMeta(const QJsonObject &obj)
{
if (!permsObj.isArray())
{
QString type = magic_enum::enum_name(permsObj.type()).data();
auto type = qmagicenum::enumName(permsObj.type());
this->errors.emplace_back(
QString("permissions is not an array (its type is %1)")
.arg(type));
@@ -132,7 +133,7 @@ PluginMeta::PluginMeta(const QJsonObject &obj)
const auto &t = permsArr.at(i);
if (!t.isObject())
{
QString type = magic_enum::enum_name(t.type()).data();
auto type = qmagicenum::enumName(t.type());
this->errors.push_back(QString("permissions element #%1 is not "
"an object (its type is %2)")
.arg(i)
@@ -161,7 +162,7 @@ PluginMeta::PluginMeta(const QJsonObject &obj)
{
if (!tagsObj.isArray())
{
QString type = magic_enum::enum_name(tagsObj.type()).data();
auto type = qmagicenum::enumName(tagsObj.type());
this->errors.emplace_back(
QString("tags is not an array (its type is %1)").arg(type));
return;
@@ -173,7 +174,7 @@ PluginMeta::PluginMeta(const QJsonObject &obj)
const auto &t = tagsArr.at(i);
if (!t.isString())
{
QString type = magic_enum::enum_name(t.type()).data();
auto type = qmagicenum::enumName(t.type());
this->errors.push_back(
QString("tags element #%1 is not a string (its type is %2)")
.arg(i)
+7 -7
View File
@@ -107,14 +107,14 @@ public:
return {};
}
// this uses magic enum to help automatic tooling find usages
auto typeName =
magic_enum::enum_name(lua::api::EventType::CompletionRequested);
std::string cbName;
cbName.reserve(5 + typeName.size());
cbName += "c2cb-";
cbName += typeName;
auto typ =
lua_getfield(this->state_, LUA_REGISTRYINDEX,
QString("c2cb-%1")
.arg(magic_enum::enum_name<lua::api::EventType>(
lua::api::EventType::CompletionRequested)
.data())
.toStdString()
.c_str());
lua_getfield(this->state_, LUA_REGISTRYINDEX, cbName.c_str());
if (typ != LUA_TFUNCTION)
{
lua_pop(this->state_, 1);
+5 -4
View File
@@ -1,6 +1,8 @@
#ifdef CHATTERINO_HAVE_PLUGINS
# include "controllers/plugins/PluginPermission.hpp"
# include "util/QMagicEnum.hpp"
# include <magic_enum/magic_enum.hpp>
# include <QJsonObject>
@@ -11,14 +13,13 @@ PluginPermission::PluginPermission(const QJsonObject &obj)
auto jsontype = obj.value("type");
if (!jsontype.isString())
{
QString tn = magic_enum::enum_name(jsontype.type()).data();
auto tn = qmagicenum::enumName(jsontype.type());
this->errors.emplace_back(QString("permission type is defined but is "
"not a string (its type is %1)")
.arg(tn));
}
auto strtype = jsontype.toString().toStdString();
auto opt = magic_enum::enum_cast<PluginPermission::Type>(
strtype, magic_enum::case_insensitive);
auto opt = qmagicenum::enumCast<PluginPermission::Type>(
jsontype.toString(), qmagicenum::CASE_INSENSITIVE);
if (!opt.has_value())
{
this->errors.emplace_back(QString("permission type is an unknown (%1)")