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);
105 Value(
const char *val);
106 Value(
const QByteArray & val);
107 Value(
const QString & val);
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>
355 typename boost::mpl::if_<
358 >::type dataRef = data;
360 value.setData(GetType<T>(), &dataRef);
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);
389 typename T::CType *gobj;
390 value.getData(GetType<T>(), &gobj);
396 typename T::CType *gobj =
static_cast<typename T::CType*
>(data);
397 value.setData(GetType<T>(), &gobj);
408 static inline void set(
Value & value,
const char (&data)[N])
410 QByteArray str = QByteArray::fromRawData(data, N);
411 value.setData(Type::String, &str);
420 static inline void set(
Value & value,
const char (&data)[N])
422 QByteArray str = QByteArray::fromRawData(data, N);
423 value.setData(Type::String, &str);
434 static inline void set(
Value & value,
const char *data)
436 QByteArray str = QByteArray::fromRawData(data, qstrlen(data));
437 value.setData(Type::String, &str);
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);
465 static inline Value
get(
const Value & value)
470 static inline void set(Value & value,
const Value & data)
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);
qint64 toInt64(bool *ok=NULL) const
int toInt(bool *ok=NULL) const
static RefPointer< T > wrap(typename T::CType *nativePtr, bool increaseRef=true)
static Value create(const T &data)
Wrappers for Glib and GObject classes.
Smart pointer class for working with wrapper classes that support reference counting.
Wrapper class for GValue.
QByteArray toByteArray(bool *ok=NULL) const
uchar toUChar(bool *ok=NULL) const
long toLong(bool *ok=NULL) const
Error toError(bool *ok=NULL) const
uint toUInt(bool *ok=NULL) const
T get(bool *ok=NULL) const
char toChar(bool *ok=NULL) const
QString toString(bool *ok=NULL) const
bool toBool(bool *ok=NULL) const
ulong toULong(bool *ok=NULL) const
quint64 toUInt64(bool *ok=NULL) const
Wrapper class for GError.