24 #include "refpointer.h"
26 #include <boost/mpl/if.hpp>
27 #include <boost/type_traits.hpp>
29 #include <QtCore/QString>
30 #include <QtCore/QDebug>
31 #include <QtCore/QSharedData>
44 typedef void (*SetFunction)(
Value & value,
const void *data);
45 typedef void (*GetFunction)(
const Value & value,
void *data);
48 inline ValueVTable(SetFunction s, GetFunction g) : set(s),
get(g) {}
81 explicit Value(
const GValue *gvalue);
103 Value(
const char *val);
104 Value(
const QByteArray & val);
105 Value(
const QString & val);
118 template <
typename T>
119 static inline Value create(
const T & data);
132 template <
typename T>
138 bool isValid()
const;
144 bool canTransformTo(
Type type)
const;
178 template <
typename T> T
get(
bool *ok = NULL)
const;
190 template <
typename T>
void set(
const T & data);
194 inline bool toBool(
bool *ok = NULL)
const {
return get<bool>(ok); }
197 inline char toChar(
bool *ok = NULL)
const {
return get<char>(ok); }
200 inline uchar
toUChar(
bool *ok = NULL)
const {
return get<uchar>(ok); }
203 inline int toInt(
bool *ok = NULL)
const {
return get<int>(ok); }
206 inline uint
toUInt(
bool *ok = NULL)
const {
return get<uint>(ok); }
209 inline long toLong(
bool *ok = NULL)
const {
return get<long>(ok); }
212 inline ulong
toULong(
bool *ok = NULL)
const {
return get<ulong>(ok); }
215 inline qint64
toInt64(
bool *ok = NULL)
const {
return get<qint64>(ok); }
218 inline quint64
toUInt64(
bool *ok = NULL)
const {
return get<quint64>(ok); }
221 inline QByteArray
toByteArray(
bool *ok = NULL)
const {
return get<QByteArray>(ok); }
224 inline QString
toString(
bool *ok = NULL)
const {
return get<QString>(ok); }
236 operator const GValue*()
const;
245 static void registerValueVTable(
Type type,
const ValueVTable & vtable);
248 template <
typename T>
258 void getData(
Type dataType,
void *data)
const;
267 void setData(
Type dataType,
const void *data);
270 QSharedDataPointer<Data> d;
282 template <
typename T>
285 static inline T
get(
const Value & value);
286 static inline void set(
Value & value,
const T & data);
292 template <
typename T>
301 template <
typename T>
307 template <
typename T>
316 }
catch (
const std::exception &) {
324 template <
typename T>
329 }
catch (
const std::exception & e) {
330 qWarning() <<
"QGlib::Value::set:" << e.what();
336 template <
typename T>
340 typename boost::mpl::if_<
345 value.getData(GetType<T>(), &result);
346 return static_cast<T
>(result);
349 template <
typename T>
350 inline void ValueImpl<T>::set(Value & value,
const T & data)
353 typename boost::mpl::if_<
356 >::type dataRef = data;
358 value.setData(GetType<T>(), &dataRef);
364 struct ValueImpl< QFlags<T> >
366 static inline QFlags<T>
get(
const Value & value)
369 value.getData(GetType< QFlags<T> >(), &flags);
370 return QFlags<T>(QFlag(flags));
373 static inline void set(Value & value,
const QFlags<T> & data)
376 value.setData(GetType< QFlags<T> >(), &flags);
383 struct ValueImpl< RefPointer<T> >
385 static inline RefPointer<T>
get(
const Value & value)
387 typename T::CType *gobj;
388 value.getData(GetType<T>(), &gobj);
392 static inline void set(Value & value,
const RefPointer<T> & data)
394 typename T::CType *gobj =
static_cast<typename T::CType*
>(data);
395 value.setData(GetType<T>(), &gobj);
402 struct ValueImpl<const char[N]>
406 static inline void set(Value & value,
const char (&data)[N])
408 QByteArray str = QByteArray::fromRawData(data, N);
409 value.setData(Type::String, &str);
414 struct ValueImpl<char[N]>
418 static inline void set(Value & value,
const char (&data)[N])
420 QByteArray str = QByteArray::fromRawData(data, N);
421 value.setData(Type::String, &str);
428 struct ValueImpl<const char*>
432 static inline void set(Value & value,
const char *data)
434 QByteArray str = QByteArray::fromRawData(data, qstrlen(data));
435 value.setData(Type::String, &str);
442 struct ValueImpl<QString>
444 static inline QString
get(
const Value & value)
447 value.getData(Type::String, &str);
448 return QString::fromUtf8(str);
451 static inline void set(Value & value,
const QString & data)
453 QByteArray str = data.toUtf8();
454 value.setData(Type::String, &str);
461 struct ValueImpl<Value>
463 static inline Value
get(
const Value & value)
468 static inline void set(Value & value,
const Value & data)
477 struct ValueImpl<Error>
479 static inline Error
get(
const Value & value)
482 value.getData(GetType<Error>(), &error);
483 return Error::copy(error);
486 static inline void set(Value & value,
const Error & data)
488 value.setData(GetType<Error>(), static_cast<const GError *>(data));
496 class QTGLIB_EXPORT InvalidValueException :
public std::logic_error
499 inline InvalidValueException()
500 :
std::logic_error(
"This Value instance has not been initialized") {}
503 class QTGLIB_EXPORT InvalidTypeException :
public std::logic_error
506 inline InvalidTypeException(
const std::string & dataType,
const std::string & valueType)
507 :
std::logic_error(
"Unable to handle value type \"" + dataType +
508 "\". This Value instance has been initialized to hold values of type \""
509 + valueType +
"\" and no conversion is possible") {}
512 class QTGLIB_EXPORT UnregisteredTypeException :
public std::logic_error
515 inline UnregisteredTypeException(
const std::string & typeName)
516 :
std::logic_error(
"Unable to handle unregistered type \"" + typeName +
"\"") {}
519 class QTGLIB_EXPORT TransformationFailedException :
public std::runtime_error
522 inline TransformationFailedException(
const std::string & srcTypeName,
523 const std::string & destTypeName)
524 :
std::runtime_error(
"Failed to transform value from type \""
525 + srcTypeName +
"\" to type \"" + destTypeName +
"\"") {}
533 QTGLIB_EXPORT QDebug operator<<(QDebug debug,
const Value & value);
Error toError(bool *ok=NULL) const
uchar toUChar(bool *ok=NULL) const
static Value create(const T &data)
Wrappers for Glib and GObject classes.
char toChar(bool *ok=NULL) const
Wrapper class for GValue.
long toLong(bool *ok=NULL) const
T get(bool *ok=NULL) const
QByteArray toByteArray(bool *ok=NULL) const
QString toString(bool *ok=NULL) const
ulong toULong(bool *ok=NULL) const
qint64 toInt64(bool *ok=NULL) const
int toInt(bool *ok=NULL) const
bool toBool(bool *ok=NULL) const
uint toUInt(bool *ok=NULL) const
static RefPointer< T > wrap(typename T::CType *nativePtr, bool increaseRef=true)
quint64 toUInt64(bool *ok=NULL) const
Wrapper class for GError.