ReUseX
0.0.5
3D Point Cloud Processing for Building Reuse
Toggle main menu visibility
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
7
#include "
materialepas_property_types.hpp
"
8
#include "
materialepas_traits.hpp
"
9
#include <cstdint>
10
#include <map>
11
#include <optional>
12
#include <string>
13
#include <string_view>
14
#include <vector>
15
16
namespace
reusex::core::serialization
{
17
24
class
PropertyValue
{
25
public
:
32
PropertyValue
(
const
void
*blob_data,
size_t
blob_size,
33
traits::PropertyType
type
);
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
60
private
:
61
std::vector<uint8_t> data_;
62
traits::PropertyType
type_;
63
};
64
72
class
Deserializer
{
73
public
:
84
template
<
typename
T>
85
static
void
deserialize
(T &obj,
86
const
std::map<std::string, PropertyValue> &values);
87
88
private
:
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
110
class
Serializer
{
111
public
:
119
template
<
typename
T>
120
static
std::map<std::string, PropertyValue>
serialize
(
const
T &obj);
121
122
private
:
123
// Type-specific serializers
124
static
PropertyValue
serialize_string(
const
void
*ptr);
125
static
PropertyValue
serialize_integer(
const
void
*ptr);
126
static
PropertyValue
serialize_double(
const
void
*ptr);
127
static
PropertyValue
serialize_boolean(
const
void
*ptr);
128
static
PropertyValue
serialize_tristate(
const
void
*ptr);
129
static
PropertyValue
serialize_string_array(
const
void
*ptr);
130
static
PropertyValue
serialize_enum_value(
const
void
*ptr,
131
std::string_view enum_type);
132
static
PropertyValue
serialize_enum_array(
const
void
*ptr,
133
std::string_view enum_type);
134
static
PropertyValue
135
serialize_object_array(
const
void
*ptr,
136
const
traits::PropertyDescriptor
&desc);
137
};
138
139
// Template implementation must be in header for visibility
140
template
<
typename
T>
141
void
Deserializer::deserialize
(
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) {
162
case
traits::PropertyType::String
:
163
deserialize_string(field_ptr, it->second);
164
break
;
165
case
traits::PropertyType::Integer
:
166
deserialize_integer(field_ptr, it->second);
167
break
;
168
case
traits::PropertyType::Double
:
169
deserialize_double(field_ptr, it->second);
170
break
;
171
case
traits::PropertyType::Boolean
:
172
deserialize_boolean(field_ptr, it->second);
173
break
;
174
case
traits::PropertyType::TriState
:
175
deserialize_tristate(field_ptr, it->second);
176
break
;
177
case
traits::PropertyType::StringArray
:
178
deserialize_string_array(field_ptr, it->second);
179
break
;
180
case
traits::PropertyType::EnumValue
:
181
deserialize_enum_value(field_ptr, it->second, Traits::struct_name());
182
break
;
183
case
traits::PropertyType::EnumArray
:
184
deserialize_enum_array(field_ptr, it->second, Traits::struct_name());
185
break
;
186
case
traits::PropertyType::ObjectArray
:
187
deserialize_object_array(field_ptr, it->second, desc);
188
break
;
189
}
190
}
191
}
192
193
template
<
typename
T>
194
std::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 =
reinterpret_cast<
const
char
*
>
(&obj) + desc.offset;
201
202
// Dispatch to type-specific serializer
203
PropertyValue
value(
""
,
traits::PropertyType::String
);
// placeholder
204
205
switch
(desc.type) {
206
case
traits::PropertyType::String
:
207
value = serialize_string(field_ptr);
208
break
;
209
case
traits::PropertyType::Integer
:
210
value = serialize_integer(field_ptr);
211
break
;
212
case
traits::PropertyType::Double
:
213
value = serialize_double(field_ptr);
214
break
;
215
case
traits::PropertyType::Boolean
:
216
value = serialize_boolean(field_ptr);
217
break
;
218
case
traits::PropertyType::TriState
:
219
value = serialize_tristate(field_ptr);
220
break
;
221
case
traits::PropertyType::StringArray
:
222
value = serialize_string_array(field_ptr);
223
break
;
224
case
traits::PropertyType::EnumValue
:
225
value = serialize_enum_value(field_ptr, Traits::struct_name());
226
break
;
227
case
traits::PropertyType::EnumArray
:
228
value = serialize_enum_array(field_ptr, Traits::struct_name());
229
break
;
230
case
traits::PropertyType::ObjectArray
:
231
value = serialize_object_array(field_ptr, desc);
232
break
;
233
}
234
235
// Store with leksikon_guid as key (or field_name for nested arrays)
236
const
char
*store_key = desc.type ==
traits::PropertyType::ObjectArray
237
? desc.field_name
238
: desc.leksikon_guid;
239
values.emplace(store_key, std::move(value));
240
}
241
242
return
values;
243
}
244
245
}
// namespace reusex::core::serialization
reusex::core::serialization::Deserializer
Deserializer for converting database values to C++ structs.
Definition
materialepas_serialization.hpp:72
reusex::core::serialization::Deserializer::deserialize
static void deserialize(T &obj, const std::map< std::string, PropertyValue > &values)
Deserialize a struct from property value map.
Definition
materialepas_serialization.hpp:141
reusex::core::serialization::PropertyValue
Type-erased property value holder.
Definition
materialepas_serialization.hpp:24
reusex::core::serialization::PropertyValue::type
traits::PropertyType type() const noexcept
Get property type.
Definition
materialepas_serialization.hpp:58
reusex::core::serialization::PropertyValue::as_string
std::string_view as_string() const noexcept
Get BLOB as string view (for String, Integer, Double, etc.).
reusex::core::serialization::PropertyValue::PropertyValue
PropertyValue(const void *blob_data, size_t blob_size, traits::PropertyType type)
Construct from raw BLOB data.
reusex::core::serialization::PropertyValue::blob
const std::vector< uint8_t > & blob() const noexcept
Get raw BLOB data.
Definition
materialepas_serialization.hpp:46
reusex::core::serialization::PropertyValue::PropertyValue
PropertyValue(std::string_view str, traits::PropertyType type)
Construct from string (for convenience).
reusex::core::serialization::Serializer
Serializer for converting C++ structs to database values.
Definition
materialepas_serialization.hpp:110
reusex::core::serialization::Serializer::serialize
static std::map< std::string, PropertyValue > serialize(const T &obj)
Serialize a struct to property value map.
Definition
materialepas_serialization.hpp:194
materialepas_property_types.hpp
materialepas_traits.hpp
reusex::core::serialization
Definition
materialepas_serialization.hpp:16
reusex::core::traits
Definition
materialepas_property_types.hpp:10
reusex::core::traits::PropertyType
PropertyType
Property type enumeration for MaterialPassport serialization.
Definition
materialepas_property_types.hpp:18
reusex::core::traits::PropertyType::String
@ String
std::string
Definition
materialepas_property_types.hpp:20
reusex::core::traits::PropertyType::Boolean
@ Boolean
std::optional<bool>
Definition
materialepas_property_types.hpp:29
reusex::core::traits::PropertyType::EnumValue
@ EnumValue
Enum values (Material, etc.).
Definition
materialepas_property_types.hpp:38
reusex::core::traits::PropertyType::EnumArray
@ EnumArray
std::vector<EnumType>
Definition
materialepas_property_types.hpp:41
reusex::core::traits::PropertyType::StringArray
@ StringArray
std::vector<std::string>
Definition
materialepas_property_types.hpp:35
reusex::core::traits::PropertyType::Integer
@ Integer
std::optional<int>
Definition
materialepas_property_types.hpp:23
reusex::core::traits::PropertyType::TriState
@ TriState
TriState enum (yes/no/unknown).
Definition
materialepas_property_types.hpp:32
reusex::core::traits::PropertyType::Double
@ Double
std::optional<double>
Definition
materialepas_property_types.hpp:26
reusex::core::traits::PropertyType::ObjectArray
@ ObjectArray
std::vector<StructType> (nested objects)
Definition
materialepas_property_types.hpp:44
reusex::core::traits::PropertyDescriptor
Property metadata descriptor.
Definition
materialepas_traits.hpp:20
reusex::core::traits::PropertyTraits
Trait interface for property metadata.
Definition
materialepas_traits.hpp:55
libs
reusex
include
core
materialepas_serialization.hpp
Generated by
1.17.0