swift
Loading...
Searching...
No Matches
logcategorylist.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2014 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_LOGCATEGORYLIST_H
7#define SWIFT_MISC_LOGCATEGORYLIST_H
8
9#include <initializer_list>
10#include <tuple>
11#include <type_traits>
12
13#include <QJsonObject>
14#include <QMetaType>
15#include <QObject>
16#include <QString>
17#include <QStringList>
18#include <QtGlobal>
19
20#include "misc/collection.h"
21#include "misc/logcategories.h"
22#include "misc/logcategory.h"
23#include "misc/sequence.h"
25#include "misc/typetraits.h"
26
27SWIFT_DECLARE_SEQUENCE_MIXINS(swift::misc, CLogCategory, CLogCategoryList)
28
29namespace swift::misc
30{
34 class SWIFT_MISC_EXPORT CLogCategoryList : public CSequence<CLogCategory>, public mixin::MetaType<CLogCategoryList>
35 {
38
39 public:
41 CLogCategoryList() = default;
42
44 CLogCategoryList(const CLogCategory &category) { this->push_back(category); }
45
47 CLogCategoryList(const CSequence<CLogCategory> &other) : CSequence<CLogCategory>(other) {}
48
50 CLogCategoryList(std::nullptr_t) = delete;
51
67 template <typename T>
68 CLogCategoryList(const T *pointer) requires(std::is_class_v<T>) : CLogCategoryList(fromClass(pointer))
69 {}
70
73 {
74 auto copy = *this;
75 copy.push_back(other);
76 return copy;
77 }
78
81 {
82 auto copy = *this;
83 copy.push_back(other);
84 return copy;
85 }
86
89
91 QStringList toQStringList() const;
92
94 QString toQString(bool i18n = false) const;
95
97 static CLogCategoryList fromQStringList(const QStringList &stringList);
98
100 static CLogCategoryList fromQString(const QString &string);
101
103 bool anyStartWith(const QString &prefix) const;
104
106 bool anyEndWith(const QString &suffix) const;
107
109 static void registerMetadata();
110
112 QString convertToQString(bool i18n = false) const;
113
114 private:
115 template <typename T>
116 static CLogCategoryList fromClass(const T *ptr)
117 {
118 static_assert(sizeof(T) > 0, "T must be a complete type, not forward declared");
119 static const auto staticList = [] {
120 CLogCategoryList list;
121 if constexpr (THasGetLogCategories<T>::value)
122 {
123 list.push_back(fromQStringList(T::getLogCategories()));
124 }
125 if constexpr (QMetaTypeId<T>::Defined)
126 {
127 list.push_back(QMetaType(qMetaTypeId<T>()).name());
128 }
129 if constexpr (std::is_base_of_v<QObject, T>)
130 {
131 list.appendCategoriesFromMetaObject(T::staticMetaObject);
132 }
133 return list;
134 }();
135 auto list = staticList;
136 if constexpr (std::is_base_of_v<QObject, T>)
137 {
138 if (ptr)
139 {
140 list.appendCategoriesFromMetaObject(*ptr->metaObject(), T::staticMetaObject);
141 }
142 }
143 else
144 {
145 Q_UNUSED(ptr);
146 }
147 if (list.isEmpty())
148 {
149 return { CLogCategories::uncategorized() };
150 }
151 return list;
152 }
153
154 void appendCategoriesFromMetaObject(const QMetaObject &, const QMetaObject &super = QObject::staticMetaObject);
155 };
156} // namespace swift::misc
157
158Q_DECLARE_METATYPE(swift::misc::CLogCategoryList)
160
161#endif // SWIFT_MISC_LOGCATEGORYLIST_H
Generic ordered container with value semantics.
Definition collection.h:113
static const QString & validation()
Validation.
A log category is an arbitrary string tag which can be attached to log messages.
Definition logcategory.h:28
A sequence of log categories.
CLogCategoryList with(const CLogCategoryList &other) const
Return a copy with some other categories appended.
CLogCategoryList()=default
Empty constructor.
CLogCategoryList withValidation() const
Return a copy with validation category appended.
CLogCategoryList(const CLogCategory &category)
By single element.
CLogCategoryList(const CSequence< CLogCategory > &other)
Copy construct from superclass instance.
CLogCategoryList(std::nullptr_t)=delete
Prevent accidental use of the initializer list constructor.
CLogCategoryList(const T *pointer)
Construct by extracting categories from a class T.
CLogCategoryList with(const CLogCategory &other) const
Return a copy with another category appended.
CSequence()=default
Default constructor.
void push_back(const CLogCategory &value)
Definition sequence.h:308
bool isEmpty() const
Synonym for empty.
Definition sequence.h:285
CRTP class template from which a derived class can inherit common methods dealing with the metatype o...
#define SWIFT_MISC_DECLARE_USING_MIXIN_METATYPE(DERIVED)
When a derived class and a base class both inherit from mixin::MetaType, the derived class uses this ...
Free functions in swift::misc.
void registerMetadata()
Register all relevant metadata in Misc.
#define SWIFT_DECLARE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template declaration of mixins for a CSequence subclass to be placed near the top of the hea...
Definition sequence.h:62
Trait to detect whether a class T has a static member function named getLogCategories.
Definition typetraits.h:84
#define SWIFT_MISC_EXPORT
Export a class or function from the library.