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,30 +12,39 @@
/** Very simple task that just loads a folder's contents directly. /** Very simple task that just loads a folder's contents directly.
*/ */
class BasicFolderLoadTask : public Task class BasicFolderLoadTask : public Task {
{
Q_OBJECT Q_OBJECT
public: public:
struct Result { struct Result {
QMap<QString, Resource::Ptr> resources; QMap<QString, Resource::Ptr> resources;
}; };
using ResultPtr = std::shared_ptr<Result>; using ResultPtr = std::shared_ptr<Result>;
[[nodiscard]] ResultPtr result() const { [[nodiscard]] ResultPtr result() const { return m_result; }
return m_result;
}
public: 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; } [[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 void executeTask() override
{ {
m_dir.refresh(); m_dir.refresh();
for (auto entry : m_dir.entryInfoList()) { 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); m_result->resources.insert(resource->internal_id(), resource);
} }
@ -50,4 +59,6 @@ private:
ResultPtr m_result; ResultPtr m_result;
bool m_aborted = false; bool m_aborted = false;
std::function<Resource*(QFileInfo const&)> m_create_func;
}; };