Porytiles
Loading...
Searching...
No Matches
config_value.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <utility>
5#include <vector>
6
8
9namespace porytiles2 {
10
21template <typename T>
23 public:
32 ConfigValue(T value, std::string name, std::string source, const std::vector<std::string> &source_details)
33 : value_{std::move(value)}, name_{std::move(name)}, source_{std::move(source)}, source_details_{source_details}
34 {
35 }
36
45 operator const T &() const &
46 {
47 return value_;
48 }
49
58 operator T &&() &&
59 {
60 return std::move(value_);
61 }
62
68 [[nodiscard]] const T &value() const &
69 {
70 return value_;
71 }
72
78 [[nodiscard]] T &&value() &&
79 {
80 return std::move(value_);
81 }
82
94 [[nodiscard]] const std::string &name() const
95 {
96 return name_;
97 }
98
110 [[nodiscard]] const std::string &source() const
111 {
112 return source_;
113 }
114
124 [[nodiscard]] const std::vector<std::string> &source_details() const
125 {
126 return source_details_;
127 }
128
155 [[nodiscard]] std::pair<std::vector<std::string>, std::vector<std::vector<FormatParam>>> format_data() const
156 {
157 // foo = 3
158 // Source: ./porytiles.yaml:12
159 // ... details here
160
161 std::vector<std::string> err_text{};
162 std::vector<std::vector<FormatParam>> params{};
163
164 err_text.emplace_back("{} = {}");
165 params.push_back(
166 std::vector{
168 err_text.emplace_back("Source: {}");
169 params.push_back(std::vector{FormatParam{source(), Style::italic}});
170
171 // Add source details if available
172 if (!source_details().empty()) {
173 err_text.emplace_back("");
174 params.emplace_back();
175 std::ranges::copy(source_details(), std::back_inserter(err_text));
176 for (const auto &_ : source_details()) {
177 params.emplace_back();
178 }
179 }
180
181 return {err_text, params};
182 }
183
184 private:
185 T value_;
186 std::string name_;
187 std::string source_;
188 std::vector<std::string> source_details_;
189};
190
191} // namespace porytiles2
A container that wraps a configuration value with its name and source information.
T && value() &&
Gets an rvalue reference to the underlying value.
const std::vector< std::string > & source_details() const
Gets the source details for this configuration value.
ConfigValue(T value, std::string name, std::string source, const std::vector< std::string > &source_details)
Constructs a ConfigValue with a value, name, and source information.
const T & value() const &
Gets a const reference to the underlying value.
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 & name() const
Gets the name of this configuration value.
const std::string & source() const
Gets the source information for this configuration value.
A text parameter with associated styling for formatted output.
@ italic
Italic text formatting.
@ bold
Bold text formatting.
@ yellow
Yellow text color.