ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
ZMarkupDocs.hpp
Go to the documentation of this file.
1#pragma once
2
13
14#include <charconv>
15#include <functional>
16#include <memory>
17#include <stdexcept>
18#include <string>
19#include <string_view>
20#include <type_traits>
21#include <unordered_map>
22#include <variant>
23#include <vector>
24
25namespace ZenitUI
26{
27 class Layout;
28}
29
30namespace ZenitUI::ZMarkup
31{
35
36 struct Element;
37 class Registry;
38
40 enum class AttrType
41 {
42 String,
43 Float,
44 Int,
45 Bool,
47 };
48
49 [[nodiscard]] const char *toString(AttrType t) noexcept;
50
52 using AttrValue = std::variant<std::string, float, int, bool, std::vector<std::string>>;
53
55 struct AttrDoc
56 {
57 std::string name;
58 AttrType type{AttrType::String};
59 std::string defaultValue;
60 AttrValue defaultTyped{std::string{}};
61 std::string example;
62 std::string description;
63 };
64
66 struct TagDoc
67 {
68 std::string name;
69 std::string widget;
70 std::string description;
71 std::string text;
72 std::vector<AttrDoc> attrs;
73 std::vector<std::string> parts;
74 };
75
78 {
79 std::string_view name;
80 std::string_view syntax;
81 std::string_view description;
82 std::string_view example{};
83 };
84
85 namespace detail
86 {
87 template <typename T>
88 constexpr AttrType attrTypeFor()
89 {
90 if constexpr (std::is_same_v<T, std::string>)
91 return AttrType::String;
92 else if constexpr (std::is_same_v<T, float>)
93 return AttrType::Float;
94 else if constexpr (std::is_same_v<T, int>)
95 return AttrType::Int;
96 else if constexpr (std::is_same_v<T, bool>)
97 return AttrType::Bool;
98 else if constexpr (std::is_same_v<T, std::vector<std::string>>)
99 return AttrType::List;
100 else
101 static_assert(!sizeof(T), "Unsupported ZMarkup attribute type");
102 }
103
104 template <typename T>
105 std::string formatDefaultValue(const T &v)
106 {
107 if constexpr (std::is_same_v<T, std::string>)
108 {
109 return v.empty() ? std::string("\"\"") : v;
110 }
111 else if constexpr (std::is_same_v<T, bool>)
112 {
113 return v ? "true" : "false";
114 }
115 else if constexpr (std::is_same_v<T, int>)
116 {
117 return std::to_string(v);
118 }
119 else if constexpr (std::is_same_v<T, float>)
120 {
121 char buf[32];
122 auto [ptr, ec] = std::to_chars(buf, buf + sizeof(buf), v);
123 if (ec == std::errc())
124 return std::string(buf, ptr);
125 return std::to_string(v);
126 }
127 else if constexpr (std::is_same_v<T, std::vector<std::string>>)
128 {
129 std::string out;
130 for (size_t i = 0; i < v.size(); ++i)
131 {
132 if (i)
133 out += ", ";
134 out += v[i];
135 }
136 return out.empty() ? std::string("\"\"") : out;
137 }
138 }
139 } // namespace detail
140
148 class Attrs
149 {
150 public:
152 static Attrs fromElement(const TagDoc &doc, const Element &el);
153
155 template <typename T>
156 const T &get(std::string_view name) const
157 {
158 auto it = values_.find(std::string(name));
159 if (it == values_.end())
160 throw std::logic_error("ZMarkup::Attrs::get: undeclared attribute '" + std::string(name) +
161 "' on tag '" + tagName_ + "'");
162 const T *ptr = std::get_if<T>(&it->second.value);
163 if (!ptr)
164 throw std::logic_error("ZMarkup::Attrs::get: type mismatch for attribute '" + std::string(name) +
165 "' on tag '" + tagName_ + "'");
166 return *ptr;
167 }
168
170 bool has(std::string_view name) const;
171
173 const std::string &text() const { return text_; }
174
175 private:
176 struct Entry
177 {
178 AttrValue value;
179 bool explicitPresent{false};
180 };
181
182 std::string tagName_;
183 std::string text_;
184 std::unordered_map<std::string, Entry> values_;
185 };
186
191 class TagBuilder
192 {
193 public:
194 TagBuilder(Registry &reg, std::string_view name);
195
196 TagBuilder &widget(std::string_view doxygenSymbol);
197 TagBuilder &describe(std::string_view desc);
198 TagBuilder &textContent(std::string_view meaning);
199 TagBuilder &part(std::string_view partName);
200
201 template <typename T>
202 TagBuilder &attr(std::string_view name,
203 T defaultValue,
204 std::string_view example,
205 std::string_view description)
206 {
207 AttrDoc ad;
208 ad.name = std::string(name);
209 ad.type = detail::attrTypeFor<T>();
210 ad.defaultValue = detail::formatDefaultValue(defaultValue);
211 ad.defaultTyped = AttrValue(std::move(defaultValue));
212 ad.example = std::string(example);
213 ad.description = std::string(description);
214 doc_.attrs.push_back(std::move(ad));
215 return *this;
216 }
217
219 void create(std::function<std::shared_ptr<Layout>(const Attrs &)> fn);
220
221 private:
222 Registry &reg_;
223 TagDoc doc_;
224 };
225
227 [[nodiscard]] const std::vector<AttrDoc> &universalAttrDocs();
228
230 [[nodiscard]] const std::vector<TagDoc> &structuralTagDocs();
231
233 [[nodiscard]] const std::vector<MarkupSyntaxDoc> &markupSyntaxDocs();
234
236} // namespace ZenitUI::ZMarkup
The base node type: tree, style, layout, input, scroll, render.
Definition Layout.hpp:154
Pre-parsed, type-safe attribute bag passed to factories registered via Registry::tag().
Definition ZMarkupDocs.hpp:149
const T & get(std::string_view name) const
Return the typed value of a declared attribute (or its declared default).
Definition ZMarkupDocs.hpp:156
static Attrs fromElement(const TagDoc &doc, const Element &el)
Build an Attrs bag by parsing el according to doc (defaults come solely from doc).
bool has(std::string_view name) const
true if the declared attribute was explicitly present in the markup element.
const std::string & text() const
Quoted text content of the element.
Definition ZMarkupDocs.hpp:173
Tag → factory map plus the prefab table (either per-UIContext or standalone/global).
Definition ZMarkup.hpp:215
void create(std::function< std::shared_ptr< Layout >(const Attrs &)> fn)
Finalize registration using a typed factory receiving pre-parsed Attrs.
AttrType
Value type of a declared ZMarkup attribute.
Definition ZMarkupDocs.hpp:41
const std::vector< AttrDoc > & universalAttrDocs()
Attributes accepted by every tag (besides ZStyle properties).
const std::vector< TagDoc > & structuralTagDocs()
Documentation of the structural pseudo-tags Slot, If, Each.
std::variant< std::string, float, int, bool, std::vector< std::string > > AttrValue
Typed storage for a parsed or default attribute value.
Definition ZMarkupDocs.hpp:52
const std::vector< MarkupSyntaxDoc > & markupSyntaxDocs()
Documentation of basic ZMarkup syntax constructs (#id, .class, @ref, ...).
@ List
Comma-separated list (e.g. Dropdown options).
Definition ZMarkupDocs.hpp:46
Metadata and single-source default for one tag attribute.
Definition ZMarkupDocs.hpp:56
std::string description
One or two sentences.
Definition ZMarkupDocs.hpp:62
std::string defaultValue
Human-readable default derived from defaultTyped.
Definition ZMarkupDocs.hpp:59
std::string example
Valid value verified by tests and used in generated examples.
Definition ZMarkupDocs.hpp:61
Parsed representation of one markup node.
Definition ZMarkup.hpp:35
Documentation entry for basic ZMarkup syntax constructs (#id, .class, @ref, ...).
Definition ZMarkupDocs.hpp:78
Metadata of one ZMarkup tag (only tag-specific attributes live in attrs).
Definition ZMarkupDocs.hpp:67
std::string widget
Doxygen symbol of the C++ class, e.g. "ZenitUI::UI::Slider".
Definition ZMarkupDocs.hpp:69
std::vector< std::string > parts
part names exposed by the widget.
Definition ZMarkupDocs.hpp:73
std::string text
Meaning of quoted text ("button label"), or "" if unused.
Definition ZMarkupDocs.hpp:71
std::string name
Markup tag, e.g. "Slider".
Definition ZMarkupDocs.hpp:68
std::string description
One or two sentences.
Definition ZMarkupDocs.hpp:70