From b6d455a02bd338e9dc0faa09d4d8177ecd8d569a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 10 Apr 2016 15:53:05 +0200 Subject: NOISSUE reorganize and document libraries --- api/logic/resources/ResourceObserver.h | 73 ++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 api/logic/resources/ResourceObserver.h (limited to 'api/logic/resources/ResourceObserver.h') diff --git a/api/logic/resources/ResourceObserver.h b/api/logic/resources/ResourceObserver.h new file mode 100644 index 00000000..c42e41ba --- /dev/null +++ b/api/logic/resources/ResourceObserver.h @@ -0,0 +1,73 @@ +#pragma once + +#include +#include + +#include +#include +#include "multimc_logic_export.h" + +class QVariant; +class Resource; + +/// Base class for things that can use a resource +class MULTIMC_LOGIC_EXPORT 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) { m_resource = resource; } + +protected: + template + T get() const { return getInternal(qMetaTypeId()).template value(); } + QVariant getInternal(const int typeId) const; + +private: + std::shared_ptr 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 MULTIMC_LOGIC_EXPORT QObjectResourceObserver : public QObject, public ResourceObserver +{ +public: + explicit QObjectResourceObserver(QObject *target, const char *property = nullptr); + + void resourceUpdated() override; + +private: + QObject *m_target; + QMetaProperty m_property; +}; + +/** Observer for functions, lambdas etc. + * Template arguments: + * * We need Ret and Arg in order to create the std::function + * * We need Func in order to std::forward the function + */ +template +class FunctionResourceObserver : public ResourceObserver +{ + std::function m_function; +public: + template + explicit FunctionResourceObserver(T &&func) + : m_function(std::forward(func)) {} + + void resourceUpdated() override + { + m_function(get()); + } +}; -- cgit v1.2.3