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
21template <typename T>
23 public:
24 ConfigValue() = default;
25
35 T value,
36 std::string canonical_name,
37 std::string source_key,
38 std::string source,
39 const std::vector<std::string> &source_details)
40 : value_{std::move(value)}, canonical_name_{std::move(canonical_name)}, source_key_{std::move(source_key)},
41 source_{std::move(source)}, source_details_{source_details}
42 {
43 }
44
51 // NOLINTNEXTLINE
52 operator const T &() const &
53 {
54 return value_;
55 }
56
63 // NOLINTNEXTLINE
64 operator T &&() &&
65 {
66 return std::move(value_);
67 }
68
69 [[nodiscard]] const T &value() const &
70 {
71 return value_;
72 }
73
74 [[nodiscard]] T &&value() &&
75 {
76 return std::move(value_);
77 }
78
79 [[nodiscard]] const std::string &canonical_name() const
80 {
81 return canonical_name_;
82 }
83
84 [[nodiscard]] const std::string &source_key() const
85 {
86 return source_key_;
87 }
88
89 [[nodiscard]] const std::string &source() const
90 {
91 return source_;
92 }
93
94 [[nodiscard]] const std::vector<std::string> &source_details() const
95 {
96 return source_details_;
97 }
98
111 template <typename U>
112 [[nodiscard]] ConfigValue<U> derive(const ConfigPODField<U> &override) const
113 {
114 return ConfigValue<U>{
115 *override,
116 override.canonical_name,
117 override.source_key,
118 override.source_info.empty() ? source() : override.source_info,
119 override.source_details.empty() ? source_details() : override.source_details};
120 }
121
146 [[nodiscard]] std::pair<std::vector<std::string>, std::vector<std::vector<FormatParam>>> format_data() const
147 {
148 // foo = 3
149 // Source: ./porytiles.yaml:12
150 // ... details here
151
152 std::vector<std::string> err_text{};
153 std::vector<std::vector<FormatParam>> params{};
154
155 err_text.emplace_back("{} = {}");
156 params.push_back(
157 std::vector{
160 err_text.emplace_back("Source: {}");
161 params.push_back(std::vector{FormatParam{source(), Style::italic}});
162
163 // Add source details if available
164 if (!source_details().empty()) {
165 err_text.emplace_back("");
166 params.emplace_back();
167 err_text.append_range(source_details());
168 for (const auto &_ : source_details()) {
169 params.emplace_back();
170 }
171 }
172
173 return {err_text, params};
174 }
175
201 [[nodiscard]] std::vector<std::string> prettify(const TextFormatter &formatter) const
202 {
203 const auto [format_strings, param_vectors] = format_data();
204
205 std::vector<std::string> result;
206 result.reserve(format_strings.size());
207
208 for (const auto &[text, params] : std::views::zip(format_strings, param_vectors)) {
209 result.push_back(formatter.format(text, params));
210 }
211
212 return result;
213 }
214
215 private:
216 T value_;
217 std::string canonical_name_;
218 std::string source_key_;
219 std::string source_;
220 std::vector<std::string> source_details_;
221};
222
223} // 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.