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 : : #pragma once
16 : :
17 : : #include <hpactor/config/toml_config_parser.hpp>
18 : :
19 : : #include <memory>
20 : : #include <string>
21 : : #include <vector>
22 : :
23 : : namespace hpactor::config {
24 : :
25 : : class TomlParserRegistry {
26 : : public:
27 : : using SystemParserFactory = std::unique_ptr<ITomlSystemConfigParser> (*)();
28 : : using DocumentParserFactory = std::unique_ptr<ITomlDocumentConfigParser> (*)();
29 : :
30 : : static TomlParserRegistry& instance();
31 : :
32 : : bool add_system_parser(std::string_view name, int order,
33 : : SystemParserFactory factory);
34 : : bool add_document_parser(std::string_view name, int order,
35 : : DocumentParserFactory factory);
36 : :
37 : : std::vector<std::unique_ptr<ITomlSystemConfigParser>>
38 : : create_system_parsers() const;
39 : :
40 : : std::vector<std::unique_ptr<ITomlDocumentConfigParser>>
41 : : create_document_parsers() const;
42 : :
43 : : private:
44 : 123 : TomlParserRegistry() = default;
45 : :
46 : : struct SystemEntry {
47 : : std::string name;
48 : : int order{0};
49 : : SystemParserFactory factory{nullptr};
50 : : };
51 : :
52 : : struct DocumentEntry {
53 : : std::string name;
54 : : int order{0};
55 : : DocumentParserFactory factory{nullptr};
56 : : };
57 : :
58 : : mutable std::vector<SystemEntry> system_parsers_;
59 : : mutable std::vector<DocumentEntry> document_parsers_;
60 : : };
61 : :
62 : : template <typename ParserT> class TomlSystemParserRegistration {
63 : : public:
64 : 1109 : TomlSystemParserRegistration() {
65 : 1109 : registered_ = TomlParserRegistry::instance().add_system_parser(
66 : : ParserT::kName, ParserT::kOrder,
67 : 1445 : []() -> std::unique_ptr<ITomlSystemConfigParser> {
68 : 336 : return std::make_unique<ParserT>();
69 : : });
70 : 1109 : }
71 : :
72 : 2 : bool registered() const noexcept {
73 : 2 : return registered_;
74 : : }
75 : :
76 : : private:
77 : : bool registered_{false};
78 : : };
79 : :
80 : : template <typename ParserT> class TomlDocumentParserRegistration {
81 : : public:
82 : 123 : TomlDocumentParserRegistration() {
83 : 123 : registered_ = TomlParserRegistry::instance().add_document_parser(
84 : : ParserT::kName, ParserT::kOrder,
85 : 160 : []() -> std::unique_ptr<ITomlDocumentConfigParser> {
86 : 37 : return std::make_unique<ParserT>();
87 : : });
88 : 123 : }
89 : :
90 : : bool registered() const noexcept {
91 : : return registered_;
92 : : }
93 : :
94 : : private:
95 : : bool registered_{false};
96 : : };
97 : :
98 : : } // namespace hpactor::config
|