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  const auto typeId = QMetaType::type(tn);
46  const int size = QMetaType(typeId).sizeOf();
47  // for types of unknown size int
48  it = typeSizeLookup.insert(tn, size ? size_t(size) : sizeof(int));
49  }
50  return *it;
51 }
52 
53 } // namespace
54 
55 void CppHelper::sortMembers(QVector<PropertyNode const *> &props)
56 {
57  std::sort(props.begin(), props.end(), [](PropertyNode const *lhs, PropertyNode const *rhs) {
58  return typeSize(lhs->type()) > typeSize(rhs->type());
59  });
60 }
61 
62 void CppHelper::sortMembersForSerialization(QVector<PropertyNode const *> &props)
63 {
64  std::sort(props.begin(), props.end(), [](PropertyNode const *lhs, PropertyNode const *rhs) {
65  return lhs->dependencies().isEmpty() > rhs->dependencies().isEmpty();
66  });
67 }
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-2022 The KDE developers.
Generated on Mon Jun 27 2022 04:01:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.