swift
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"
24 #include "misc/swiftmiscexport.h"
25 #include "misc/typetraits.h"
26 
27 SWIFT_DECLARE_SEQUENCE_MIXINS(swift::misc, CLogCategory, CLogCategoryList)
28 
29 namespace 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 
48 
50  CLogCategoryList(std::nullptr_t) = delete;
51 
67  template <typename T, typename = std::enable_if_t<std::is_class_v<T>>>
69  {}
70 
72  CLogCategoryList with(const CLogCategory &other) const
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) { list.push_back(QMetaType::typeName(qMetaTypeId<T>())); }
126  if constexpr (std::is_base_of_v<QObject, T>)
127  {
128  list.appendCategoriesFromMetaObject(T::staticMetaObject);
129  }
130  return list;
131  }();
132  auto list = staticList;
133  if constexpr (std::is_base_of_v<QObject, T>)
134  {
135  if (ptr) { list.appendCategoriesFromMetaObject(*ptr->metaObject(), T::staticMetaObject); }
136  }
137  else { Q_UNUSED(ptr); }
138  if (list.isEmpty()) { return { CLogCategories::uncategorized() }; }
139  return list;
140  }
141 
142  void appendCategoriesFromMetaObject(const QMetaObject &, const QMetaObject &super = QObject::staticMetaObject);
143  };
144 } // namespace swift::misc
145 
146 Q_DECLARE_METATYPE(swift::misc::CLogCategoryList)
148 
149 #endif // SWIFT_MISC_LOGCATEGORYLIST_H
Generic ordered container with value semantics.
Definition: collection.h:107
static const QString & uncategorized()
Uncategorized.
Definition: logcategories.h:24
static const QString & validation()
Validation.
Definition: logcategories.h:38
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(const T *pointer)
Construct by extracting categories from a class T.
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 with(const CLogCategory &other) const
Return a copy with another category appended.
Generic sequential container with value semantics.
Definition: sequence.h:86
CSequence()=default
Default constructor.
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
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...
Definition: mixinmetatype.h:29
#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.