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 bool contains(const QString& key) const;
39 Q_INVOKABLE QObject* get(const QString& key) const;
40 Q_INVOKABLE void add(const QVariant&, const QVariantMap&, bool autoKey = false);
41 Q_INVOKABLE void addWithKey(const QString& key, const QVariant& url_module_comp,
42 const QVariantMap&);
43 Q_SIGNAL void objectAdded(QObject* obj, const QVariant& key /*may null*/);
44
45 auto async() const -> bool;
46 void setAsync(bool);
47 Q_SIGNAL void asyncChanged(bool);
48
49 Q_INVOKABLE bool removeObject(QObject*);
50 Q_INVOKABLE void clear();
51
52 void add(std::optional<QStringView> key, QQmlComponent* comp, const QVariantMap&);
53
54private:
55 Q_SIGNAL void queueAdded(qint64);
56 Q_SLOT void onQueueAdded(qint64);
57 Q_SLOT void onUncacheObjectDeleted(QObject*);
58
59 struct Task {
60 QString key;
61 bool hasKey { false };
62 QObject* object { nullptr };
63
64 QPointer<QQmlComponent> component {};
65 PoolIncubator* incubator { nullptr };
66 };
67
68 void incubatorStateChanged(qint64 id, QQmlIncubator::Status status);
69 void setInitialState(QObject* o);
70 auto genSerial() const -> qint64;
71 void onComponentProgress(qint64 id, qreal);
72 void onComponentLoaded(qint64 id);
73 auto createComponent() -> QQmlComponent*;
74 auto tryCreateComponent(const QVariant&) -> QQmlComponent*;
75 auto tryCache(QStringView) -> bool;
76
77 using task_iterator = std::map<qint64, Task>::iterator;
78 void clearTask(task_iterator);
79
80 mutable qint64 m_serial;
81 std::map<qint64, Task> m_tasks;
82 std::map<QString, QPointer<QObject>, std::less<>> m_objs;
83 std::set<QObject*> m_nokey_objs;
84
85 bool m_async;
86};
87
88} // namespace qml_material