61template <
typename Source,
typename Access = Reflect::RecordAccess<Source>>
71 using Placer = std::function<std::optional<uint32_t>(
Surface&, std::string&)>;
75 const Source&,
const Source&, std::string&)>;
91 m_factories.insert_or_assign(name, std::move(instantiator));
113 typename Factory,
typename Initial>
117 Factory factory, Initial initial,
118 std::type_identity_t<std::function<
void(
const Input&,
const Placement&,
Tend<T>&)>> placed = {})
124 using Maker = std::function<std::optional<Placer>(
125 const Input&,
const Placement&, std::string&)>;
127 Maker maker = [factory = std::move(factory), initial = std::move(initial),
128 placed = std::move(placed)](
129 const Input& decoded_input,
const Placement& decoded_placement,
130 std::string& error) -> std::optional<Placer> {
131 auto geometry = std::invoke(factory, decoded_input, decoded_placement, error);
134 error =
"could not create the geometry";
137 std::optional<T> value;
138 if constexpr (std::is_member_object_pointer_v<Initial>) {
139 if constexpr (std::is_invocable_v<Initial, const Input&>)
140 value =
static_cast<T
>(std::invoke(initial, decoded_input));
142 value =
static_cast<T
>(std::invoke(initial, decoded_placement));
144 value = std::invoke(initial, decoded_input, decoded_placement, error);
148 error =
"could not create the initial state";
151 std::function<void(Tend<T>&)> after;
153 after = [placed, decoded_input, decoded_placement](Tend<T>& tend) {
154 placed(decoded_input, decoded_placement, tend);
157 return make_placer<T>(std::move(*geometry), std::move(*value), std::move(after));
161 const Source& factory_element,
const Source& instance,
162 std::string&
error) -> std::optional<Placer> {
164 const auto decoded_input = input.
read(factory_element, error, &where);
165 if (!decoded_input) {
169 const auto decoded_placement = placement.read(instance, error, &where);
170 if (!decoded_placement) {
174 auto placer = maker(*decoded_input, *decoded_placement, error);
181 const Source& factory_element,
const Kakshya::CompositeArray& rows,
182 std::string&
error) -> std::optional<std::vector<Placer>> {
184 const auto decoded_input = input.
read(factory_element, error, &where);
185 if (!decoded_input) {
191 error =
located(Access::location(factory_element), error);
195 std::vector<Placer> placers;
196 placers.reserve(placements->size());
197 for (
const Placement& decoded_placement : *placements) {
198 auto placer = maker(*decoded_input, decoded_placement, error);
200 error =
located(Access::location(factory_element), error);
203 placers.push_back(std::move(*placer));
208 m_factories.insert_or_assign(name, std::move(single));
209 m_bulk.insert_or_assign(name, std::move(bulk));
220 std::string& error,
bool replace =
false)
225 const std::string target = std::string(prefix) + entry.first;
226 const auto bulk = other.
m_bulk.
find(entry.first);
228 m_bulk.insert_or_assign(target, bulk->second);
237 [[nodiscard]]
bool contains(std::string_view name)
const
243 [[nodiscard]] std::vector<std::string>
names()
const
245 std::vector<std::string> result;
247 result.push_back(entry.first);
255 return found ==
m_factories.end() ? nullptr : &found->second;
261 const auto found =
m_bulk.find(name);
262 return found ==
m_bulk.end() ? nullptr : &found->second;
266 [[nodiscard]]
static std::string
located(uint64_t row, std::string_view message)
272 template <
typename T>
274 std::function<
void(
Tend<T>&)> after)
276 return [form = std::move(form), initial = std::move(initial),
277 after = std::move(after)](
Surface& surface, std::string& error) -> std::optional<uint32_t> {
284 error =
"could not place the element";
289 return tend.
result->element.id;
Illustrative geometry functions for common Mapped use cases.
Owns a sequence of values that share a CompositeLayout.
const_iterator end() const noexcept
const_iterator find(const K &key) const
The entry for key, or end() when absent.
Map kept as one sorted vector, cheap to copy.
RecordBinding & ignore(std::initializer_list< std::string_view > names)
Leave these attributes to other readers of the same element.
std::optional< Input > read(const Source &source, std::string &error, uint64_t *error_location=nullptr) const
Decode the source element into a new Input.
RecordBinding & ignore_prefix(std::string prefix)
Leave every attribute whose name begins with a prefix to other readers.
RecordBinding & ignore_child(std::string child_name)
Leave every child element of this name to another reader.
Bind described attributes and configured child collections of a source element.
A type whose generated sidecar defines Generated::Description<T>::members().
std::optional< std::vector< T > > records_of(const Kakshya::CompositeArray &rows, std::string &error)
Decode every row of a typed Composite array into values.
void error(Component component, Context context, std::source_location location, std::string_view message)
Log an error message and optionally throw an exception.
std::string located_message(uint64_t location, std::string_view message)
A diagnostic message prefixed with the location of the element it concerns.