0.5.4
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-2024 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 <map>
21#include <string>
22#include <utility>
23#include <vector>
24
25namespace clanguml {
26namespace config {
27
28template <typename T> void append_value(T &l, const T &r) { l = r; }
29
30template <typename T>
31void append_value(std::vector<T> &l, const std::vector<T> &r)
32{
33 l.insert(std::end(l), r.begin(), r.end());
34}
35
36template <typename K, typename V>
37void append_value(std::map<K, V> &l, const std::map<K, V> &r)
38{
39 l.insert(r.begin(), r.end());
40}
41
42/**
43 * Possible option inheritance methods from top level to diagram level.
44 */
46 kOverride, /*!< Override entire options */
47 kAppend /*!< Append to list options */
48};
49
51
52/**
53 * @brief Generic configuration option type
54 *
55 * This class template represents a single configuration option, which can
56 * be either a simple type such as bool or std::string or can be a list
57 * or dictionary.
58 *
59 * If the option is constructed only from default value, it's `is_declared`
60 * member is false, so we can deduce whether user provided the option or not.
61 *
62 * For each option type, there has to be defined a YAML decoder and emitter.
63 *
64 * @tparam T The type of the configuration option
65 */
66template <typename T> struct option {
67 option(std::string name_,
69 : name{std::move(name_)}
70 , value{}
72 {
73 }
74 option(std::string name_, T initial_value,
76 : name{std::move(name_)}
77 , value{std::move(initial_value)}
78 , has_value{true}
80 {
81 }
82 option(option_with_alt_names_tag /*unused*/, std::string name_,
83 std::vector<std::string> alternate_names_,
85 : name{std::move(name_)}
86 , alternate_names{std::move(alternate_names_)}
87 , value{}
89 {
90 }
91
92 /**
93 * @brief Set the option value
94 *
95 * @param v Option value
96 */
97 void set(const T &v)
98 {
99 value = v;
100 is_declared = true;
101 has_value = true;
102 }
103
104 /**
105 * @brief Override option value
106 *
107 * This method overrides the option depending on it's inheritance type.
108 *
109 * @param o New option value
110 */
111 void override(const option<T> &o)
112 {
113 if (o.is_declared && inheritance_mode == option_inherit_mode::kAppend) {
114 append_value(value, o.value);
115 is_declared = true;
116 has_value = true;
117 }
118 else if (!is_declared && o.is_declared) {
119 set(o.value);
120 }
121 }
122
123 void operator()(const T &v) { set(v); }
124
125 T &operator()() { return value; }
126
127 const T &operator()() const { return value; }
128
129 operator bool() const { return has_value; }
130
131 /*! Option name, it is also the YAML key in the configuration file */
132 std::string name;
133
134 /*! Alternate option names */
135 std::vector<std::string> alternate_names;
136
137 /*! Option value */
139
140 /*! Whether or not the value was provided by the user or default */
141 bool is_declared{false};
142
143 /*!
144 * Whether the option has value, if the option has no default value
145 * and wasn't provided in the config this is set to `false`.
146 */
147 bool has_value{false};
148
149 /*! The inheritance mode for this option */
151};
152} // namespace config
153} // namespace clanguml