1 | /* |
2 | This file is part of the KDE libraries |
3 | SPDX-FileCopyrightText: 2006 Thomas Braxton <brax108@cox.net> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-or-later |
6 | */ |
7 | |
8 | #ifndef KSERVICE_CONVERSION_CHECK_P_H |
9 | #define KSERVICE_CONVERSION_CHECK_P_H |
10 | |
11 | #include <QDate> |
12 | #include <QPoint> |
13 | #include <QRect> |
14 | #include <QSize> |
15 | #include <QString> |
16 | #include <QUuid> |
17 | #include <QVariant> |
18 | |
19 | class QColor; |
20 | class QFont; |
21 | |
22 | // Copied from kconfigconversioncheck_p.h in KConfig |
23 | namespace KServiceConversionCheck |
24 | { |
25 | // used to distinguish between supported/unsupported types |
26 | struct supported { |
27 | }; |
28 | struct unsupported { |
29 | }; |
30 | |
31 | // traits type class to define support for constraints |
32 | template<typename T> |
33 | struct QVconvertible { |
34 | typedef unsupported toQString; |
35 | typedef unsupported toQVariant; |
36 | }; |
37 | |
38 | // constraint classes |
39 | template<typename T> |
40 | struct type_toQString { |
41 | void constraint() |
42 | { |
43 | supported x = y; |
44 | Q_UNUSED(x); |
45 | } |
46 | typename QVconvertible<T>::toQString y; |
47 | }; |
48 | |
49 | template<typename T> |
50 | struct type_toQVariant { |
51 | void constraint() |
52 | { |
53 | supported x = y; |
54 | Q_UNUSED(x); |
55 | } |
56 | typename QVconvertible<T>::toQVariant y; |
57 | }; |
58 | |
59 | // check if T is convertible to QString thru QVariant |
60 | // if not supported can't be used in QList<T> functions |
61 | template<typename T> |
62 | inline void to_QString() |
63 | { |
64 | void (type_toQString<T>::*x)() = &type_toQString<T>::constraint; |
65 | Q_UNUSED(x); |
66 | } |
67 | |
68 | // check if T is convertible to QVariant & supported in readEntry/writeEntry |
69 | template<typename T> |
70 | inline void to_QVariant() |
71 | { |
72 | void (type_toQVariant<T>::*x)() = &type_toQVariant<T>::constraint; |
73 | Q_UNUSED(x); |
74 | } |
75 | |
76 | // define for all types handled in readEntry/writeEntry |
77 | // string_support - is supported by QVariant(type).toString(), |
78 | // can be used in QList<T> functions |
79 | // variant_support - has a QVariant constructor |
80 | #define QVConversions(type, string_support, variant_support) \ |
81 | template<> \ |
82 | struct QVconvertible<type> { \ |
83 | typedef string_support toQString; \ |
84 | typedef variant_support toQVariant; \ |
85 | } |
86 | |
87 | // The only types needed here are the types handled in readEntry/writeEntry |
88 | // the default QVconvertible will take care of the rest. |
89 | QVConversions(bool, supported, supported); |
90 | QVConversions(int, supported, supported); |
91 | QVConversions(unsigned int, supported, supported); |
92 | QVConversions(long long, supported, supported); |
93 | QVConversions(unsigned long long, supported, supported); |
94 | QVConversions(float, supported, supported); |
95 | QVConversions(double, supported, supported); |
96 | QVConversions(QString, supported, supported); |
97 | QVConversions(QColor, unsupported, supported); |
98 | QVConversions(QFont, supported, supported); |
99 | QVConversions(QDateTime, unsupported, supported); |
100 | QVConversions(QDate, unsupported, supported); |
101 | QVConversions(QSize, unsupported, supported); |
102 | QVConversions(QRect, unsupported, supported); |
103 | QVConversions(QPoint, unsupported, supported); |
104 | QVConversions(QSizeF, unsupported, supported); |
105 | QVConversions(QRectF, unsupported, supported); |
106 | QVConversions(QPointF, unsupported, supported); |
107 | QVConversions(QByteArray, supported, supported); |
108 | QVConversions(QStringList, unsupported, supported); |
109 | QVConversions(QVariantList, unsupported, supported); |
110 | QVConversions(QUrl, supported, supported); |
111 | QVConversions(QList<QUrl>, unsupported, supported); |
112 | QVConversions(QUuid, supported, supported); |
113 | } |
114 | |
115 | #endif |
116 | |