Akonadi

cpphelper.cpp
1 /*
2  Copyright (c) 2017 Daniel Vrátil <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "cpphelper.h"
21 #include "typehelper.h"
22 #include "nodetree.h"
23 
24 #include <QHash>
25 #include <QSharedDataPointer>
26 #include <QMap>
27 #include <QMetaType>
28 #include <QSharedPointer>
29 #include <QSet>
30 namespace {
31 
32 class Dummy;
33 
34 static QHash<QByteArray, size_t> typeSizeLookup = {
35  { "Scope", sizeof(QSharedDataPointer<Dummy>) },
36  { "ScopeContext", sizeof(QSharedDataPointer<Dummy>) },
37  { "QSharedPointer", sizeof(QSharedPointer<Dummy>) },
38  { "Tristate", sizeof(qint8) },
39  { "Akonadi::Protocol::Attributes", sizeof(QMap<int, Dummy>) },
40  { "QSet", sizeof(QSet<Dummy>) },
41  { "QVector", sizeof(QVector<Dummy>) }
42 };
43 
44 // FIXME: This is based on hacks and guesses, does not work for generated types
45 // and does not consider alignment. It should be good enough (TM) for our needs,
46 // but it would be nice to make it smarter, for example by looking up type sizes
47 // from the Node tree and understanding enums and QFlags types.
48 size_t typeSize(const QString &typeName)
49 {
50  QByteArray tn;
51  // Don't you just loooove hacks?
52  // TODO: Extract underlying type during XML parsing
53  if (typeName.startsWith(QLatin1String("Akonadi::Protocol")) &&
54  typeName.endsWith(QLatin1String("Ptr"))) {
55  tn = "QSharedPointer";
56  } else {
57  tn = TypeHelper::isContainer(typeName)
58  ? TypeHelper::containerName(typeName).toUtf8()
59  : typeName.toUtf8();
60  }
61  auto it = typeSizeLookup.find(tn);
62  if (it == typeSizeLookup.end()) {
63  const auto typeId = QMetaType::type(tn);
64  const int size = QMetaType(typeId).sizeOf();
65  // for types of unknown size int
66  it = typeSizeLookup.insert(tn, size ? size_t(size) : sizeof(int));
67  }
68  return *it;
69 }
70 
71 }
72 
73 void CppHelper::sortMembers(QVector<PropertyNode const *> &props)
74 {
75  std::sort(props.begin(), props.end(),
76  [](PropertyNode const *lhs, PropertyNode const *rhs) {
77  return typeSize(lhs->type()) > typeSize(rhs->type());
78  });
79 }
80 
81 void CppHelper::sortMembersForSerialization(QVector<PropertyNode const *> &props)
82 {
83  std::sort(props.begin(), props.end(),
84  [](PropertyNode const *lhs, PropertyNode const *rhs) {
85  return lhs->dependencies().isEmpty() > rhs->dependencies().isEmpty();
86  });
87 }
88 
QHash::iterator insert(const Key &key, const T &value)
QVector::iterator begin()
int type(const char *typeName)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
QHash::iterator find(const Key &key)
Definition: item.h:44
int sizeOf(int type)
QHash::iterator end()
QVector::iterator end()
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:43:37 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.