#ifndef LOGOS_TYPES_H #define LOGOS_TYPES_H #include #include #include #include "logos_shared_api.h" class LogosResultException : public std::runtime_error { public: using std::runtime_error::runtime_error; }; struct LogosResult { bool success; // Error message if success is false // Value can be retrieved like this: // // LogosResult result = someMethod(); // if (result.success) { // QString someValue = result.getValue(); // // OR // QString someValue = result.getString(); // } QVariant value; // LogosResult result = someMethod(); // if (!result.success) { // QString error = result.getError(); // } QVariant error; template T getError() const { if (success) { throw LogosResultException("Attempted to get error from a successful LogosResult"); } return error.value(); } template T getValue() const { if (!success) { throw LogosResultException("Attempted to get value from a failed LogosResult: " + error.toString().toStdString()); } return value.value(); } template T getValue(const QString &key, T defaultValue = T()) const { const QVariantMap &map = getValue(); if (!map.contains(key)) { return defaultValue; } return qvariant_cast(map.value(key)); } template T getValue(int index, const QString &key, T defaultValue = T()) const { const QVariantList &list = getValue(); if (index < 0 || index >= list.size()) { return defaultValue; } return qvariant_cast(list[index].toMap().value(key, defaultValue)); } QString getString() const { return getValue(); } QString getString(const QString &key, const QString &defaultValue = "") const { return getValue(key, defaultValue); } QString getString(int index, const QString &key, const QString &defaultValue = "") const { return getValue(index, key, defaultValue); } bool getBool() const { return getValue(); } bool getBool(const QString &key) const { return getValue(key); } bool getBool(int index, const QString &key) const { return getValue(index, key); } int getInt() const { return getValue(); } int getInt(const QString &key, int defaultValue = 0) const { return getValue(key, defaultValue); } int getInt(int index, const QString &key, int defaultValue = 0) const { return getValue(index, key, defaultValue); } QVariantList getList() const { return getValue(); } QVariantList getList(const QString &key, const QVariantList &defaultValue = QVariantList()) const { return getValue(key, defaultValue); } QVariantList getList(int index, const QString &key, const QVariantList &defaultValue = QVariantList()) const { return getValue(index, key, defaultValue); } QVariantMap getMap() const { return getValue(); } QVariantMap getMap(const QString &key, const QVariantMap &defaultValue = QVariantMap()) const { return getValue(key, defaultValue); } QVariantMap getMap(int index, const QString &key, const QVariantMap &defaultValue = QVariantMap()) const { return getValue(index, key, defaultValue); } }; // Provide (de)serialisation for being use as Remote Object. // // LOGOS_SHARED_API not because these hold state, but because they are the only // out-of-line symbols in logos_types.cpp.obj: leaving them un-imported lets ld // pull that object into a Windows consumer, and archive pull-in is transitive. // The single-provider rule is per object file, not per class — see // logos_shared_api.h. LOGOS_SHARED_API QDataStream &operator<<(QDataStream &out, const LogosResult &result); LOGOS_SHARED_API QDataStream &operator>>(QDataStream &in, LogosResult &result); #endif