swift
mixinhash.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_MIXINHASH_H
7 #define SWIFT_MISC_MIXIN_MIXINHASH_H
8 
9 #include <algorithm>
10 #include <initializer_list>
11 #include <type_traits>
12 #include <utility>
13 
14 #include <QDBusArgument>
15 #include <QHash>
16 #include <QJsonArray>
17 #include <QJsonObject>
18 #include <QJsonValue>
19 #include <QJsonValueRef>
20 #include <QList>
21 #include <QMap>
22 #include <QString>
23 #include <QtDebug>
24 #include <QtGlobal>
25 
26 #include "misc/inheritancetraits.h"
27 #include "misc/metaclass.h"
28 
29 namespace swift::misc
30 {
31  class CEmpty;
32 
33  // Needed so that our qHash overload doesn't hide the qHash overloads in the global namespace.
34  // This will be safe as long as no global qHash has the same signature as ours.
35  // Alternative would be to qualify all our invokations of the global qHash as ::qHash.
37 
38  namespace mixin
39  {
46  template <class Derived>
48  {
49  public:
51  friend size_t qHash(const Derived &value, size_t seed = 0) // clazy:exclude=qhash-namespace
52  {
53  return ::qHash(hashImpl(value), seed);
54  }
55 
56  private:
57  static size_t hashImpl(const Derived &value);
58 
59  template <typename T>
60  static size_t baseHash(const T *base)
61  {
62  return qHash(*base);
63  }
64  static size_t baseHash(const void *);
65  static size_t baseHash(const CEmpty *);
66  };
67 
68  template <class Derived>
69  size_t HashByMetaClass<Derived>::hashImpl(const Derived &value)
70  {
71  size_t hash = baseHash(static_cast<const TBaseOfT<Derived> *>(&value));
72  introspect<Derived>().forEachMember([&](auto member) {
73  if constexpr (!decltype(member)::has(MetaFlags<DisabledForHashing>()))
74  {
75  hash ^= qHash(member.in(value));
76  }
77  });
78  return hash;
79  }
80 
81  template <class Derived>
82  size_t HashByMetaClass<Derived>::baseHash(const void *)
83  {
84  return 0;
85  }
86 
87  template <class Derived>
88  size_t HashByMetaClass<Derived>::baseHash(const CEmpty *)
89  {
90  return 0;
91  }
92  } // namespace mixin
93 } // namespace swift::misc
94 
95 #endif // SWIFT_MISC_MIXIN_MIXINHASH_H
Default base class for CValueObject.
Definition: valueobject.h:76
CRTP class template from which a derived class can inherit common methods dealing with hashing instan...
Definition: mixinhash.h:48
friend size_t qHash(const Derived &value, size_t seed=0)
qHash overload, needed for storing value in a QSet.
Definition: mixinhash.h:51
size_t qHash(const std::string &key, uint seed)
std::string qHash
Definition: metaclass.h:86
Free functions in swift::misc.