KDECore
conversion_check.h
Go to the documentation of this file.
00001 /* 00002 This file is part of the KDE libraries 00003 Copyright (c) 2006 Thomas Braxton <brax108@cox.net> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 00022 #ifndef CONVERSION_CHECK_H 00023 #define CONVERSION_CHECK_H 00024 00025 #include <QtCore/QString> 00026 #include <QtGui/QColor> 00027 #include <QtGui/QFont> 00028 #include <QtCore/QDate> 00029 #include <QtCore/QPoint> 00030 #include <QtCore/QSize> 00031 #include <QtCore/QRect> 00032 #include <kurl.h> 00033 #include <QtCore/QVariant> 00034 00035 namespace ConversionCheck { 00036 00037 // used to distinguish between supported/unsupported types 00038 struct supported { }; 00039 struct unsupported { }; 00040 00041 // traits type class to define support for constraints 00042 template <typename T> 00043 struct QVconvertible 00044 { 00045 typedef unsupported toQString; 00046 typedef unsupported toQVariant; 00047 }; 00048 00049 // constraint classes 00050 template <typename T> 00051 struct type_toQString 00052 { 00053 void constraint() { supported x = y; } 00054 typename QVconvertible<T>::toQString y; 00055 }; 00056 00057 template <typename T> 00058 struct type_toQVariant 00059 { 00060 void constraint() { supported x = y; } 00061 typename QVconvertible<T>::toQVariant y; 00062 }; 00063 00064 00065 // check if T is convertible to QString thru QVariant 00066 // if not supported can't be used in QList<T> functions 00067 template <typename T> 00068 inline void to_QString() 00069 { 00070 void (type_toQString<T>::*x)() = &type_toQString<T>::constraint; 00071 Q_UNUSED(x); 00072 } 00073 00074 // check if T is convertible to QVariant & supported in readEntry/writeEntry 00075 template <typename T> 00076 inline void to_QVariant() 00077 { 00078 void (type_toQVariant<T>::*x)() = &type_toQVariant<T>::constraint; 00079 Q_UNUSED(x); 00080 } 00081 00082 // define for all types handled in readEntry/writeEntry 00083 // string_support - is supported by QVariant(type).toString(), 00084 // can be used in QList<T> functions 00085 // variant_support - has a QVariant constructor 00086 #define QVConversions(type, string_support, variant_support) \ 00087 template <> struct QVconvertible<type> {\ 00088 typedef string_support toQString;\ 00089 typedef variant_support toQVariant;\ 00090 } 00091 00092 // The only types needed here are the types handled in readEntry/writeEntry 00093 // the default QVconvertible will take care of the rest. 00094 QVConversions(bool, supported, supported); 00095 QVConversions(int, supported, supported); 00096 QVConversions(unsigned int, supported, supported); 00097 QVConversions(long long, supported, supported); 00098 QVConversions(unsigned long long, supported, supported); 00099 QVConversions(float, supported, supported); 00100 QVConversions(double, supported, supported); 00101 QVConversions(QString, supported, supported); 00102 QVConversions(QColor, unsupported, supported); 00103 QVConversions(QFont, supported, supported); 00104 QVConversions(QDateTime, unsupported, supported); 00105 QVConversions(QDate, unsupported, supported); 00106 QVConversions(QSize, unsupported, supported); 00107 QVConversions(QRect, unsupported, supported); 00108 QVConversions(QPoint, unsupported, supported); 00109 QVConversions(QSizeF, unsupported, supported); 00110 QVConversions(QRectF, unsupported, supported); 00111 QVConversions(QPointF, unsupported, supported); 00112 QVConversions(QByteArray, supported, supported); 00113 QVConversions(QStringList, unsupported, supported); 00114 QVConversions(QVariantList, unsupported, supported); 00115 QVConversions(KUrl, supported, supported); 00116 QVConversions(KUrl::List, unsupported, supported); 00117 } 00118 00119 #endif 00120
KDE 4.6 API Reference