Modern implementation of Singleton
With this implementation we also make the initialization of the Singleton instance thread-safe
This commit is contained in:
+8
-17
@@ -161,27 +161,18 @@ namespace {
|
||||
// *****************************************************************************
|
||||
// class member definitions
|
||||
namespace Action {
|
||||
TaskFactory* TaskFactory::instance_ = nullptr;
|
||||
|
||||
TaskFactory& TaskFactory::instance()
|
||||
{
|
||||
/// \todo move the static instance here. It is the "modern" way to implement a singleton
|
||||
if (nullptr == instance_) {
|
||||
instance_ = new TaskFactory;
|
||||
}
|
||||
return *instance_;
|
||||
} // TaskFactory::instance
|
||||
static TaskFactory instance_;
|
||||
return instance_;
|
||||
}
|
||||
|
||||
void TaskFactory::cleanup()
|
||||
{
|
||||
if (instance_ != nullptr) {
|
||||
for (auto&& i : registry_) {
|
||||
delete i.second;
|
||||
}
|
||||
delete instance_;
|
||||
instance_ = nullptr;
|
||||
}
|
||||
} //TaskFactory::cleanup
|
||||
for (auto&& i : registry_) {
|
||||
delete i.second;
|
||||
}
|
||||
}
|
||||
|
||||
void TaskFactory::registerTask(TaskType type, Task::UniquePtr task)
|
||||
{
|
||||
@@ -190,7 +181,7 @@ namespace Action {
|
||||
delete i->second;
|
||||
}
|
||||
registry_[type] = task.release();
|
||||
} // TaskFactory::registerTask
|
||||
}
|
||||
|
||||
TaskFactory::TaskFactory()
|
||||
{
|
||||
|
||||
+1
-5
@@ -140,12 +140,8 @@ namespace Action {
|
||||
//! Prevent construction other than through instance().
|
||||
TaskFactory();
|
||||
|
||||
//! Pointer to the one and only instance of this class.
|
||||
static TaskFactory* instance_;
|
||||
//! Type used to store Task prototype classes
|
||||
using Registry = std::map<TaskType, Task*>;
|
||||
//! List of task types and corresponding prototypes.
|
||||
Registry registry_;
|
||||
std::map<TaskType, Task*> registry_;
|
||||
};
|
||||
|
||||
//! %Print the Exif (or other metadata) of a file to stdout
|
||||
|
||||
Reference in New Issue
Block a user