CaptureMoment 0.1.0
Free and open-source non-destructive photo editor
Loading...
Searching...
No Matches
worker_builder.cpp
Go to the documentation of this file.
1
9
10#include <spdlog/spdlog.h>
11#include <utility>
12
14
15std::unordered_map<WorkerType, WorkerBuilder::CreatorFunc>& WorkerBuilder::getRegistry()
16{
17 // Meyer's Singleton: Initialized on first use, destroyed on app exit.
18 static std::unordered_map<WorkerType, CreatorFunc> registry;
19 return registry;
20}
21
23{
24 // Register the creator function in the static map
25 getRegistry()[type] = std::move(creator);
26}
27
28std::unique_ptr<IWorkerRequest> WorkerBuilder::build(WorkerType type)
29{
30 auto& registry = getRegistry();
31 auto it = registry.find(type);
32
33 // Check if the requested type has been registered
34 if (it == registry.end())
35 {
36 spdlog::error("[WorkerBuilder] No creator registered for worker type: {}", static_cast<int>(type));
37 return nullptr;
38 }
39
40 try
41 {
42 // Invoke the stored lambda/function to create the instance
43 // The creator is expected to call the default constructor of the concrete implementation.
44 return it->second();
45 }
46 catch (const std::exception& e)
47 {
48 spdlog::critical("[WorkerBuilder] Exception during instantiation of worker type {}: {}",
49 static_cast<int>(type), e.what());
50 return nullptr;
51 }
52}
53
54} // namespace CaptureMoment::Core::Workers
std::function< std::unique_ptr< IWorkerRequest >()> CreatorFunc
Definition of the creator function signature.
static std::unique_ptr< IWorkerRequest > build(WorkerType type)
Retrieves an instance of the worker for the specified type.
static void registerCreator(WorkerType type, CreatorFunc creator)
Registers a creator function for a specific worker type.
WorkerType
Identifies the specific technology or strategy used for a processing worker.
Definition worker_type.h:32
Central Registry and Factory for IWorkerRequest instances.
CaptureMoment — footer