Skip to content

feat(Variant): add FromValue/ToValue helpers#219

Merged
wopss merged 17 commits into
wopss:masterfrom
rayshader:feat/variant
Jun 7, 2026
Merged

feat(Variant): add FromValue/ToValue helpers#219
wopss merged 17 commits into
wopss:masterfrom
rayshader:feat/variant

Conversation

@poirierlouis

@poirierlouis poirierlouis commented May 19, 2026

Copy link
Copy Markdown
Contributor

Changes

  • convert a Variant to/from a value. Support fundamental and common types.
  • add constness on Extract method, implementation is already readonly.

Usages
Static:

RED4ext::Variant data;
RED4ext::Variant::FromValue<uint64_t>(42, data); // return false on error

uint64_t hash;
RED4ext::Variant::ToValue(data, hash); // return false on error

Static without direct error feedback:

RED4ext::Variant data = RED4ext::Variant::FromValue<uint64_t>(42);
const auto hash = RED4ext::Variant::ToValue<uint64_t>(data);

Set/Get directly on an instance:

RED4ext::Variant data;
data.Set<uint64_t>(42); // return false on error

const auto hash = data.Get<uint64_t>();

Notes
This is a first step. In the end, integrating implementation from RedLib would likely be the best solution for end-users. I left a comment about this.

@poirierlouis poirierlouis marked this pull request as ready for review May 20, 2026 17:03
@poirierlouis

Copy link
Copy Markdown
Contributor Author

I'll take a look at Resolving from RedLib, and write a dedicated PR.

Comment thread include/RED4ext/NativeTypes-inl.hpp Outdated

@wopss wopss left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment thread include/RED4ext/NativeTypes-inl.hpp Outdated
Comment thread include/RED4ext/NativeTypes-inl.hpp Outdated
Comment thread include/RED4ext/NativeTypes-inl.hpp Outdated
Comment thread include/RED4ext/NativeTypes-inl.hpp Outdated
Comment thread include/RED4ext/NativeTypes-inl.hpp Outdated
Comment thread include/RED4ext/NativeTypes-inl.hpp Outdated
Comment thread include/RED4ext/NativeTypes.hpp Outdated
Implementation is already using only `const` methods.
Convert a Variant to/from a value. Support fundamental and common types.
Call is more elegant to use but the end-user can't check for an error at the same time.
- `ToValue` returns `std::nullopt` when value's type is unsupported
- `FromValue` returns `Variant` directly, user can call `IsEmpty` to check for an error/unsupported type.
@wopss wopss requested a review from psiberx May 30, 2026 20:26
@poirierlouis

Copy link
Copy Markdown
Contributor Author

I'll update CHANGELOG and I guess it will be ready to merge?

@wopss

wopss commented Jun 1, 2026

Copy link
Copy Markdown
Owner

Yes.

Comment thread include/RED4ext/NativeTypes.hpp Outdated
Comment thread include/RED4ext/NativeTypes-inl.hpp Outdated
Comment thread include/RED4ext/NativeTypes-inl.hpp Outdated
Comment thread include/RED4ext/RTTI/IType.hpp Outdated
@wopss wopss merged commit 109b585 into wopss:master Jun 7, 2026
9 checks passed
@poirierlouis poirierlouis deleted the feat/variant branch June 7, 2026 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants