De-duplicate a lot of variant_internal.h
#105246
Open
+143
−625
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.
variant_internal.h
(and similar headers) are currently a big old mess of duplicated logic.This PR is a first step towards de-duplication.
As a codestyle PR, it should not affect performance (and probably won't have noticeable impact on compile time either).
Explanation
In practice, I see the following types / use-cases:
VariantGetInternalPtr
: For types that are stored inVariant
, get a pointer to their value.is_local
.VariantImplicitConvert
(new): For types that are not stored directly inVariant
, but act as if they are, using another type as storage surrogate (e.g.float
or enums).VariantInternalAccessor
: Offers an API to get or set any type that can be stored inVariant
(both direct types and implicit conversion types).Using this logic, structs can be simplified:
VariantGetInternalPtr
has only 3 logical implementations (ignoringObject
): Those that are stored in theVariant
, those that use a pointer to a heap value, and those that usePackedArrayRef
. This can be abstracted and de-duplicated.VariantInitializer
andVariantDefaultInitializer
foris_local
types can just assign to the payload.VariantInternalAccessor
for stored types can access the stored type by reference, and for conversion types can return and accept copies.Other changes
Tighter requirements on the declared types allowed me to spot a few accessor mistakes in the codebase, which I addressed.
Addendum
In follow up PRs, more duplicated logic should be removed from these headers, and duplicated functions and structs should be phased out. This is merely a first step establishing a way forward.