File indexing completed on 2024-06-16 06:57:50

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2006 Thomas Braxton <brax108@cox.net>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KSERVICE_CONVERSION_CHECK_P_H
0009 #define KSERVICE_CONVERSION_CHECK_P_H
0010 
0011 #include <QDate>
0012 #include <QPoint>
0013 #include <QRect>
0014 #include <QSize>
0015 #include <QString>
0016 #include <QUuid>
0017 #include <QVariant>
0018 
0019 class QColor;
0020 class QFont;
0021 
0022 // Copied from kconfigconversioncheck_p.h in KConfig
0023 namespace KServiceConversionCheck
0024 {
0025 // used to distinguish between supported/unsupported types
0026 struct supported {
0027 };
0028 struct unsupported {
0029 };
0030 
0031 // traits type class to define support for constraints
0032 template<typename T>
0033 struct QVconvertible {
0034     typedef unsupported toQString;
0035     typedef unsupported toQVariant;
0036 };
0037 
0038 // constraint classes
0039 template<typename T>
0040 struct type_toQString {
0041     void constraint()
0042     {
0043         supported x = y;
0044         Q_UNUSED(x);
0045     }
0046     typename QVconvertible<T>::toQString y;
0047 };
0048 
0049 template<typename T>
0050 struct type_toQVariant {
0051     void constraint()
0052     {
0053         supported x = y;
0054         Q_UNUSED(x);
0055     }
0056     typename QVconvertible<T>::toQVariant y;
0057 };
0058 
0059 // check if T is convertible to QString thru QVariant
0060 // if not supported can't be used in QList<T> functions
0061 template<typename T>
0062 inline void to_QString()
0063 {
0064     void (type_toQString<T>::*x)() = &type_toQString<T>::constraint;
0065     Q_UNUSED(x);
0066 }
0067 
0068 // check if T is convertible to QVariant & supported in readEntry/writeEntry
0069 template<typename T>
0070 inline void to_QVariant()
0071 {
0072     void (type_toQVariant<T>::*x)() = &type_toQVariant<T>::constraint;
0073     Q_UNUSED(x);
0074 }
0075 
0076 // define for all types handled in readEntry/writeEntry
0077 // string_support - is supported by QVariant(type).toString(),
0078 //                  can be used in QList<T> functions
0079 // variant_support - has a QVariant constructor
0080 #define QVConversions(type, string_support, variant_support)                                                                                                   \
0081     template<>                                                                                                                                                 \
0082     struct QVconvertible<type> {                                                                                                                               \
0083         typedef string_support toQString;                                                                                                                      \
0084         typedef variant_support toQVariant;                                                                                                                    \
0085     }
0086 
0087 // The only types needed here are the types handled in readEntry/writeEntry
0088 // the default QVconvertible will take care of the rest.
0089 QVConversions(bool, supported, supported);
0090 QVConversions(int, supported, supported);
0091 QVConversions(unsigned int, supported, supported);
0092 QVConversions(long long, supported, supported);
0093 QVConversions(unsigned long long, supported, supported);
0094 QVConversions(float, supported, supported);
0095 QVConversions(double, supported, supported);
0096 QVConversions(QString, supported, supported);
0097 QVConversions(QColor, unsupported, supported);
0098 QVConversions(QFont, supported, supported);
0099 QVConversions(QDateTime, unsupported, supported);
0100 QVConversions(QDate, unsupported, supported);
0101 QVConversions(QSize, unsupported, supported);
0102 QVConversions(QRect, unsupported, supported);
0103 QVConversions(QPoint, unsupported, supported);
0104 QVConversions(QSizeF, unsupported, supported);
0105 QVConversions(QRectF, unsupported, supported);
0106 QVConversions(QPointF, unsupported, supported);
0107 QVConversions(QByteArray, supported, supported);
0108 QVConversions(QStringList, unsupported, supported);
0109 QVConversions(QVariantList, unsupported, supported);
0110 QVConversions(QUrl, supported, supported);
0111 QVConversions(QList<QUrl>, unsupported, supported);
0112 QVConversions(QUuid, supported, supported);
0113 }
0114 
0115 #endif