00001 /*************************************************************************** 00002 * variant.h 00003 * This file is part of the KDE project 00004 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * This program 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 * You should have received a copy of the GNU Library General Public License 00015 * along with this program; see the file COPYING. If not, write to 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 ***************************************************************************/ 00019 00020 #ifndef KROSS_API_VARIANT_H 00021 #define KROSS_API_VARIANT_H 00022 00023 #include <qstring.h> 00024 #include <qvariant.h> 00025 //#include <kdebug.h> 00026 00027 #include "object.h" 00028 #include "value.h" 00029 #include "exception.h" 00030 00031 namespace Kross { namespace Api { 00032 00033 /** 00034 * Variant value to wrap a QVariant into a \a Kross::Api::Value 00035 * to enable primitive types like strings or numerics. 00036 */ 00037 class Variant : public Value<Variant, QVariant> 00038 { 00039 friend class Value<Variant, QVariant>; 00040 public: 00041 00042 /** 00043 * Constructor. 00044 * 00045 * \param value The initial QVariant-value 00046 * this Variant-Object has. 00047 * \param name The name this Value has. 00048 */ 00049 Variant(const QVariant& value, const QString& name = "variant"); 00050 00051 operator bool () { return getValue().toBool(); } 00052 operator int () { return getValue().toInt(); } 00053 operator uint () { return getValue().toUInt(); } 00054 operator double () { return getValue().toDouble(); } 00055 operator const char* () { return getValue().toString().latin1(); } 00056 operator QString () { return getValue().toString(); } 00057 operator const QString () { return getValue().toString(); } 00058 operator const QString& () { return getValue().asString(); } 00059 operator QCString () { return getValue().toCString(); } 00060 operator const QCString () { return getValue().toCString(); } 00061 operator const QCString& () { return getValue().asCString(); } 00062 operator const QVariant& () { return getValue(); } 00063 00064 /** 00065 * Destructor. 00066 */ 00067 virtual ~Variant(); 00068 00069 /// \see Kross::Api::Object::getClassName() 00070 virtual const QString getClassName() const; 00071 00072 /** 00073 * \return a string representation of the variant. 00074 * 00075 * \see Kross::Api::Object::toString() 00076 */ 00077 virtual const QString toString(); 00078 00079 /** 00080 * \return a more detailed classname for the passed \p object 00081 * variant type. 00082 * 00083 * \throw TypeException If the \p object isn't a valid 00084 * \a Variant instance. 00085 * \param object the variant object we should return a more 00086 * detailed classname for. 00087 * \return If as example the passed \p object is a 00088 * QVariant::String then "Kross::Api::Variant::String" 00089 * will be returned. 00090 */ 00091 static const QString getVariantType(Object::Ptr object); 00092 00093 /** 00094 * Try to convert the given \a Object into 00095 * a QVariant. 00096 * 00097 * \throw TypeException If the convert failed. 00098 * \param object The object to convert. 00099 * \return The to a QVariant converted object. 00100 */ 00101 static const QVariant& toVariant(Object::Ptr object); 00102 00103 /** 00104 * Try to convert the given \a Object into 00105 * a QString. 00106 * 00107 * \throw TypeException If the convert failed. 00108 * \param object The object to convert. 00109 * \return The to a QString converted object. 00110 */ 00111 static const QString toString(Object::Ptr object); 00112 00113 /** 00114 * Try to convert the given \a Object into 00115 * a int. 00116 * 00117 * \throw TypeException If the convert failed. 00118 * \param object The object to convert. 00119 * \return The to a int converted object. 00120 */ 00121 static int toInt(Object::Ptr object); 00122 00123 /** 00124 * Try to convert the given \a Object into 00125 * a uint. 00126 * 00127 * \throw TypeException If the convert failed. 00128 * \param object The object to convert. 00129 * \return The to a uint converted object. 00130 */ 00131 static uint toUInt(Object::Ptr object); 00132 00133 /** 00134 * Try to convert the given \a Object into 00135 * a uint. 00136 * 00137 * \throw TypeException If the convert failed. 00138 * \param object The object to convert. 00139 * \return The to a uint converted object. 00140 */ 00141 static double toDouble(Object::Ptr object); 00142 00143 /** 00144 * Try to convert the given \a Object into 00145 * a Q_LLONG. 00146 * 00147 * \throw TypeException If the convert failed. 00148 * \param object The object to convert. 00149 * \return The to a Q_LLONG converted object. 00150 */ 00151 static Q_LLONG toLLONG(Object::Ptr object); 00152 00153 /** 00154 * Try to convert the given \a Object into 00155 * a Q_ULLONG. 00156 * 00157 * \throw TypeException If the convert failed. 00158 * \param object The object to convert. 00159 * \return The to a Q_ULLONG converted object. 00160 */ 00161 static Q_ULLONG toULLONG(Object::Ptr object); 00162 00163 /** 00164 * Try to convert the given \a Object into 00165 * a boolean value. 00166 * 00167 * \throw TypeException If the convert failed. 00168 * \param object The object to convert. 00169 * \return The to a bool converted object. 00170 */ 00171 static bool toBool(Object::Ptr object); 00172 00173 /** 00174 * Try to convert the given \a Object into 00175 * a QValueList of QVariant's. 00176 * 00177 * \throw TypeException If the convert failed. 00178 * \param object The object to convert. 00179 * \return The to a QValueList converted object. 00180 */ 00181 static QValueList<QVariant> toList(Object::Ptr object); 00182 00183 }; 00184 00185 }} 00186 00187 #endif 00188
1.4.6