Akonadi

cpphelper.cpp
1 /*
2  SPDX-FileCopyrightText: 2017 Daniel Vrátil <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "cpphelper.h"
8 #include "nodetree.h"
9 #include "typehelper.h"
10 
11 #include <QHash>
12 #include <QMap>
13 #include <QMetaType>
14 #include <QSet>
15 #include <QSharedDataPointer>
16 #include <QSharedPointer>
17 namespace
18 {
19 class Dummy;
20 
21 // FIXME: This is based on hacks and guesses, does not work for generated types
22 // and does not consider alignment. It should be good enough (TM) for our needs,
23 // but it would be nice to make it smarter, for example by looking up type sizes
24 // from the Node tree and understanding enums and QFlags types.
25 size_t typeSize(const QString &typeName)
26 {
27  static QHash<QByteArray, size_t> typeSizeLookup = {{"Scope", sizeof(QSharedDataPointer<Dummy>)},
28  {"ScopeContext", sizeof(QSharedDataPointer<Dummy>)},
29  {"QSharedPointer", sizeof(QSharedPointer<Dummy>)},
30  {"Tristate", sizeof(qint8)},
31  {"Akonadi::Protocol::Attributes", sizeof(QMap<int, Dummy>)},
32  {"QSet", sizeof(QSet<Dummy>)},
33  {"QVector", sizeof(QVector<Dummy>)}};
34 
35  QByteArray tn;
36  // Don't you just loooove hacks?
37  // TODO: Extract underlying type during XML parsing
38  if (typeName.startsWith(QLatin1String("Akonadi::Protocol")) && typeName.endsWith(QLatin1String("Ptr"))) {
39  tn = "QSharedPointer";
40  } else {
41  tn = TypeHelper::isContainer(typeName) ? TypeHelper::containerName(typeName).toUtf8() : typeName.toUtf8();
42  }
43  auto it = typeSizeLookup.find(tn);
44  if (it == typeSizeLookup.end()) {
45 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
46  const auto typeId = QMetaType::type(tn);
47 #else
48  const auto typeId = QMetaType::fromName(tn).id();
49 #endif
50  const int size = QMetaType(typeId).sizeOf();
51  // for types of unknown size int
52  it = typeSizeLookup.insert(tn, size ? size_t(size) : sizeof(int));
53  }
54  return *it;
55 }
56 
57 } // namespace
58 
59 void CppHelper::sortMembers(QVector<PropertyNode const *> &props)
60 {
61  std::sort(props.begin(), props.end(), [](PropertyNode const *lhs, PropertyNode const *rhs) {
62  return typeSize(lhs->type()) > typeSize(rhs->type());
63  });
64 }
65 
66 void CppHelper::sortMembersForSerialization(QVector<PropertyNode const *> &props)
67 {
68  std::sort(props.begin(), props.end(), [](PropertyNode const *lhs, PropertyNode const *rhs) {
69  return lhs->dependencies().isEmpty() > rhs->dependencies().isEmpty();
70  });
71 }
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
int type(const char *typeName)
QVector::iterator begin()
QHash::iterator find(const Key &key)
QHash::iterator insert(const Key &key, const T &value)
int sizeOf(int type)
QByteArray toUtf8() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QVector::iterator end()
QHash::iterator end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:52:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.