Skip to content

Commit

Permalink
Simplify TaskFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
piponazo committed Feb 5, 2022
1 parent f18d04c commit 93fe208
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
38 changes: 15 additions & 23 deletions src/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,43 +169,35 @@ namespace Action {

void TaskFactory::cleanup()
{
for (auto&& i : registry_) {
delete i.second;
}
registry_.clear();
}

void TaskFactory::registerTask(TaskType type, Task::UniquePtr task)
{
auto i = registry_.find(type);
if (i != registry_.end()) {
delete i->second;
}
registry_[type] = task.release();
registry_[type] = std::move(task);
}

TaskFactory::TaskFactory()
{
// Register a prototype of each known task
registerTask(adjust, std::make_unique<Adjust>());
registerTask(print, std::make_unique<Print>());
registerTask(rename, std::make_unique<Rename>());
registerTask(erase, std::make_unique<Erase>());
registerTask(extract, std::make_unique<Extract>());
registerTask(insert, std::make_unique<Insert>());
registerTask(modify, std::make_unique<Modify>());
registerTask(fixiso, std::make_unique<FixIso>());
registerTask(fixcom, std::make_unique<FixCom>());
} // TaskFactory c'tor
registry_.emplace(adjust, std::make_unique<Adjust>());
registry_.emplace(print, std::make_unique<Print>());
registry_.emplace(rename, std::make_unique<Rename>());
registry_.emplace(erase, std::make_unique<Erase>());
registry_.emplace(extract, std::make_unique<Extract>());
registry_.emplace(insert, std::make_unique<Insert>());
registry_.emplace(modify, std::make_unique<Modify>());
registry_.emplace(fixiso, std::make_unique<FixIso>());
registry_.emplace(fixcom, std::make_unique<FixCom>());
}

Task::UniquePtr TaskFactory::create(TaskType type)
{
auto i = registry_.find(type);
if (i != registry_.end() && i->second != 0) {
Task* t = i->second;
return t->clone();
if (i != registry_.end() && i->second) {
return i->second->clone();
}
return nullptr;
} // TaskFactory::create
}

int setModeAndPrintStructure(Exiv2::PrintStructureOption option, const std::string& path,bool binary)
{
Expand Down
3 changes: 2 additions & 1 deletion src/actions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// *****************************************************************************
// included header files
#include "exiv2app.hpp"
#include <unordered_map>

// *****************************************************************************
// class declarations
Expand Down Expand Up @@ -141,7 +142,7 @@ namespace Action {
TaskFactory();

//! List of task types and corresponding prototypes.
std::map<TaskType, Task*> registry_;
std::unordered_map<TaskType, Task::UniquePtr> registry_;
};

//! %Print the Exif (or other metadata) of a file to stdout
Expand Down

0 comments on commit 93fe208

Please sign in to comment.