0.6.0
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
option.h
Go to the documentation of this file.
1/**
2 * @file src/config/option.h
3 *
4 * Copyright (c) 2021-2025 Bartek Kryza <bkryza@gmail.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18#pragma once
19
20#include <inja/inja.hpp>
21
22#include <map>
23#include <string>
24#include <utility>
25#include <vector>
26
27namespace clanguml {
28namespace config {
29
30template <typename T> void append_value(T &l, const T &r) { l = r; }
31
32template <typename T>
33void append_value(std::vector<T> &l, const std::vector<T> &r)
34{
35 l.insert(std::end(l), r.begin(), r.end());
36}
37
38template <typename K, typename V>
39void append_value(std::map<K, V> &l, const std::map<K, V> &r)
40{
41 l.insert(r.begin(), r.end());
42}
43
44template <> void append_value(inja::json &l, const inja::json &r);
45
46/**
47 * Possible option inheritance methods from top level to diagram level.
48 */
50 kOverride, /*!< Override entire options */
51 kAppend /*!< Append to list options */
52};
53
55
56/**
57 * @brief Generic configuration option type
58 *
59 * This class template represents a single configuration option, which can
60 * be either a simple type such as bool or std::string or can be a list
61 * or dictionary.
62 *
63 * If the option is constructed only from default value, it's `is_declared`
64 * member is false, so we can deduce whether user provided the option or not.
65 *
66 * For each option type, there has to be defined a YAML decoder and emitter.
67 *
68 * @tparam T The type of the configuration option
69 */
70template <typename T> struct option {
71 option(std::string name_,
73 : name{std::move(name_)}
74 , value{}
76 {
77 }
78 option(std::string name_, T initial_value,
80 : name{std::move(name_)}
81 , value{std::move(initial_value)}
82 , has_value{true}
84 {
85 }
86 option(option_with_alt_names_tag /*unused*/, std::string name_,
87 std::vector<std::string> alternate_names_,
89 : name{std::move(name_)}
90 , alternate_names{std::move(alternate_names_)}
91 , value{}
93 {
94 }
95
96 /**
97 * @brief Set the option value
98 *
99 * @param v Option value
100 */
101 void set(const T &v)
102 {
103 value = v;
104 is_declared = true;
105 has_value = true;
106 }
107
108 /**
109 * @brief Override option value
110 *
111 * This method overrides the option depending on it's inheritance type.
112 *
113 * @param o New option value
114 */
115 void override(const option<T> &o)
116 {
117 if (o.is_declared && inheritance_mode == option_inherit_mode::kAppend) {
118 append_value(value, o.value);
119 is_declared = true;
120 has_value = true;
121 }
122 else if (!is_declared && o.is_declared) {
123 set(o.value);
124 }
125 }
126
127 void operator()(const T &v) { set(v); }
128
129 T &operator()() { return value; }
130
131 const T &operator()() const { return value; }
132
133 operator bool() const { return has_value; }
134
135 /*! Option name, it is also the YAML key in the configuration file */
136 std::string name;
137
138 /*! Alternate option names */
139 std::vector<std::string> alternate_names;
140
141 /*! Option value */
143
144 /*! Whether or not the value was provided by the user or default */
145 bool is_declared{false};
146
147 /*!
148 * Whether the option has value, if the option has no default value
149 * and wasn't provided in the config this is set to `false`.
150 */
151 bool has_value{false};
152
153 /*! The inheritance mode for this option */
155};
156} // namespace config
157} // namespace clanguml