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:
flow 2022-08-28 19:23:04 -03:00
parent 2f167b1512
commit cda2bfc240
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469

View File

@ -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;
};