24 #include "refpointer.h"
26 #include <boost/mpl/if.hpp>
28 #include <boost/type_traits.hpp>
31 #include <QtCore/QString>
32 #include <QtCore/QDebug>
33 #include <QtCore/QSharedData>
46 typedef void (*SetFunction)(
Value & value,
const void *data);
47 typedef void (*GetFunction)(
const Value & value,
void *data);
50 inline ValueVTable(SetFunction s, GetFunction g) : set(s), get(g) {}
83 explicit Value(
const GValue *gvalue);
120 template <
typename T>
121 static inline Value create(
const T & data);
134 template <
typename T>
140 bool isValid()
const;
146 bool canTransformTo(
Type type)
const;
180 template <
typename T> T get(
bool *ok = NULL)
const;
192 template <
typename T>
void set(
const T & data);
196 inline bool toBool(
bool *ok = NULL)
const {
return get<bool>(ok); }
199 inline char toChar(
bool *ok = NULL)
const {
return get<char>(ok); }
202 inline uchar
toUChar(
bool *ok = NULL)
const {
return get<uchar>(ok); }
205 inline int toInt(
bool *ok = NULL)
const {
return get<int>(ok); }
208 inline uint
toUInt(
bool *ok = NULL)
const {
return get<uint>(ok); }
211 inline long toLong(
bool *ok = NULL)
const {
return get<long>(ok); }
214 inline ulong
toULong(
bool *ok = NULL)
const {
return get<ulong>(ok); }
217 inline qint64
toInt64(
bool *ok = NULL)
const {
return get<qint64>(ok); }
220 inline quint64
toUInt64(
bool *ok = NULL)
const {
return get<quint64>(ok); }
223 inline QByteArray
toByteArray(
bool *ok = NULL)
const {
return get<QByteArray>(ok); }
226 inline QString
toString(
bool *ok = NULL)
const {
return get<QString>(ok); }
238 operator const GValue*()
const;
247 static void registerValueVTable(
Type type,
const ValueVTable & vtable);
250 template <
typename T>
260 void getData(
Type dataType,
void *data)
const;
269 void setData(
Type dataType,
const void *data);
272 QSharedDataPointer<Data> d;
284 template <
typename T>
287 static inline T get(
const Value & value);
288 static inline void set(
Value & value,
const T & data);
294 template <
typename T>
303 template <
typename T>
309 template <
typename T>
318 }
catch (
const std::exception &) {
326 template <
typename T>
331 }
catch (
const std::exception & e) {
332 qWarning() <<
"QGlib::Value::set:" << e.what();
338 template <
typename T>
342 typename boost::mpl::if_<
347 value.getData(GetType<T>(), &result);
348 return static_cast<T
>(result);
351 template <
typename T>
352 inline void ValueImpl<T>::set(Value & value,
const T & data)
355 typename boost::mpl::if_<
358 >::type dataRef = data;
360 value.setData(GetType<T>(), &dataRef);
366 struct ValueImpl< QFlags<T> >
368 static inline QFlags<T> get(
const Value & value)
371 value.getData(GetType< QFlags<T> >(), &flags);
372 return QFlags<T>(QFlag(flags));
375 static inline void set(Value & value,
const QFlags<T> & data)
378 value.setData(GetType< QFlags<T> >(), &flags);
385 struct ValueImpl< RefPointer<T> >
387 static inline RefPointer<T> get(
const Value & value)
389 typename T::CType *gobj;
390 value.getData(GetType<T>(), &gobj);
394 static inline void set(Value & value,
const RefPointer<T> & data)
396 typename T::CType *gobj =
static_cast<typename T::CType*
>(data);
397 value.setData(GetType<T>(), &gobj);
404 struct ValueImpl<const char[N]>
408 static inline void set(Value & value,
const char (&data)[N])
410 QByteArray str = QByteArray::fromRawData(data, N);
411 value.setData(Type::String, &str);
416 struct ValueImpl<char[N]>
420 static inline void set(Value & value,
const char (&data)[N])
422 QByteArray str = QByteArray::fromRawData(data, N);
423 value.setData(Type::String, &str);
430 struct ValueImpl<const char*>
434 static inline void set(Value & value,
const char *data)
436 QByteArray str = QByteArray::fromRawData(data, qstrlen(data));
437 value.setData(Type::String, &str);
444 struct ValueImpl<QString>
446 static inline QString get(
const Value & value)
449 value.getData(Type::String, &str);
450 return QString::fromUtf8(str);
453 static inline void set(Value & value,
const QString & data)
455 QByteArray str = data.toUtf8();
456 value.setData(Type::String, &str);
463 struct ValueImpl<Value>
465 static inline Value get(
const Value & value)
470 static inline void set(Value & value,
const Value & data)
479 struct ValueImpl<Error>
481 static inline Error get(
const Value & value)
484 value.getData(GetType<Error>(), &error);
485 return Error::copy(error);
488 static inline void set(Value & value,
const Error & data)
490 value.setData(GetType<Error>(),
static_cast<const GError *
>(data));
498 class QTGLIB_EXPORT InvalidValueException :
public std::logic_error
501 inline InvalidValueException()
502 : std::logic_error(
"This Value instance has not been initialized") {}
505 class QTGLIB_EXPORT InvalidTypeException :
public std::logic_error
508 inline InvalidTypeException(
const std::string & dataType,
const std::string & valueType)
509 : std::logic_error(
"Unable to handle value type \"" + dataType +
510 "\". This Value instance has been initialized to hold values of type \""
511 + valueType +
"\" and no conversion is possible") {}
514 class QTGLIB_EXPORT UnregisteredTypeException :
public std::logic_error
517 inline UnregisteredTypeException(
const std::string & typeName)
518 : std::logic_error(
"Unable to handle unregistered type \"" + typeName +
"\"") {}
521 class QTGLIB_EXPORT TransformationFailedException :
public std::runtime_error
524 inline TransformationFailedException(
const std::string & srcTypeName,
525 const std::string & destTypeName)
526 : std::runtime_error(
"Failed to transform value from type \""
527 + srcTypeName +
"\" to type \"" + destTypeName +
"\"") {}
535 QTGLIB_EXPORT QDebug operator<<(QDebug debug,
const Value & value);
Wrapper class for GError.
static RefPointer< T > wrap(typename T::CType *nativePtr, bool increaseRef=true)
Wrapper class for GValue.
uint toUInt(bool *ok=NULL) const
Value(char val)
Creates a new Value of Type::Char and sets it to hold val.
static Value create(const T &data)
ulong toULong(bool *ok=NULL) const
Value(qint64 val)
Creates a new Value of Type::Int64 and sets it to hold val.
Value(int val)
Creates a new Value of Type::Int and sets it to hold val.
Value(float val)
Creates a new Value of Type::Float and sets it to hold val.
Value(long val)
Creates a new Value of Type::Long and sets it to hold val.
Value(uint val)
Creates a new Value of Type::Uint and sets it to hold val.
Value(const QString &val)
Creates a new Value of Type::String and sets it to hold val.
Value(double val)
Creates a new Value of Type::Double and sets it to hold val.
Error toError(bool *ok=NULL) const
Value(bool val)
Creates a new Value of Type::Bool and sets it to hold val.
int toInt(bool *ok=NULL) const
QByteArray toByteArray(bool *ok=NULL) const
Value(const char *val)
Creates a new Value of Type::String and sets it to hold val.
char toChar(bool *ok=NULL) const
T get(bool *ok=NULL) const
Value(const QByteArray &val)
Creates a new Value of Type::String and sets it to hold val.
QString toString(bool *ok=NULL) const
Value(quint64 val)
Creates a new Value of Type::Uint64 and sets it to hold val.
qint64 toInt64(bool *ok=NULL) const
Value(ulong val)
Creates a new Value of Type::Ulong and sets it to hold val.
long toLong(bool *ok=NULL) const
bool toBool(bool *ok=NULL) const
quint64 toUInt64(bool *ok=NULL) const
uchar toUChar(bool *ok=NULL) const
Value(uchar val)
Creates a new Value of Type::Uchar and sets it to hold val.
Wrappers for Glib and GObject classes.