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 KCONFIG_CONVERSION_CHECK_P_H |
9 | #define KCONFIG_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 | namespace KConfigConversionCheck |
23 | { |
24 | // used to distinguish between supported/unsupported types |
25 | struct supported { |
26 | }; |
27 | struct unsupported { |
28 | }; |
29 | |
30 | // traits type class to define support for constraints |
31 | template<typename T> |
32 | struct QVconvertible { |
33 | typedef unsupported toQString; |
34 | typedef unsupported toQVariant; |
35 | }; |
36 | |
37 | // constraint classes |
38 | template<typename T> |
39 | struct type_toQString { |
40 | void constraint() |
41 | { |
42 | supported x = y; |
43 | Q_UNUSED(x); |
44 | } |
45 | typename QVconvertible<T>::toQString y; |
46 | }; |
47 | |
48 | template<typename T> |
49 | struct type_toQVariant { |
50 | void constraint() |
51 | { |
52 | supported x = y; |
53 | Q_UNUSED(x); |
54 | } |
55 | typename QVconvertible<T>::toQVariant y; |
56 | }; |
57 | |
58 | // check if T is convertible to QString thru QVariant |
59 | // if not supported can't be used in QList<T> functions |
60 | template<typename T> |
61 | inline void to_QString() |
62 | { |
63 | void (type_toQString<T>::*x)() = &type_toQString<T>::constraint; |
64 | Q_UNUSED(x); |
65 | } |
66 | |
67 | // check if T is convertible to QVariant & supported in readEntry/writeEntry |
68 | template<typename T> |
69 | inline void to_QVariant() |
70 | { |
71 | void (type_toQVariant<T>::*x)() = &type_toQVariant<T>::constraint; |
72 | Q_UNUSED(x); |
73 | } |
74 | |
75 | // define for all types handled in readEntry/writeEntry |
76 | // string_support - is supported by QVariant(type).toString(), |
77 | // can be used in QList<T> functions |
78 | // variant_support - has a QVariant constructor |
79 | #define QVConversions(type, string_support, variant_support) \ |
80 | template<> \ |
81 | struct QVconvertible<type> { \ |
82 | typedef string_support toQString; \ |
83 | typedef variant_support toQVariant; \ |
84 | } |
85 | |
86 | // The only types needed here are the types handled in readEntry/writeEntry |
87 | // the default QVconvertible will take care of the rest. |
88 | QVConversions(bool, supported, supported); |
89 | QVConversions(int, supported, supported); |
90 | QVConversions(unsigned int, supported, supported); |
91 | QVConversions(long long, supported, supported); |
92 | QVConversions(unsigned long long, supported, supported); |
93 | QVConversions(float, supported, supported); |
94 | QVConversions(double, supported, supported); |
95 | QVConversions(QString, supported, supported); |
96 | QVConversions(QColor, unsupported, supported); |
97 | QVConversions(QFont, supported, supported); |
98 | QVConversions(QDateTime, unsupported, supported); |
99 | QVConversions(QDate, unsupported, supported); |
100 | QVConversions(QSize, unsupported, supported); |
101 | QVConversions(QRect, unsupported, supported); |
102 | QVConversions(QPoint, unsupported, supported); |
103 | QVConversions(QSizeF, unsupported, supported); |
104 | QVConversions(QRectF, unsupported, supported); |
105 | QVConversions(QPointF, unsupported, supported); |
106 | QVConversions(QByteArray, supported, supported); |
107 | QVConversions(QStringList, unsupported, supported); |
108 | QVConversions(QVariantList, unsupported, supported); |
109 | QVConversions(QUrl, supported, supported); |
110 | QVConversions(QList<QUrl>, unsupported, supported); |
111 | QVConversions(QUuid, supported, supported); |
112 | } |
113 | |
114 | #endif |
115 | |