mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-05-03 16:25:54 +08:00
[QoI] Support polymorphism when extracting log category from QObject
This commit is contained in:
@@ -20,9 +20,9 @@ struct QMetaObject;
|
|||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
void CLogCategoryList::appendCategoriesFromMetaObject(const QMetaObject &metaObject)
|
void CLogCategoryList::appendCategoriesFromMetaObject(const QMetaObject &metaObject, const QMetaObject &super)
|
||||||
{
|
{
|
||||||
for (auto *meta = &metaObject; meta; meta = meta->superClass())
|
for (auto *meta = &metaObject; meta != &super; meta = meta->superClass())
|
||||||
{
|
{
|
||||||
push_back(meta->className());
|
push_back(meta->className());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ namespace BlackMisc
|
|||||||
* It is legal to pass static_cast<T>(nullptr), but in member functions passing the <tt>this</tt> pointer is easier.
|
* It is legal to pass static_cast<T>(nullptr), but in member functions passing the <tt>this</tt> pointer is easier.
|
||||||
*/
|
*/
|
||||||
template <typename T, typename = std::enable_if_t<std::is_class_v<T>>>
|
template <typename T, typename = std::enable_if_t<std::is_class_v<T>>>
|
||||||
CLogCategoryList(const T *pointer) : CLogCategoryList(fromClass<T>()) { Q_UNUSED(pointer); }
|
CLogCategoryList(const T *pointer) : CLogCategoryList(fromClass(pointer)) {}
|
||||||
|
|
||||||
//! Return a copy with another category appended.
|
//! Return a copy with another category appended.
|
||||||
CLogCategoryList with(const CLogCategory &other) const { auto copy = *this; copy.push_back(other); return copy; }
|
CLogCategoryList with(const CLogCategory &other) const { auto copy = *this; copy.push_back(other); return copy; }
|
||||||
@@ -103,22 +103,28 @@ namespace BlackMisc
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static const CLogCategoryList &fromClass()
|
static CLogCategoryList fromClass(const T *ptr)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(T) > 0, "T must be a complete type, not forward declared");
|
static_assert(sizeof(T) > 0, "T must be a complete type, not forward declared");
|
||||||
static const auto list = []
|
static const auto staticList = []
|
||||||
{
|
{
|
||||||
CLogCategoryList list;
|
CLogCategoryList list;
|
||||||
if constexpr (THasGetLogCategories<T>::value) { list.push_back(fromQStringList(T::getLogCategories())); }
|
if constexpr (THasGetLogCategories<T>::value) { list.push_back(fromQStringList(T::getLogCategories())); }
|
||||||
if constexpr (QMetaTypeId<T>::Defined) { list.push_back(QMetaType::typeName(qMetaTypeId<T>())); }
|
if constexpr (QMetaTypeId<T>::Defined) { list.push_back(QMetaType::typeName(qMetaTypeId<T>())); }
|
||||||
if constexpr (std::is_base_of_v<QObject, T>) { list.appendCategoriesFromMetaObject(T::staticMetaObject); }
|
if constexpr (std::is_base_of_v<QObject, T>) { list.appendCategoriesFromMetaObject(T::staticMetaObject); }
|
||||||
if (list.isEmpty()) { list.push_back(CLogCategories::uncategorized()); }
|
|
||||||
return list;
|
return list;
|
||||||
}();
|
}();
|
||||||
|
auto list = staticList;
|
||||||
|
if constexpr (std::is_base_of_v<QObject, T>)
|
||||||
|
{
|
||||||
|
if (ptr) { list.appendCategoriesFromMetaObject(*ptr->metaObject(), T::staticMetaObject); }
|
||||||
|
}
|
||||||
|
else { Q_UNUSED(ptr); }
|
||||||
|
if (list.isEmpty()) { return { CLogCategories::uncategorized() }; }
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
void appendCategoriesFromMetaObject(const QMetaObject &);
|
void appendCategoriesFromMetaObject(const QMetaObject &, const QMetaObject &super = QObject::staticMetaObject);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user