SNode.C
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
ConfigInstance.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "net/config/ConfigInstance.h"
43
44#ifndef DOXYGEN_SHOULD_SKIP_THIS
45
46#include "utils/Config.h"
47
48#include <functional>
49#include <memory>
50
51#ifdef __GNUC__
52#pragma GCC diagnostic push
53#pragma GCC diagnostic ignored "-Wfloat-equal"
54#ifdef __has_warning
55#if __has_warning("-Wweak-vtables")
56#pragma GCC diagnostic ignored "-Wweak-vtables"
57#endif
58#if __has_warning("-Wcovered-switch-default")
59#pragma GCC diagnostic ignored "-Wcovered-switch-default"
60#endif
61#endif
62#endif
63#include "utils/CLI11.hpp"
64#ifdef __GNUC__
65#pragma GCC diagnostic pop
66#endif
67
68#endif /* DOXYGEN_SHOULD_SKIP_THIS */
69
70namespace net::config {
71
72 const std::string ConfigInstance::nameAnonymous = "<anonymous>";
73
74 ConfigInstance::ConfigInstance(const std::string& instanceName, Role role)
75 : instanceName(instanceName)
76 , role(role) {
77 instanceSc = utils::Config::addInstance(instanceName,
78 std::string("Configuration for ")
79 .append(role == Role::SERVER ? "server" : "client")
80 .append(" instance '")
81 .append(instanceName)
82 .append("'"),
83 "Instance");
84
85 utils::Config::addStandardFlags(instanceSc);
86 utils::Config::addHelp(instanceSc);
87
89 ->add_flag_callback(
90 "--disabled{true}",
91 [this]() {
92 utils::Config::disabled(instanceSc, disableOpt->as<bool>());
93 },
94 "Disable this instance")
95 ->trigger_on_parse()
96 ->default_val("false")
97 ->type_name("bool")
98 ->check(CLI::IsMember({"true", "false"}))
99 ->group(instanceSc->get_formatter()->get_label("Persistent Options"));
100 }
101
103 utils::Config::removeInstance(instanceSc);
104 }
105
107 return role;
108 }
109
110 const std::string& ConfigInstance::getInstanceName() const {
111 return instanceName.empty() ? nameAnonymous : instanceName;
112 }
113
114 void ConfigInstance::setInstanceName(const std::string& instanceName) {
115 this->instanceName = instanceName;
116 }
117
118 CLI::App* ConfigInstance::addSection(const std::string& name, const std::string& description) {
119 CLI::App* sectionSc = instanceSc //
120 ->add_subcommand(name, description)
121 ->fallthrough()
122 ->configurable(false)
123 ->allow_extras(false)
124 ->group("Sections")
125 ->disabled(this->instanceName.empty() || name.empty());
126
127 sectionSc //
128 ->option_defaults()
129 ->configurable(!sectionSc->get_disabled());
130
131 if (!sectionSc->get_disabled()) {
132 utils::Config::addStandardFlags(sectionSc);
133 utils::Config::addSimpleHelp(sectionSc);
134 }
135
136 return sectionSc;
137 }
138
139 void ConfigInstance::required(CLI::App* section, bool req) {
140 if (req != section->get_required()) {
141 if (req) {
143 instanceSc->needs(section);
144 } else {
146 instanceSc->remove_needs(section);
147 }
148
149 section->required(req);
150
151 if (!disableOpt->as<bool>()) {
152 utils::Config::required(instanceSc, requiredCount > 0);
153 }
154 }
155 }
156
158 return requiredCount > 0;
159 }
160
161 CLI::App* ConfigInstance::getSection(const std::string& name) const {
162 return instanceSc->get_subcommand_no_throw(name);
163 }
164
166 return disableOpt //
167 ->as<bool>();
168 }
169
170 void ConfigInstance::setDisabled(bool disabled) {
171 disableOpt //
172 ->default_val(disabled ? "true" : "false")
173 ->clear();
174
175 utils::Config::disabled(instanceSc, disabled);
176 }
177
178} // namespace net::config
CLI::App * getSection(const std::string &name) const
void setDisabled(bool disabled=true)
void setInstanceName(const std::string &instanceName)
ConfigInstance(const std::string &instanceName, Role role)
static const std::string nameAnonymous
CLI::App * addSection(const std::string &name, const std::string &description)
const std::string & getInstanceName() const
void required(CLI::App *section, bool req=true)