diff options
author | Jan Dalheimer <jan@dalheimer.de> | 2015-05-28 19:38:29 +0200 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2015-06-06 21:23:05 +0200 |
commit | 3a8b238052163952831fb5924b2483a375e86ebd (patch) | |
tree | ab120b4fac3a5345a20e7a09e1e7477e67d9ed6f /logic/resources/ResourceObserver.h | |
parent | 161dc66c2c8d5f973ee69dab36c3969a7efd7495 (diff) | |
download | MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.tar MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.tar.gz MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.tar.lz MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.tar.xz MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.zip |
NOISSUE Various changes from multiauth that are unrelated to it
Diffstat (limited to 'logic/resources/ResourceObserver.h')
-rw-r--r-- | logic/resources/ResourceObserver.h | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/logic/resources/ResourceObserver.h b/logic/resources/ResourceObserver.h new file mode 100644 index 00000000..27430d42 --- /dev/null +++ b/logic/resources/ResourceObserver.h @@ -0,0 +1,67 @@ +#pragma once + +#include <memory> +#include <functional> + +#include <QObject> +#include <QMetaProperty> + +class QVariant; +class Resource; + +/// Base class for things that can use a resource +class ResourceObserver +{ +public: + virtual ~ResourceObserver(); + +protected: // these methods are called by the Resource when something changes + virtual void resourceUpdated() = 0; + virtual void setFailure(const QString &) {} + virtual void setProgress(const int) {} + +private: + friend class Resource; + void setSource(std::shared_ptr<Resource> resource) { m_resource = resource; } + +protected: + template<typename T> + T get() const { return getInternal(qMetaTypeId<T>()).template value<T>(); } + QVariant getInternal(const int typeId) const; + +private: + std::shared_ptr<Resource> m_resource; +}; + +/** Observer for QObject properties + * + * Give it a target and the name of a property, and that property will be set when the resource changes. + * + * If no name is given an attempt to find a default property for some common classes is done. + */ +class QObjectResourceObserver : public QObject, public ResourceObserver +{ +public: + explicit QObjectResourceObserver(QObject *target, const char *property = nullptr); + + void resourceUpdated() override; + +private: + QObject *m_target; + QMetaProperty m_property; +}; + +template <typename Ret, typename Arg, typename Func> +class FunctionResourceObserver : public ResourceObserver +{ + std::function<Ret(Arg)> m_function; +public: + template <typename T> + explicit FunctionResourceObserver(T &&func) + : m_function(std::forward<Func>(func)) {} + + void resourceUpdated() override + { + m_function(get<Arg>()); + } +}; |