Skip to content

Commit

Permalink
Fixed OffsetPointer and OffsetArray being nonstandard layout
Browse files Browse the repository at this point in the history
  • Loading branch information
EPICGameGuy committed Nov 22, 2023
1 parent c350ad2 commit 49f44f5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
6 changes: 6 additions & 0 deletions dependencies/egg-library/include/egg/mesh_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ struct MeshFileHeader
OffsetArray<MeshTriangleStripHeader> strips;
};

static_assert(std::is_standard_layout_v<Vector> == true);
static_assert(std::is_standard_layout_v<OffsetArray<Vector>> == true);
static_assert(std::is_standard_layout_v<OffsetArray<Vector2>> == true);
static_assert(std::is_standard_layout_v<OffsetArray<MeshTriangleStripHeader>> == true);
static_assert(std::is_standard_layout_v<MeshFileHeader> == true);

#pragma pack(pop)
34 changes: 23 additions & 11 deletions dependencies/egg-library/include/egg/offset_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#include <span>
#endif

template <typename T, typename size_type_T = uint32_t>
template <typename T>
struct OffsetPointer
{
// Offset (in bytes)
// This is generally an offset from the first byte of THIS OffsetPointer
size_type_T offset;
int32_t offset;

T* get_ptr()
{
Expand All @@ -26,14 +26,27 @@ struct OffsetPointer
}

using elem_T = T;
static_assert(std::is_arithmetic_v<size_type_T> == true);
};

template <typename T, typename size_type_T = uint32_t>
struct OffsetArray: public OffsetPointer<T, size_type_T>
template <typename T>
struct OffsetArray
{
// Offset (in bytes)
// This is generally an offset from the first byte of THIS OffsetPointer
int32_t offset;

// Length of the array (in bytes)
size_type_T length;
int32_t length;

T* get_ptr()
{
return reinterpret_cast<T*>(((uint8_t*)this) + offset);
}

const T* get_ptr() const
{
return reinterpret_cast<const T*>(((const uint8_t*)this) + offset);
}

T* get_array()
{
Expand All @@ -45,13 +58,13 @@ struct OffsetArray: public OffsetPointer<T, size_type_T>
return this->get_ptr();
}

T& operator[](size_t index)
T& operator[](int32_t index)
{
assert(index >= 0 && index < num_elements());
return get_array()[index];
}

const T& operator[](size_t index) const
const T& operator[](int32_t index) const
{
assert(index >= 0 && index < num_elements());
return get_array()[index];
Expand All @@ -77,20 +90,19 @@ struct OffsetArray: public OffsetPointer<T, size_type_T>
return &get_array()[num_elements()];
}


#ifdef __cpp_lib_span
std::span<T> get_span()
{
return std::span<T>(get_array(), num_elements());
}
#endif

size_type_T num_elements() const
int32_t num_elements() const
{
return length / sizeof(T);
}

void set_num_elements(size_type_T num_elements)
void set_num_elements(int32_t num_elements)
{
length = num_elements * sizeof(T);
}
Expand Down
1 change: 1 addition & 0 deletions dependencies/egg-library/src/egg.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "egg/math_types.hpp"
#include "egg/mesh_header.hpp"

#include <sstream>

Expand Down

0 comments on commit 49f44f5

Please sign in to comment.