ReUseX  0.0.5
3D Point Cloud Processing for Building Reuse
Loading...
Searching...
No Matches
materialepas_serialization.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Povl Filip Sonne-Frederiksen
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#pragma once
6
9#include <cstdint>
10#include <map>
11#include <optional>
12#include <string>
13#include <string_view>
14#include <vector>
15
17
25public:
32 PropertyValue(const void *blob_data, size_t blob_size,
34
40 PropertyValue(std::string_view str, traits::PropertyType type);
41
46 const std::vector<uint8_t> &blob() const noexcept { return data_; }
47
52 std::string_view as_string() const noexcept;
53
58 traits::PropertyType type() const noexcept { return type_; }
59
60private:
61 std::vector<uint8_t> data_;
63};
64
73public:
84 template <typename T>
85 static void deserialize(T &obj,
86 const std::map<std::string, PropertyValue> &values);
87
88private:
89 // Type-specific deserializers (throws on type mismatch/parse error)
90 static void deserialize_string(void *ptr, const PropertyValue &value);
91 static void deserialize_integer(void *ptr, const PropertyValue &value);
92 static void deserialize_double(void *ptr, const PropertyValue &value);
93 static void deserialize_boolean(void *ptr, const PropertyValue &value);
94 static void deserialize_tristate(void *ptr, const PropertyValue &value);
95 static void deserialize_string_array(void *ptr, const PropertyValue &value);
96 static void deserialize_enum_value(void *ptr, const PropertyValue &value,
97 std::string_view enum_type);
98 static void deserialize_enum_array(void *ptr, const PropertyValue &value,
99 std::string_view enum_type);
100 static void deserialize_object_array(void *ptr, const PropertyValue &value,
101 const traits::PropertyDescriptor &desc);
102};
103
111public:
119 template <typename T>
120 static std::map<std::string, PropertyValue>
121 serialize(const T &obj);
122
123private:
124 // Type-specific serializers
125 static PropertyValue serialize_string(const void *ptr);
126 static PropertyValue serialize_integer(const void *ptr);
127 static PropertyValue serialize_double(const void *ptr);
128 static PropertyValue serialize_boolean(const void *ptr);
129 static PropertyValue serialize_tristate(const void *ptr);
130 static PropertyValue serialize_string_array(const void *ptr);
131 static PropertyValue serialize_enum_value(const void *ptr,
132 std::string_view enum_type);
133 static PropertyValue serialize_enum_array(const void *ptr,
134 std::string_view enum_type);
135 static PropertyValue serialize_object_array(
136 const void *ptr, const traits::PropertyDescriptor &desc);
137};
138
139// Template implementation must be in header for visibility
140template <typename T>
142 T &obj, const std::map<std::string, PropertyValue> &values) {
143 using Traits = traits::PropertyTraits<T>;
144
145 for (size_t i = 0; i < Traits::property_count(); ++i) {
146 const auto &desc = Traits::properties()[i];
147
148 // Find property by leksikon_guid (or field_name for nested arrays)
149 const char *lookup_key = desc.type == traits::PropertyType::ObjectArray
150 ? desc.field_name
151 : desc.leksikon_guid;
152 auto it = values.find(lookup_key);
153 if (it == values.end()) {
154 continue; // Use default value (field already initialized)
155 }
156
157 // Calculate field pointer
158 void *field_ptr = reinterpret_cast<char *>(&obj) + desc.offset;
159
160 // Dispatch to type-specific deserializer
161 switch (desc.type) {
163 deserialize_string(field_ptr, it->second);
164 break;
166 deserialize_integer(field_ptr, it->second);
167 break;
169 deserialize_double(field_ptr, it->second);
170 break;
172 deserialize_boolean(field_ptr, it->second);
173 break;
175 deserialize_tristate(field_ptr, it->second);
176 break;
178 deserialize_string_array(field_ptr, it->second);
179 break;
181 deserialize_enum_value(field_ptr, it->second, Traits::struct_name());
182 break;
184 deserialize_enum_array(field_ptr, it->second, Traits::struct_name());
185 break;
187 deserialize_object_array(field_ptr, it->second, desc);
188 break;
189 }
190 }
191}
192
193template <typename T>
194std::map<std::string, PropertyValue> Serializer::serialize(const T &obj) {
195 using Traits = traits::PropertyTraits<T>;
196 std::map<std::string, PropertyValue> values;
197
198 for (size_t i = 0; i < Traits::property_count(); ++i) {
199 const auto &desc = Traits::properties()[i];
200 const void *field_ptr =
201 reinterpret_cast<const char *>(&obj) + desc.offset;
202
203 // Dispatch to type-specific serializer
204 PropertyValue value("", traits::PropertyType::String); // placeholder
205
206 switch (desc.type) {
208 value = serialize_string(field_ptr);
209 break;
211 value = serialize_integer(field_ptr);
212 break;
214 value = serialize_double(field_ptr);
215 break;
217 value = serialize_boolean(field_ptr);
218 break;
220 value = serialize_tristate(field_ptr);
221 break;
223 value = serialize_string_array(field_ptr);
224 break;
226 value = serialize_enum_value(field_ptr, Traits::struct_name());
227 break;
229 value = serialize_enum_array(field_ptr, Traits::struct_name());
230 break;
232 value = serialize_object_array(field_ptr, desc);
233 break;
234 }
235
236 // Store with leksikon_guid as key (or field_name for nested arrays)
237 const char *store_key = desc.type == traits::PropertyType::ObjectArray
238 ? desc.field_name
239 : desc.leksikon_guid;
240 values.emplace(store_key, std::move(value));
241 }
242
243 return values;
244}
245
246} // namespace reusex::core::serialization
Deserializer for converting database values to C++ structs.
static void deserialize(T &obj, const std::map< std::string, PropertyValue > &values)
Deserialize a struct from property value map.
traits::PropertyType type() const noexcept
Get property type.
std::string_view as_string() const noexcept
Get BLOB as string view (for String, Integer, Double, etc.).
PropertyValue(const void *blob_data, size_t blob_size, traits::PropertyType type)
Construct from raw BLOB data.
const std::vector< uint8_t > & blob() const noexcept
Get raw BLOB data.
PropertyValue(std::string_view str, traits::PropertyType type)
Construct from string (for convenience).
Serializer for converting C++ structs to database values.
static std::map< std::string, PropertyValue > serialize(const T &obj)
Serialize a struct to property value map.
PropertyType
Property type enumeration for MaterialPassport serialization.
@ TriState
TriState enum (yes/no/unknown).
@ ObjectArray
std::vector<StructType> (nested objects)
Trait interface for property metadata.