swift
mixinmetatype.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2013 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_MIXIN_MIXINMETATYPE_H
7 #define SWIFT_MISC_MIXIN_MIXINMETATYPE_H
8 
9 #include <QMetaType>
10 #include <QString>
11 #include <QtGlobal>
12 
13 #include "misc/inheritancetraits.h"
14 #include "misc/propertyindexref.h"
15 #include "misc/variant.h"
16 
17 namespace swift::misc
18 {
19  namespace mixin
20  {
27  template <class Derived>
28  class MetaType
29  {
30  public:
32  static void registerMetadata();
33 
37  int getMetaTypeId() const;
38 
40  QString getClassName() const;
41 
44  bool isA(int metaTypeId) const;
45 
46  private:
47  const Derived *derived() const;
48  Derived *derived();
49 
50  template <typename Base2>
51  static bool baseIsA(const Base2 *base, int metaTypeId);
52  static bool baseIsA(const void *, int);
53  };
54 
55  template <class Derived>
57  {
58  private_ns::MetaTypeHelper<Derived>::maybeRegisterMetaType();
59  }
60 
61  template <class Derived>
63  {
64  return private_ns::MetaTypeHelper<Derived>::maybeGetMetaTypeId();
65  }
66 
67  template <class Derived>
69  {
70  return QMetaType::typeName(getMetaTypeId());
71  }
72 
73  template <class Derived>
74  bool MetaType<Derived>::isA(int metaTypeId) const
75  {
76  if (metaTypeId == QMetaType::UnknownType) { return false; }
77  if (metaTypeId == getMetaTypeId()) { return true; }
78  return baseIsA(static_cast<const TMetaBaseOfT<Derived> *>(derived()), metaTypeId);
79  }
80 
81  template <class Derived>
82  const Derived *MetaType<Derived>::derived() const
83  {
84  return static_cast<const Derived *>(this);
85  }
86 
87  template <class Derived>
88  Derived *MetaType<Derived>::derived()
89  {
90  return static_cast<Derived *>(this);
91  }
92 
93  template <class Derived>
94  template <typename Base2>
95  bool MetaType<Derived>::baseIsA(const Base2 *base, int metaTypeId)
96  {
97  return base->isA(metaTypeId);
98  }
99 
100  template <class Derived>
101  bool MetaType<Derived>::baseIsA(const void *, int)
102  {
103  return false;
104  }
105 
106  // *INDENT-OFF*
111 #define SWIFT_MISC_DECLARE_USING_MIXIN_METATYPE(DERIVED) \
112  using ::swift::misc::mixin::MetaType<DERIVED>::registerMetadata; \
113  using ::swift::misc::mixin::MetaType<DERIVED>::getMetaTypeId; \
114  using ::swift::misc::mixin::MetaType<DERIVED>::getClassName; \
115  using ::swift::misc::mixin::MetaType<DERIVED>::isA;
116  // *INDENT-ON*
117 
118  } // namespace mixin
119 } // namespace swift::misc
120 
121 #endif // SWIFT_MISC_MIXIN_MIXINMETATYPE_H
CRTP class template from which a derived class can inherit common methods dealing with the metatype o...
Definition: mixinmetatype.h:29
int getMetaTypeId() const
Returns the Qt meta type ID of this object.
Definition: mixinmetatype.h:62
QString getClassName() const
Class name.
Definition: mixinmetatype.h:68
bool isA(int metaTypeId) const
Returns true if this object is an instance of the class with the given meta type ID,...
Definition: mixinmetatype.h:74
static void registerMetadata()
Register metadata.
Definition: mixinmetatype.h:56
Free functions in swift::misc.
typename TMetaBaseOf< T >::type TMetaBaseOfT
Alias for typename TMetaBaseOf<T>::type.