Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #include <hpactor/config/toml_parser_registry.hpp>
16 : :
17 : : #include <algorithm>
18 : : #include <mutex>
19 : :
20 : : namespace hpactor::config {
21 : :
22 : 1306 : TomlParserRegistry& TomlParserRegistry::instance() {
23 : 1306 : static auto* reg = new TomlParserRegistry();
24 : 1306 : return *reg;
25 : : }
26 : :
27 : 1109 : bool TomlParserRegistry::add_system_parser(std::string_view name, int order,
28 : : SystemParserFactory factory) {
29 : : static std::mutex mutex;
30 : 1109 : std::lock_guard<std::mutex> lock(mutex);
31 : :
32 : 5555 : for (const auto& entry : system_parsers_) {
33 : 4447 : if (entry.name == name)
34 : 1 : return false;
35 : : }
36 : :
37 : 2216 : system_parsers_.push_back({std::string{name}, order, factory});
38 : 1108 : return true;
39 : 1109 : }
40 : :
41 : 123 : bool TomlParserRegistry::add_document_parser(std::string_view name, int order,
42 : : DocumentParserFactory factory) {
43 : : static std::mutex mutex;
44 : 123 : std::lock_guard<std::mutex> lock(mutex);
45 : :
46 : 123 : for (const auto& entry : document_parsers_) {
47 : 0 : if (entry.name == name)
48 : 0 : return false;
49 : : }
50 : :
51 : 246 : document_parsers_.push_back({std::string{name}, order, factory});
52 : 123 : return true;
53 : 123 : }
54 : :
55 : : std::vector<std::unique_ptr<ITomlSystemConfigParser>>
56 : 37 : TomlParserRegistry::create_system_parsers() const {
57 : : static std::mutex mutex;
58 : 37 : std::lock_guard<std::mutex> lock(mutex);
59 : :
60 : 37 : auto entries = system_parsers_;
61 : 37 : std::sort(entries.begin(), entries.end(),
62 : 1153 : [](const SystemEntry& a, const SystemEntry& b) {
63 : 1153 : if (a.order != b.order)
64 : 1079 : return a.order < b.order;
65 : 74 : return a.name < b.name;
66 : : });
67 : :
68 : 37 : std::vector<std::unique_ptr<ITomlSystemConfigParser>> parsers;
69 : 37 : parsers.reserve(entries.size());
70 : 373 : for (const auto& entry : entries) {
71 : 336 : if (entry.factory)
72 : 336 : parsers.push_back(entry.factory());
73 : : }
74 : 37 : return parsers;
75 : 37 : }
76 : :
77 : : std::vector<std::unique_ptr<ITomlDocumentConfigParser>>
78 : 37 : TomlParserRegistry::create_document_parsers() const {
79 : : static std::mutex mutex;
80 : 37 : std::lock_guard<std::mutex> lock(mutex);
81 : :
82 : 37 : auto entries = document_parsers_;
83 : 37 : std::sort(entries.begin(), entries.end(),
84 : 0 : [](const DocumentEntry& a, const DocumentEntry& b) {
85 : 0 : if (a.order != b.order)
86 : 0 : return a.order < b.order;
87 : 0 : return a.name < b.name;
88 : : });
89 : :
90 : 37 : std::vector<std::unique_ptr<ITomlDocumentConfigParser>> parsers;
91 : 37 : parsers.reserve(entries.size());
92 : 74 : for (const auto& entry : entries) {
93 : 37 : if (entry.factory)
94 : 37 : parsers.push_back(entry.factory());
95 : : }
96 : 37 : return parsers;
97 : 37 : }
98 : :
99 : : } // namespace hpactor::config
|