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
12auto pool_object_count() -> std::size_t;
13
14class Pool;
15class PoolIncubator : public QQmlIncubator {
16public:
17 PoolIncubator(Pool* p, qint64 id, IncubationMode mode);
18
19protected:
20 void statusChanged(Status) override;
21 void setInitialState(QObject*) override;
22
23private:
24 Pool* m_pool;
25 qint64 m_id;
26};
27
28class Pool : public QObject {
29 Q_OBJECT
30 QML_ELEMENT
31
32 Q_PROPERTY(bool async READ async WRITE setAsync NOTIFY asyncChanged FINAL)
33
34 friend class PoolIncubator;
35
36public:
37 Pool(QObject* parent = nullptr);
38 ~Pool();
39
40 Q_INVOKABLE bool contains(const QString& key) const;
41 Q_INVOKABLE QObject* get(const QString& key) const;
42 Q_INVOKABLE void add(const QVariant&, const QVariantMap&, bool autoKey = false);
43 Q_INVOKABLE void addWithKey(const QString& key, const QVariant& url_module_comp,
44 const QVariantMap&);
45 Q_SIGNAL void objectAdded(QObject* obj, const QVariant& key /*may null*/);
46
47 auto async() const -> bool;
48 void setAsync(bool);
49 Q_SIGNAL void asyncChanged(bool);
50
51 Q_INVOKABLE bool removeObject(QObject*);
52 Q_INVOKABLE void clear();
53
54 void add(std::optional<QStringView> key, QQmlComponent* comp, const QVariantMap&);
55
56private:
57 Q_SIGNAL void queueAdded(qint64);
58 Q_SLOT void onQueueAdded(qint64);
59 Q_SLOT void onUncacheObjectDeleted(QObject*);
60
61 struct Task {
62 QString key;
63 bool hasKey { false };
64 QObject* object { nullptr };
65
66 QPointer<QQmlComponent> component {};
67 PoolIncubator* incubator { nullptr };
68 };
69
70 void incubatorStateChanged(qint64 id, QQmlIncubator::Status status);
71 void setInitialState(QObject* o);
72 auto genSerial() const -> qint64;
73 void onComponentProgress(qint64 id, qreal);
74 void onComponentLoaded(qint64 id);
75 auto createComponent() -> QQmlComponent*;
76 auto tryCreateComponent(const QVariant&) -> QQmlComponent*;
77 auto tryCache(QStringView) -> bool;
78
79 using task_iterator = std::map<qint64, Task>::iterator;
80 void clearTask(task_iterator);
81
82 mutable qint64 m_serial;
83 std::map<qint64, Task> m_tasks;
84 std::map<QString, QPointer<QObject>, std::less<>> m_objs;
85 std::set<QPointer<QObject>> m_nokey_objs;
86
87 bool m_async;
88 bool m_drop;
89};
90
91} // namespace qml_material