|
11 | 11 |
|
12 | 12 | namespace fly {
|
13 | 13 |
|
| 14 | +//================================================================================================== |
| 15 | +std::shared_ptr<ConfigManager> ConfigManager::create( |
| 16 | + std::shared_ptr<SequencedTaskRunner> task_runner, |
| 17 | + ConfigFileType file_type, |
| 18 | + std::filesystem::path path) |
| 19 | +{ |
| 20 | + // ConfigManager has a private constructor, thus cannot be used with std::make_shared. This |
| 21 | + // class is used to expose the private constructor locally. |
| 22 | + struct ConfigManagerImpl final : public ConfigManager |
| 23 | + { |
| 24 | + ConfigManagerImpl( |
| 25 | + std::shared_ptr<SequencedTaskRunner> task_runner, |
| 26 | + ConfigFileType file_type, |
| 27 | + std::filesystem::path path) noexcept : |
| 28 | + ConfigManager(std::move(task_runner), file_type, std::move(path)) |
| 29 | + { |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + auto config_manager = |
| 34 | + std::make_shared<ConfigManagerImpl>(std::move(task_runner), file_type, std::move(path)); |
| 35 | + return config_manager->start() ? config_manager : nullptr; |
| 36 | +} |
| 37 | + |
14 | 38 | //==================================================================================================
|
15 | 39 | ConfigManager::ConfigManager(
|
16 |
| - const std::shared_ptr<SequencedTaskRunner> &task_runner, |
| 40 | + std::shared_ptr<SequencedTaskRunner> task_runner, |
17 | 41 | ConfigFileType file_type,
|
18 |
| - const std::filesystem::path &path) noexcept : |
19 |
| - m_path(path), |
20 |
| - m_task_runner(task_runner) |
| 42 | + std::filesystem::path path) noexcept : |
| 43 | + m_task_runner(std::move(task_runner)), |
| 44 | + m_path(std::move(path)) |
21 | 45 | {
|
| 46 | + m_monitor = std::make_shared<PathMonitorImpl>(m_task_runner, create_config<PathConfig>()); |
| 47 | + |
22 | 48 | switch (file_type)
|
23 | 49 | {
|
24 | 50 | case ConfigFileType::Ini:
|
@@ -47,35 +73,27 @@ ConfigManager::~ConfigManager()
|
47 | 73 | //==================================================================================================
|
48 | 74 | bool ConfigManager::start()
|
49 | 75 | {
|
50 |
| - if (m_parser) |
| 76 | + if (!m_parser || !m_monitor) |
51 | 77 | {
|
52 |
| - m_monitor = std::make_shared<PathMonitorImpl>(m_task_runner, create_config<PathConfig>()); |
| 78 | + return false; |
| 79 | + } |
53 | 80 |
|
54 |
| - if (m_monitor->start()) |
55 |
| - { |
56 |
| - std::weak_ptr<ConfigManager> weak_self = shared_from_this(); |
| 81 | + std::weak_ptr<ConfigManager> weak_self = shared_from_this(); |
57 | 82 |
|
58 |
| - auto callback = [weak_self](const std::filesystem::path &, PathMonitor::PathEvent) |
| 83 | + auto callback = [weak_self](std::filesystem::path, PathMonitor::PathEvent) |
| 84 | + { |
| 85 | + if (auto self = weak_self.lock(); self) |
| 86 | + { |
| 87 | + auto task = [](std::shared_ptr<ConfigManager> nested_self) |
59 | 88 | {
|
60 |
| - if (auto self = weak_self.lock(); self) |
61 |
| - { |
62 |
| - auto task = [](std::shared_ptr<ConfigManager> nested_self) |
63 |
| - { |
64 |
| - nested_self->update_config(); |
65 |
| - }; |
66 |
| - |
67 |
| - self->m_task_runner->post_task( |
68 |
| - FROM_HERE, |
69 |
| - std::move(task), |
70 |
| - std::move(weak_self)); |
71 |
| - } |
| 89 | + nested_self->update_config(); |
72 | 90 | };
|
73 | 91 |
|
74 |
| - return m_monitor->add_file(m_path, callback); |
| 92 | + self->m_task_runner->post_task(FROM_HERE, std::move(task), std::move(weak_self)); |
75 | 93 | }
|
76 |
| - } |
| 94 | + }; |
77 | 95 |
|
78 |
| - return false; |
| 96 | + return m_monitor->add_file(m_path, callback); |
79 | 97 | }
|
80 | 98 |
|
81 | 99 | //==================================================================================================
|
|
0 commit comments