Skip to content

Commit

Permalink
[Runtime]Adds one new get_struct_properties function that will retu…
Browse files Browse the repository at this point in the history
…rn properties of base type for one structure type. (#84)

[Runtime]Fix the bug that causes properties in base type of one derived structure type not get serialized/deserialized.
  • Loading branch information
JX-Master authored Nov 13, 2024
1 parent 8d670ab commit 9f0b109
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Modules/Luna/Runtime/Reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,16 @@ namespace Luna
//! * `type` must specify one valid type object.
LUNA_RUNTIME_API Span<const StructurePropertyDesc> get_struct_properties(typeinfo_t type);

//! Gets properties of the specified structure.
//! @param[in] type The type to query.
//! @param[out] out_properties Returns properties of the specified structure. Existing elements in the vector will not be modified.
//! If `type` is not a structure or generic structure instanced type, this vector is not changed.
//! @param[in] include_base_type Whether to query properties that belongs to the base type of this structure type if this structure type has
//! base type. If this is `false`, only properties that belongs to this type (not the base type) will be returned.
//! @par Valid Usage
//! * `type` must specify one valid type object.
LUNA_RUNTIME_API void get_struct_properties(typeinfo_t type, Vector<StructurePropertyDesc>& out_properties, bool include_base_type = true);

//! Gets the base type of the specified type.
//! @param[in] type The type to query.
//! @return Returns the base type of the specified type.
Expand Down
6 changes: 4 additions & 2 deletions Modules/Luna/Runtime/Source/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace Luna
Variant ret(VariantType::object);
lutry
{
auto properties = get_struct_properties(type);
Vector<StructurePropertyDesc> properties;
get_struct_properties(type, properties);
for(auto& prop : properties)
{
if (is_type_serializable(prop.type))
Expand All @@ -38,7 +39,8 @@ namespace Luna
{
lutry
{
auto properties = get_struct_properties(type);
Vector<StructurePropertyDesc> properties;
get_struct_properties(type, properties);
for(auto& prop : properties)
{
auto& prop_data = data[prop.name];
Expand Down
28 changes: 28 additions & 0 deletions Modules/Luna/Runtime/Source/TypeInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,34 @@ namespace Luna
}
return Span<const StructurePropertyDesc>();
}
LUNA_RUNTIME_API void get_struct_properties(typeinfo_t type, Vector<StructurePropertyDesc>& out_properties, bool include_base_type)
{
TypeInfo* t = (TypeInfo*)type;
switch (t->kind)
{
case TypeKind::structure:
{
StructureTypeInfo* type = (StructureTypeInfo*)t;
if(include_base_type && type->base_type)
{
get_struct_properties(type->base_type, out_properties, true);
}
out_properties.insert(out_properties.end(), type->property_descs.begin(), type->property_descs.end());
break;
}
case TypeKind::generic_structure_instanced:
{
GenericStructureInstancedTypeInfo* type = (GenericStructureInstancedTypeInfo*)t;
if(include_base_type && type->base_type)
{
get_struct_properties(type->base_type, out_properties, true);
}
out_properties.insert(out_properties.end(), type->property_descs.begin(), type->property_descs.end());
break;
}
default: break;
}
}
LUNA_RUNTIME_API typeinfo_t get_base_type(typeinfo_t type)
{
TypeInfo* t = (TypeInfo*)type;
Expand Down

0 comments on commit 9f0b109

Please sign in to comment.