21#include <unordered_map>
30namespace ZenitUI::ZMarkup
49 [[nodiscard]]
const char *toString(
AttrType t)
noexcept;
52 using AttrValue = std::variant<std::string, float, int, bool, std::vector<std::string>>;
72 std::vector<AttrDoc> attrs;
79 std::string_view name;
80 std::string_view syntax;
81 std::string_view description;
82 std::string_view example{};
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>)
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>>)
101 static_assert(!
sizeof(T),
"Unsupported ZMarkup attribute type");
104 template <
typename T>
105 std::string formatDefaultValue(
const T &v)
107 if constexpr (std::is_same_v<T, std::string>)
109 return v.empty() ? std::string(
"\"\"") : v;
111 else if constexpr (std::is_same_v<T, bool>)
113 return v ?
"true" :
"false";
115 else if constexpr (std::is_same_v<T, int>)
117 return std::to_string(v);
119 else if constexpr (std::is_same_v<T, float>)
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);
127 else if constexpr (std::is_same_v<T, std::vector<std::string>>)
130 for (
size_t i = 0; i < v.size(); ++i)
136 return out.empty() ? std::string(
"\"\"") : out;
155 template <
typename T>
156 const T &
get(std::string_view name)
const
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);
164 throw std::logic_error(
"ZMarkup::Attrs::get: type mismatch for attribute '" + std::string(name) +
165 "' on tag '" + tagName_ +
"'");
170 bool has(std::string_view name)
const;
173 const std::string &
text()
const {
return text_; }
179 bool explicitPresent{
false};
182 std::string tagName_;
184 std::unordered_map<std::string, Entry> values_;
194 TagBuilder(
Registry ®, std::string_view name);
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);
201 template <
typename T>
202 TagBuilder &attr(std::string_view name,
204 std::string_view example,
205 std::string_view description)
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);
214 doc_.attrs.push_back(std::move(ad));
219 void create(std::function<std::shared_ptr<Layout>(
const Attrs &)> fn);
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