MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
AttributeChecks.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "RecordBinding.hpp"
4
5namespace MayaFlux::Reflect {
6
7/**
8 * @brief A diagnostic message prefixed with the location of the element it concerns.
9 * @param location Position reported by RecordAccess::location, such as an XML row.
10 */
11[[nodiscard]] inline std::string located_message(uint64_t location, std::string_view message)
12{
13 return "row " + std::to_string(location) + ": " + std::string(message);
14}
15
16/**
17 * @class AttributeIndex
18 * @brief The values of one attribute across a tree, each claimed by one element.
19 *
20 * Claiming a value records the element that holds it, and a second claim of the
21 * same value is refused with the location of the first. It serves any source
22 * with a RecordAccess, so the same check holds for an XML document, a script
23 * or any other element tree.
24 *
25 * Usage:
26 * @code
27 * AttributeIndex<Element> ids("id");
28 * std::string error;
29 * for (const Element& element : elements) {
30 * if (!ids.claim(element, error))
31 * return false;
32 * }
33 * @endcode
34 *
35 * @tparam Source Element type with a RecordAccess specialization.
36 */
37template <typename Source, typename Access = RecordAccess<Source>>
39public:
40 /** @param attribute Name of the attribute whose values are claimed. */
41 explicit AttributeIndex(std::string attribute)
42 : m_attribute(std::move(attribute))
43 {
44 }
45
46 /**
47 * @brief Claim the attribute's value for an element.
48 * @param error Receives a located message when false is returned: the
49 * attribute is missing or empty, or another element already holds the value.
50 */
51 bool claim(const Source& source, std::string& error)
52 {
53 const auto value = Access::attribute(source, m_attribute);
54 if (!value || value->empty()) {
55 error = located_message(Access::location(source),
56 "<" + std::string(Access::name(source)) + "> needs a non-empty " + m_attribute);
57 return false;
58 }
59 const auto [slot, inserted] = m_claimed.emplace(std::string(*value), Access::location(source));
60 if (!inserted) {
61 error = located_message(Access::location(source),
62 m_attribute + " " + std::string(*value) + " is already used at row "
63 + std::to_string(slot->second));
64 return false;
65 }
66 return true;
67 }
68
69 [[nodiscard]] bool contains(std::string_view value) const
70 {
71 return m_claimed.find(value) != m_claimed.end();
72 }
73
74 /** @brief The location of the element that holds a value, or nullopt. */
75 [[nodiscard]] std::optional<uint64_t> location_of(std::string_view value) const
76 {
77 const auto found = m_claimed.find(value);
78 if (found == m_claimed.end())
79 return std::nullopt;
80 return found->second;
81 }
82
83 [[nodiscard]] const std::string& attribute() const noexcept { return m_attribute; }
84
85private:
86 std::string m_attribute;
87 std::map<std::string, uint64_t, std::less<>> m_claimed;
88};
89
90/**
91 * @brief Check that an element's attribute names a value that exists.
92 *
93 * @param attribute Name of the referring attribute, which also names what it refers to.
94 * @param exists Callable taking the value and returning whether it is known,
95 * for instance AttributeIndex::contains or a lookup in any container.
96 * @param error Receives a located message when nullopt is returned: the attribute
97 * is missing, or its value is unknown.
98 * @return The value of the attribute.
99 */
100template <typename Source, typename Access = RecordAccess<Source>, typename Exists>
101[[nodiscard]] std::optional<std::string_view> check_reference(const Source& source,
102 std::string_view attribute, Exists&& exists, std::string& error)
103{
104 const auto value = Access::attribute(source, attribute);
105 if (!value) {
106 error = located_message(Access::location(source),
107 "<" + std::string(Access::name(source)) + "> needs a " + std::string(attribute) + " attribute");
108 return std::nullopt;
109 }
110 if (!std::invoke(std::forward<Exists>(exists), *value)) {
111 error = located_message(Access::location(source),
112 "<" + std::string(Access::name(source)) + "> refers to unknown " + std::string(attribute)
113 + " " + std::string(*value));
114 return std::nullopt;
115 }
116 return value;
117}
118
119}
AttributeIndex(std::string attribute)
bool contains(std::string_view value) const
std::map< std::string, uint64_t, std::less<> > m_claimed
std::optional< uint64_t > location_of(std::string_view value) const
The location of the element that holds a value, or nullopt.
const std::string & attribute() const noexcept
bool claim(const Source &source, std::string &error)
Claim the attribute's value for an element.
std::string located_message(uint64_t location, std::string_view message)
A diagnostic message prefixed with the location of the element it concerns.
std::optional< std::string_view > check_reference(const Source &source, std::string_view attribute, Exists &&exists, std::string &error)
Check that an element's attribute names a value that exists.