QmlMaterial 0.1.0
Loading...
Searching...
No Matches
pool.hpp
1#pragma once
2
3#include <set>
4#include <map>
5#include <QtQml/QQmlEngine>
6#include <QtQml/QQmlIncubator>
7#include <QtQuick/QQuickItem>
8
9namespace qml_material
10{
11
12class Pool;
13class PoolIncubator : public QQmlIncubator {
14public:
15 PoolIncubator(Pool* p, qint64 id, IncubationMode mode);
16
17protected:
18 void statusChanged(Status) override;
19 void setInitialState(QObject*) override;
20
21private:
22 Pool* m_pool;
23 qint64 m_id;
24};
25
26class Pool : public QObject {
27 Q_OBJECT
28 QML_ELEMENT
29
30 Q_PROPERTY(bool async READ async WRITE setAsync NOTIFY asyncChanged FINAL)
31
32 friend class PoolIncubator;
33
34public:
35 Pool(QObject* parent = nullptr);
36 ~Pool();
37
38 Q_INVOKABLE void add(QQmlComponent* comp, const QVariantMap&, bool cache);
39 Q_INVOKABLE void add(const QUrl& url, const QVariantMap&, bool cache);
40 Q_INVOKABLE void addFromModule(const QString& uri, const QString& typeName, const QVariantMap&,
41 bool cache);
42
43 Q_INVOKABLE void addWithKey(const QString& key, QQmlComponent* comp, const QVariantMap&,
44 bool cache);
45 Q_INVOKABLE void addWithKey(const QString& key, const QUrl& url, const QVariantMap&,
46 bool cache);
47 Q_INVOKABLE void addFromModuleWithKey(const QString& key, const QString& uri,
48 const QString& typeName, const QVariantMap&, bool cache);
49
50 Q_SIGNAL void objectAdded(QObject* obj, bool is_cache);
51
52 auto async() const -> bool;
53 void setAsync(bool);
54 Q_SIGNAL void asyncChanged(bool);
55
56 Q_INVOKABLE bool removeObject(QObject*);
57
58private:
59 Q_SIGNAL void queueAdded(qint64);
60 Q_SLOT void onQueueAdded(qint64);
61 Q_SLOT void onUncacheObjectDeleted(QObject*);
62
63 struct Task {
64 QString key;
65 bool cache;
66 QObject* object;
67
68 QPointer<QQmlComponent> component {};
69 PoolIncubator* incubator { nullptr };
70 };
71
72 void incubatorStateChanged(qint64 id, QQmlIncubator::Status status);
73 void setInitialState(QObject* o);
74 auto genSerial() const -> qint64;
75 void onComponentProgress(qint64 id, qreal);
76 void onComponentLoaded(qint64 id);
77 auto createComponent() -> QQmlComponent*;
78 auto tryCache(QStringView, bool) -> bool;
79
80 using task_iterator = std::map<qint64, Task>::iterator;
81 void clearTask(task_iterator);
82
83 mutable qint64 m_serial;
84 std::map<qint64, Task> m_tasks;
85 std::map<QString, QPointer<QObject>, std::less<>> m_objs;
86 std::set<QObject*> m_uncache_objs;
87
88 bool m_async;
89};
90
91} // namespace qml_material