Add pointer-to-member member function overload #1021
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changelog
untyped_component
.size
andalignment
fromEcsMetaType
.Details
Pointer-to-member Member Function
This new overload simplifies adding members with offsets and also helps ensure the offset is correct even if the structure changes or padding is inserted before/after fields, it also helps deal with offsets caused by inheritance with members or virtual functions.
Using the example struct. I've included how this would usually be laid out in memory.
You can register the component with the padding included like this:
Or if you wanted to ignore the padding you would need to add the members with their offsets for every member.
With this change, this becomes simplified to the following:
The pointer-to-member overload also supports fixed-size arrays so you could register the padding like the following and it will be registered the same as doing
.member<char>("pad", 2, offsetof(SomeData, pad))
.Packed Structs
I've modified the case when an explicit offset is provided to use the
alignment
already stored inEcsComponent
for theEcsStruct
, this allows for the alignment of the struct to differ from the alignment of the members.During this change I also noticed that the member count was being ignored in this case so I also updated the code to correctly calculate the size for
ecs_member_t
.From this, I then calculate the expected minimum size of the struct using the member offset and the member size calculated by the size and count, this will now provide an error if a member is added with an offset and size that would exceed the size of the struct.
Because of this change, I had to adjust the test case
StructTypes_partial_type_custom_offset
becausepartial
is set to true if the last member of the field is added with an offset because the expected size and actual size matches.So with this change using the same example struct from before but this time packed. (Using MSVC)
This produces the output: