MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
CompositeInsertion.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Composite.hpp"
4
5namespace MayaFlux::Kakshya {
6
7/**
8 * @class CompositeInsertion
9 * @brief Mutable insertion adapter for a CompositeArray.
10 *
11 * Appends empty elements, writes fields by name, and copies compatible
12 * Composite values from another array. Copying an element also copies its
13 * present text values and rebases their offsets into the destination's text
14 * store. The adapter borrows its destination array and does not own storage.
15 *
16 * Usage:
17 * @code
18 * CompositeInsertion insert(items);
19 * auto index = insert.append();
20 * insert.set_text(index, "name", "Ada");
21 * insert.set(index, "score", 0.95);
22 * @endcode
23 *
24 * Copying between arrays with the same finalized layout:
25 * @code
26 * CompositeInsertion destination_insert(destination);
27 * auto source = items.at(0);
28 * auto copied_index = destination_insert.append(*source);
29 * @endcode
30 *
31 * @note The destination CompositeArray must outlive this adapter. Field
32 * writes require exact declared types; they never coerce numeric values.
33 */
34class MAYAFLUX_API CompositeInsertion {
35public:
36 /**
37 * @brief Borrow the array into which elements and fields will be written.
38 * @param array Destination array, which must outlive this adapter.
39 */
40 explicit CompositeInsertion(CompositeArray& array) noexcept
41 : m_array(&array)
42 {
43 }
44
45 /**
46 * @brief Append an empty element.
47 * @return Index of the new element.
48 * @throws std::length_error If the packed storage cannot grow.
49 */
50 [[nodiscard]] size_t append() { return m_array->append(); }
51 /**
52 * @brief Copy one Composite with a matching layout into this array.
53 * @param source Element to copy, including its present text fields.
54 * @return New element index, or std::nullopt for incompatible layout,
55 * invalid source text bounds, or insufficient storage capacity.
56 */
57 [[nodiscard]] std::optional<size_t> append(const Composite& source);
58
59 /**
60 * @brief Write an arithmetic field of an existing element.
61 * @tparam T Exact type declared for the field.
62 * @param index Element index.
63 * @param field_name Field to write.
64 * @param value Value to store.
65 * @return True on success; false for an invalid index, name, or type.
66 */
67 template <typename T>
68 requires ArithmeticData<T>
69 bool set(size_t index, std::string_view field_name, T value)
70 {
71 return m_array->set(index, field_name, value);
72 }
73
74 /**
75 * @brief Write UTF-8 bytes to a std::string field.
76 * @param index Element index.
77 * @param field_name Text field to write.
78 * @param value UTF-8 bytes to append to the destination text store.
79 * @return True on success; false for an invalid index, name, type, or
80 * text storage size.
81 */
82 bool set_text(size_t index, std::string_view field_name, std::string_view value)
83 {
84 return m_array->set_text(index, field_name, value);
85 }
86
87 /** @brief Write a present NULL to a dynamic field. */
88 bool set_null(size_t index, std::string_view field_name)
89 {
90 return m_array->set_null(index, field_name);
91 }
92 /** @brief Write binary bytes to a fixed or dynamic BLOB field. */
93 bool set_blob(size_t index, std::string_view field_name, std::span<const uint8_t> value)
94 {
95 return m_array->set_blob(index, field_name, value);
96 }
97
98 /**
99 * @brief Mark a named field absent in an existing element.
100 * @param index Element index.
101 * @param field_name Field to clear.
102 * @return True on success; false for an invalid index or field name.
103 */
104 bool clear(size_t index, std::string_view field_name)
105 {
106 return m_array->clear(index, field_name);
107 }
108
109private:
111};
112
113}
glm::vec3 value
uint32_t index
Definition VKDevice.cpp:122
Owns a sequence of values that share a CompositeLayout.
CompositeInsertion(CompositeArray &array) noexcept
Borrow the array into which elements and fields will be written.
bool set(size_t index, std::string_view field_name, T value)
Write an arithmetic field of an existing element.
bool clear(size_t index, std::string_view field_name)
Mark a named field absent in an existing element.
bool set_text(size_t index, std::string_view field_name, std::string_view value)
Write UTF-8 bytes to a std::string field.
bool set_blob(size_t index, std::string_view field_name, std::span< const uint8_t > value)
Write binary bytes to a fixed or dynamic BLOB field.
size_t append()
Append an empty element.
bool set_null(size_t index, std::string_view field_name)
Write a present NULL to a dynamic field.
Mutable insertion adapter for a CompositeArray.
Read view of one value in a CompositeArray.