Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkaratarakis committed Jun 30, 2024
1 parent 8158216 commit 3a7708c
Showing 1 changed file with 122 additions and 12 deletions.
134 changes: 122 additions & 12 deletions test/sub_struct_view_test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "fixed_containers/sub_struct_view.hpp"

#include "fixed_containers/fixed_map.hpp"
#include "fixed_containers/fixed_set.hpp"
#include "fixed_containers/fixed_vector.hpp"
#include "fixed_containers/reflection.hpp"

#include <gtest/gtest.h>

#include <cstddef>
#include <iterator>

namespace fixed_containers::sub_struct_view
Expand All @@ -25,8 +28,27 @@ struct FlatSubStruct1
const double* retain1;
const float* retain2;
};

} // namespace

template <typename S>
static auto extract_field_to_offset_map(const S& instance = {})
{
FixedMap<std::string_view, std::ptrdiff_t, reflection::field_count_of<S>()> field_to_offset{};

Check failure on line 37 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / build

‘constexpr’ call flows off the end of the function

Check failure on line 37 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / build

‘constexpr’ call flows off the end of the function

Check failure on line 37 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / Analyze (cpp, gcc-11, Ninja, Debug, OFF)

‘constexpr’ call flows off the end of the function

Check failure on line 37 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / Analyze (cpp, gcc-11, Ninja, Debug, OFF)

‘constexpr’ call flows off the end of the function

Check failure on line 37 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / build

‘constexpr’ call flows off the end of the function

Check failure on line 37 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / build

‘constexpr’ call flows off the end of the function
reflection::for_each_field(instance,
[&]<class T>(const std::string_view& name, T& field)
{
const std::byte* base_pointer =
reinterpret_cast<const std::byte*>(&instance);
const std::byte* field_pointer =
reinterpret_cast<const std::byte*>(&field);
field_to_offset.try_emplace(
name, std::distance(base_pointer, field_pointer));
});

return field_to_offset;
}

TEST(SubStructView, Flat)
{
FlatSuperStruct1 flat_super_struct_1{};
Expand All @@ -35,33 +57,121 @@ TEST(SubStructView, Flat)

FlatSubStruct1 flat_sub_struct_1{};

FixedMap<std::string_view, std::size_t, 10> field_to_offset{};
std::size_t current_offset{};

reflection::for_each_field(flat_sub_struct_1,
[&]<class T>(const std::string_view& name, T& /*field*/)
{
field_to_offset.try_emplace(name, current_offset);
current_offset += sizeof(T);
});
auto field_to_offset_map = extract_field_to_offset_map(flat_sub_struct_1);

reflection::for_each_field(flat_super_struct_1,
[&]<class T>(const std::string_view& name, T& field)
{
auto it = field_to_offset.find(name);
if (it == field_to_offset.cend())
auto it = field_to_offset_map.find(name);
if (it == field_to_offset_map.cend())
{
return;
}

std::byte* byte_ptr =
reinterpret_cast<std::byte*>(&flat_sub_struct_1);
const std::size_t offset = it->second;
const std::ptrdiff_t offset = it->second;
std::advance(byte_ptr, offset);
T** field_in_struct = reinterpret_cast<T**>(byte_ptr);
*field_in_struct = &field;
});

ASSERT_TRUE(flat_sub_struct_1.retain1 == &flat_super_struct_1.retain1);
ASSERT_TRUE(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, 5> retain1{};
float ignore2{};
};

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


template <class T>
class StridedArrayView
{
FixedVector<std::byte*, reflection::field_count_of<T>()> field_pointers_{};

Check failure on line 109 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / build

‘constexpr’ call flows off the end of the function

Check failure on line 109 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / Analyze (cpp, gcc-11, Ninja, Debug, OFF)

‘constexpr’ call flows off the end of the function

Check failure on line 109 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / build

‘constexpr’ call flows off the end of the function
std::ptrdiff_t stride_;

public:
template <class P>
void update(P& parent)
{
field_pointers_.clear();
stride_ = sizeof(P);

T flat_sub_struct_1{};

auto field_to_offset_map = extract_field_to_offset_map(flat_sub_struct_1);

reflection::for_each_field(parent,
[&]<class F>(const std::string_view& name, F& field)
{
auto it = field_to_offset_map.find(name);
if (it == field_to_offset_map.cend())
{
return;
}

std::byte* byte_ptr =
reinterpret_cast<std::byte*>(&flat_sub_struct_1);
const std::ptrdiff_t offset = it->second;
std::advance(byte_ptr, offset);
F** field_in_struct = reinterpret_cast<F**>(byte_ptr);
*field_in_struct = &field;
});
}

T at(const std::size_t i) const
{
T instance{};
std::size_t field_i = 0;
reflection::for_each_field(instance,
[&]<class F>(const std::string_view& /*name*/, F& field)
{
const PointerAndStride& current_pointer_and_stride =

Check failure on line 148 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / build

‘PointerAndStride’ does not name a type

Check failure on line 148 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / Analyze (cpp, gcc-11, Ninja, Debug, OFF)

‘PointerAndStride’ does not name a type

Check failure on line 148 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / build

‘PointerAndStride’ does not name a type
field_pointers_and_strides_.at(field_i);
std::byte* byte_ptr = reinterpret_cast<std::byte*>(
current_pointer_and_stride.pointer);

Check failure on line 151 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / build

‘current_pointer_and_stride’ was not declared in this scope

Check failure on line 151 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / Analyze (cpp, gcc-11, Ninja, Debug, OFF)

‘current_pointer_and_stride’ was not declared in this scope

Check failure on line 151 in test/sub_struct_view_test.cpp

View workflow job for this annotation

GitHub Actions / build

‘current_pointer_and_stride’ was not declared in this scope
const auto offset = current_pointer_and_stride.stride *
static_cast<std::ptrdiff_t>(i);
std::advance(byte_ptr, offset);
F* field_in_struct = reinterpret_cast<F*>(byte_ptr);
field = *field_in_struct;
field_i++;
});

return instance;
}
};

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

} // namespace

TEST(SubStructView, ArraysOfStructs)
{
StridedArrayView<PointXZ> view{};
(void)view.at(0).x;
}

} // namespace fixed_containers::sub_struct_view

0 comments on commit 3a7708c

Please sign in to comment.