Akonadi

cpphelper.cpp
1/*
2 SPDX-FileCopyrightText: 2017 Daniel Vrátil <dvratil@kde.og>
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>
17namespace
18{
19class 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.
25size_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 {"QList", sizeof(QList<Dummy>)}};
34
35 QByteArray tn;
36 // Don't you just loooove hacks?
37 // TODO: Extract underlying type during XML parsing
38 if (typeName.startsWith(QLatin1StringView("Akonadi::Protocol")) && typeName.endsWith(QLatin1StringView("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::fromName(tn).id();
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
55void CppHelper::sortMembers(QList<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
62void CppHelper::sortMembersForSerialization(QList<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}
QString typeName(const QJsonObject &obj)
iterator end()
iterator find(const Key &key)
iterator insert(const Key &key, const T &value)
iterator begin()
iterator end()
int sizeOf(int type)
QMetaType fromName(QByteArrayView typeName)
int id() const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.