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::mixin
18 {
25  template <class Derived>
26  class MetaType
27  {
28  public:
30  static void registerMetadata();
31 
35  int getMetaTypeId() const;
36 
39 
42  bool isA(int metaTypeId) const;
43 
44  private:
45  const Derived *derived() const;
46  Derived *derived();
47 
48  template <typename Base2>
49  static bool baseIsA(const Base2 *base, int metaTypeId);
50  static bool baseIsA(const void *, int);
51  };
52 
53  template <class Derived>
55  {
56  private_ns::MetaTypeHelper<Derived>::maybeRegisterMetaType();
57  }
58 
59  template <class Derived>
61  {
62  return private_ns::MetaTypeHelper<Derived>::maybeGetMetaTypeId();
63  }
64 
65  template <class Derived>
67  {
68  return QMetaType(getMetaTypeId()).name();
69  }
70 
71  template <class Derived>
72  bool MetaType<Derived>::isA(int metaTypeId) const
73  {
74  if (metaTypeId == QMetaType::UnknownType) { return false; }
75  if (metaTypeId == getMetaTypeId()) { return true; }
76  return baseIsA(static_cast<const TMetaBaseOfT<Derived> *>(derived()), metaTypeId);
77  }
78 
79  template <class Derived>
80  const Derived *MetaType<Derived>::derived() const
81  {
82  return static_cast<const Derived *>(this);
83  }
84 
85  template <class Derived>
86  Derived *MetaType<Derived>::derived()
87  {
88  return static_cast<Derived *>(this);
89  }
90 
91  template <class Derived>
92  template <typename Base2>
93  bool MetaType<Derived>::baseIsA(const Base2 *base, int metaTypeId)
94  {
95  return base->isA(metaTypeId);
96  }
97 
98  template <class Derived>
99  bool MetaType<Derived>::baseIsA(const void *, int)
100  {
101  return false;
102  }
103 
108 #define SWIFT_MISC_DECLARE_USING_MIXIN_METATYPE(DERIVED) \
109  using ::swift::misc::mixin::MetaType<DERIVED>::registerMetadata; \
110  using ::swift::misc::mixin::MetaType<DERIVED>::getMetaTypeId; \
111  using ::swift::misc::mixin::MetaType<DERIVED>::getClassName; \
112  using ::swift::misc::mixin::MetaType<DERIVED>::isA;
113 
114 } // namespace swift::misc::mixin
115 
116 #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:27
int getMetaTypeId() const
Returns the Qt meta type ID of this object.
Definition: mixinmetatype.h:60
QString getClassName() const
Class name.
Definition: mixinmetatype.h:66
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:72
static void registerMetadata()
Register metadata.
Definition: mixinmetatype.h:54
Mixin classes which implement common operations for value classes.
typename TMetaBaseOf< T >::type TMetaBaseOfT
Alias for typename TMetaBaseOf<T>::type.
const char * name() const const