24 #include "refpointer.h"
25 #include <boost/mpl/if.hpp>
26 #include <boost/type_traits.hpp>
28 #include <QtCore/QString>
29 #include <QtCore/QDebug>
30 #include <QtCore/QSharedData>
43 typedef void (*SetFunction)(
Value & value,
const void *data);
44 typedef void (*GetFunction)(
const Value & value,
void *data);
47 inline ValueVTable(SetFunction s, GetFunction g) : set(s),
get(g) {}
80 explicit Value(
const GValue *gvalue);
102 Value(
const char *val);
103 Value(
const QByteArray & val);
104 Value(
const QString & val);
117 template <
typename T>
118 static inline Value create(
const T & data);
131 template <
typename T>
137 bool isValid()
const;
143 bool canTransformTo(
Type type)
const;
177 template <
typename T> T
get(
bool *ok = NULL)
const;
189 template <
typename T>
void set(
const T & data);
193 inline bool toBool(
bool *ok = NULL)
const {
return get<bool>(ok); }
196 inline char toChar(
bool *ok = NULL)
const {
return get<char>(ok); }
199 inline uchar
toUChar(
bool *ok = NULL)
const {
return get<uchar>(ok); }
202 inline int toInt(
bool *ok = NULL)
const {
return get<int>(ok); }
205 inline uint
toUInt(
bool *ok = NULL)
const {
return get<uint>(ok); }
208 inline long toLong(
bool *ok = NULL)
const {
return get<long>(ok); }
211 inline ulong
toULong(
bool *ok = NULL)
const {
return get<ulong>(ok); }
214 inline qint64
toInt64(
bool *ok = NULL)
const {
return get<qint64>(ok); }
217 inline quint64
toUInt64(
bool *ok = NULL)
const {
return get<quint64>(ok); }
220 inline QByteArray
toByteArray(
bool *ok = NULL)
const {
return get<QByteArray>(ok); }
223 inline QString
toString(
bool *ok = NULL)
const {
return get<QString>(ok); }
233 operator const GValue*()
const;
242 static void registerValueVTable(
Type type,
const ValueVTable & vtable);
245 template <
typename T>
255 void getData(
Type dataType,
void *data)
const;
264 void setData(
Type dataType,
const void *data);
267 QSharedDataPointer<Data> d;
279 template <
typename T>
282 static inline T
get(
const Value & value);
283 static inline void set(
Value & value,
const T & data);
289 template <
typename T>
298 template <
typename T>
304 template <
typename T>
313 }
catch (
const std::exception &) {
321 template <
typename T>
326 }
catch (
const std::exception & e) {
327 qWarning() <<
"QGlib::Value::set:" << e.what();
333 template <
typename T>
337 typename boost::mpl::if_<
342 value.getData(GetType<T>(), &result);
343 return static_cast<T
>(result);
346 template <
typename T>
347 inline void ValueImpl<T>::set(Value & value,
const T & data)
350 typename boost::mpl::if_<
353 >::type dataRef = data;
355 value.setData(GetType<T>(), &dataRef);
361 struct ValueImpl< QFlags<T> >
363 static inline QFlags<T>
get(
const Value & value)
366 value.getData(GetType< QFlags<T> >(), &flags);
367 return QFlags<T>(QFlag(flags));
370 static inline void set(Value & value,
const QFlags<T> & data)
373 value.setData(GetType< QFlags<T> >(), &flags);
380 struct ValueImpl< RefPointer<T> >
382 static inline RefPointer<T>
get(
const Value & value)
384 typename T::CType *gobj;
385 value.getData(GetType<T>(), &gobj);
389 static inline void set(Value & value,
const RefPointer<T> & data)
391 typename T::CType *gobj =
static_cast<typename T::CType*
>(data);
392 value.setData(GetType<T>(), &gobj);
399 struct ValueImpl<const char[N]>
403 static inline void set(Value & value,
const char (&data)[N])
405 QByteArray str = QByteArray::fromRawData(data, N);
406 value.setData(Type::String, &str);
411 struct ValueImpl<char[N]>
415 static inline void set(Value & value,
const char (&data)[N])
417 QByteArray str = QByteArray::fromRawData(data, N);
418 value.setData(Type::String, &str);
425 struct ValueImpl<const char*>
429 static inline void set(Value & value,
const char *data)
431 QByteArray str = QByteArray::fromRawData(data, qstrlen(data));
432 value.setData(Type::String, &str);
439 struct ValueImpl<QString>
441 static inline QString
get(
const Value & value)
444 value.getData(Type::String, &str);
445 return QString::fromUtf8(str);
448 static inline void set(Value & value,
const QString & data)
450 QByteArray str = data.toUtf8();
451 value.setData(Type::String, &str);
458 struct ValueImpl<Value>
460 static inline Value
get(
const Value & value)
465 static inline void set(Value & value,
const Value & data)
475 class QTGLIB_EXPORT InvalidValueException :
public std::logic_error
478 inline InvalidValueException()
479 : std::logic_error(
"This Value instance has not been initialized") {}
482 class QTGLIB_EXPORT InvalidTypeException :
public std::logic_error
485 inline InvalidTypeException(
const std::string & dataType,
const std::string & valueType)
486 : std::logic_error(
"Unable to handle value type \"" + dataType +
487 "\". This Value instance has been initialized to hold values of type \""
488 + valueType +
"\" and no conversion is possible") {}
491 class QTGLIB_EXPORT UnregisteredTypeException :
public std::logic_error
494 inline UnregisteredTypeException(
const std::string & typeName)
495 : std::logic_error(
"Unable to handle unregistered type \"" + typeName +
"\"") {}
498 class QTGLIB_EXPORT TransformationFailedException :
public std::runtime_error
501 inline TransformationFailedException(
const std::string & srcTypeName,
502 const std::string & destTypeName)
503 : std::runtime_error(
"Failed to transform value from type \""
504 + srcTypeName +
"\" to type \"" + destTypeName +
"\"") {}
512 QTGLIB_EXPORT QDebug operator<<(QDebug debug,
const Value & value);