Skip to content

Commit

Permalink
[WIP] Introduce ArrayOfStructsView
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkaratarakis committed Jul 1, 2024
1 parent 2ec72ac commit 1c49cfc
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
39 changes: 39 additions & 0 deletions include/fixed_containers/sub_struct_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,43 @@ void sub_struct_view_of(Super& super_struct,
sub_struct_field_properties);
}

template <typename SuperStruct, typename SubStruct>
class StridedArrayView
{
FixedMap<std::string_view, FieldProperties, reflection::field_count_of<SuperStruct>()>
super_struct_field_properties_;
FixedMap<std::string_view, FieldProperties, reflection::field_count_of<SubStruct>()>
sub_struct_field_properties_;
std::byte* base_array_super_struct_ptr_;
std::size_t size_;

public:
StridedArrayView()
: super_struct_field_properties_(extract_field_properties_of<SuperStruct>())
, sub_struct_field_properties_(extract_field_properties_of<SubStruct>())
, base_array_super_struct_ptr_{nullptr}
{
}

public:
template <typename ArrayOfSuperStruct>
void update(ArrayOfSuperStruct& super_struct_instance)
{
base_array_super_struct_ptr_ = memory::addressof_as_mutable_byte_ptr(super_struct_instance);
size_ = super_struct_instance.size();
}

SubStruct at(const std::size_t i) const
{
SubStruct instance{};
std::byte* base_of_ith_entry = std::next(
base_array_super_struct_ptr_, static_cast<std::ptrdiff_t>(i * sizeof(SuperStruct)));
sub_struct_view::sub_struct_view_of(base_of_ith_entry,
super_struct_field_properties_,
memory::addressof_as_mutable_byte_ptr(instance),
sub_struct_field_properties_);
return instance;
}
};

} // namespace fixed_containers::sub_struct_view
47 changes: 47 additions & 0 deletions test/sub_struct_view_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,53 @@ TEST(SubStructView, SubStructViewOf)
ASSERT_EQ(flat_sub_struct_1.retain1, &flat_super_struct_1.retain1);
ASSERT_EQ(flat_sub_struct_1.retain2, &flat_super_struct_1.retain2);
}

namespace
{
struct PointXYZ
{
double x{};
double y{};
double z{};
};

struct FlatSuperStruct2
{
int ignore1{};
std::array<PointXYZ, 3> retain1{};
float ignore2{};
};

struct PointXZ
{
const double* z{};
const double* x{};
};

struct FlatSubStruct2
{
StridedArrayView<PointXYZ, PointXZ> retain1{};
};

} // namespace

TEST(SubStructView, ArraysOfStructs)
{
FlatSuperStruct2 flat_super_struct_2{};
FlatSubStruct2 flat_sub_struct_2{};

flat_sub_struct_2.retain1.update(flat_super_struct_2.retain1);

ASSERT_EQ(flat_sub_struct_2.retain1.at(0).x, &flat_super_struct_2.retain1.at(0).x);
ASSERT_EQ(flat_sub_struct_2.retain1.at(0).z, &flat_super_struct_2.retain1.at(0).z);

ASSERT_EQ(flat_sub_struct_2.retain1.at(1).x, &flat_super_struct_2.retain1.at(1).x);
ASSERT_EQ(flat_sub_struct_2.retain1.at(1).z, &flat_super_struct_2.retain1.at(1).z);

ASSERT_EQ(flat_sub_struct_2.retain1.at(2).x, &flat_super_struct_2.retain1.at(2).x);
ASSERT_EQ(flat_sub_struct_2.retain1.at(2).z, &flat_super_struct_2.retain1.at(2).z);
}

} // namespace fixed_containers::sub_struct_view

#endif

0 comments on commit 1c49cfc

Please sign in to comment.