Porytiles
Loading...
Searching...
No Matches
config_value.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <ranges>
4#include <string>
5#include <utility>
6#include <vector>
7
10
11namespace porytiles {
12
23template <typename T>
25 public:
26 ConfigValue() = default;
27
39 T value,
40 std::string canonical_name,
41 std::string source_key,
42 std::string source,
43 const std::vector<std::string> &source_details)
44 : value_{std::move(value)}, canonical_name_{std::move(canonical_name)}, source_key_{std::move(source_key)},
45 source_{std::move(source)}, source_details_{source_details}
46 {
47 }
48
57 // NOLINTNEXTLINE
58 operator const T &() const &
59 {
60 return value_;
61 }
62
71 // NOLINTNEXTLINE
72 operator T &&() &&
73 {
74 return std::move(value_);
75 }
76
77 [[nodiscard]] const T &value() const &
78 {
79 return value_;
80 }
81
82 [[nodiscard]] T &&value() &&
83 {
84 return std::move(value_);
85 }
86
87 [[nodiscard]] const std::string &canonical_name() const
88 {
89 return canonical_name_;
90 }
91
92 [[nodiscard]] const std::string &source_key() const
93 {
94 return source_key_;
95 }
96
97 [[nodiscard]] const std::string &source() const
98 {
99 return source_;
100 }
101
102 [[nodiscard]] const std::vector<std::string> &source_details() const
103 {
104 return source_details_;
105 }
106
121 template <typename U>
122 [[nodiscard]] ConfigValue<U> derive(const ConfigPODField<U> &override) const
123 {
124 return ConfigValue<U>{
125 *override,
126 override.canonical_name,
127 override.source_key,
128 override.source_info.empty() ? source() : override.source_info,
129 override.source_details.empty() ? source_details() : override.source_details};
130 }
131
158 [[nodiscard]] std::pair<std::vector<std::string>, std::vector<std::vector<FormatParam>>> format_data() const
159 {
160 // foo = 3
161 // Source: ./porytiles.yaml:12
162 // ... details here
163
164 std::vector<std::string> err_text{};
165 std::vector<std::vector<FormatParam>> params{};
166
167 err_text.emplace_back("{} = {}");
168 params.push_back(
169 std::vector{
172 err_text.emplace_back("Source: {}");
173 params.push_back(std::vector{FormatParam{source(), Style::italic}});
174
175 // Add source details if available
176 if (!source_details().empty()) {
177 err_text.emplace_back("");
178 params.emplace_back();
179 err_text.append_range(source_details());
180 for (const auto &_ : source_details()) {
181 params.emplace_back();
182 }
183 }
184
185 return {err_text, params};
186 }
187
215 [[nodiscard]] std::vector<std::string> prettify(const TextFormatter &formatter) const
216 {
217 const auto [format_strings, param_vectors] = format_data();
218
219 std::vector<std::string> result;
220 result.reserve(format_strings.size());
221
222 for (const auto &[text, params] : std::views::zip(format_strings, param_vectors)) {
223 result.push_back(formatter.format(text, params));
224 }
225
226 return result;
227 }
228
229 private:
230 T value_;
231 std::string canonical_name_;
232 std::string source_key_;
233 std::string source_;
234 std::vector<std::string> source_details_;
235};
236
237} // namespace porytiles
A container that wraps a configuration value with its name and source information.
const std::vector< std::string > & source_details() const
ConfigValue< U > derive(const ConfigPODField< U > &override) const
Creates a child ConfigValue from a ConfigPODField, inheriting this value's source provenance.
const std::string & source() const
const std::string & source_key() const
std::pair< std::vector< std::string >, std::vector< std::vector< FormatParam > > > format_data() const
Generates formatted text data for displaying this configuration value.
const std::string & canonical_name() const
const T & value() const &
std::vector< std::string > prettify(const TextFormatter &formatter) const
Generates a prettified, styled readout of this configuration value.
ConfigValue(T value, std::string canonical_name, std::string source_key, std::string source, const std::vector< std::string > &source_details)
Constructs a ConfigValue with a value, names, and source information.
A text parameter with associated styling for formatted output.
static const Style italic
Italic text formatting.
static const Style yellow
Yellow foreground color.
static const Style bold
Bold text formatting.
Abstract base class for applying text styling with context-aware formatting.
virtual std::string format(const std::string &format_str, const std::vector< FormatParam > &params) const
Formats a string with styled parameters using fmtlib syntax.
A lightweight wrapper for per-field configuration values with source metadata.