feat: allow specifying factory for resources in BasicFolderLoadTask
This allows us to hook our own resource type, that possibly has more content than the base Resource, to it. Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
parent
2f167b1512
commit
cda2bfc240
@ -12,8 +12,7 @@
|
||||
|
||||
/** Very simple task that just loads a folder's contents directly.
|
||||
*/
|
||||
class BasicFolderLoadTask : public Task
|
||||
{
|
||||
class BasicFolderLoadTask : public Task {
|
||||
Q_OBJECT
|
||||
public:
|
||||
struct Result {
|
||||
@ -21,21 +20,31 @@ public:
|
||||
};
|
||||
using ResultPtr = std::shared_ptr<Result>;
|
||||
|
||||
[[nodiscard]] ResultPtr result() const {
|
||||
return m_result;
|
||||
}
|
||||
[[nodiscard]] ResultPtr result() const { return m_result; }
|
||||
|
||||
public:
|
||||
BasicFolderLoadTask(QDir dir) : Task(nullptr, false), m_dir(dir), m_result(new Result) {}
|
||||
BasicFolderLoadTask(QDir dir) : Task(nullptr, false), m_dir(dir), m_result(new Result)
|
||||
{
|
||||
m_create_func = [](QFileInfo const& entry) -> Resource* {
|
||||
return new Resource(entry);
|
||||
};
|
||||
}
|
||||
BasicFolderLoadTask(QDir dir, std::function<Resource*(QFileInfo const&)> create_function)
|
||||
: Task(nullptr, false), m_dir(dir), m_result(new Result), m_create_func(std::move(create_function))
|
||||
{}
|
||||
|
||||
[[nodiscard]] bool canAbort() const override { return true; }
|
||||
bool abort() override { m_aborted = true; return true; }
|
||||
bool abort() override
|
||||
{
|
||||
m_aborted = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void executeTask() override
|
||||
{
|
||||
m_dir.refresh();
|
||||
for (auto entry : m_dir.entryInfoList()) {
|
||||
auto resource = new Resource(entry);
|
||||
auto resource = m_create_func(entry);
|
||||
m_result->resources.insert(resource->internal_id(), resource);
|
||||
}
|
||||
|
||||
@ -50,4 +59,6 @@ private:
|
||||
ResultPtr m_result;
|
||||
|
||||
bool m_aborted = false;
|
||||
|
||||
std::function<Resource*(QFileInfo const&)> m_create_func;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user