Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework Variant.h #714

Merged
merged 1 commit into from
Oct 7, 2024
Merged

Rework Variant.h #714

merged 1 commit into from
Oct 7, 2024

Conversation

CedNaru
Copy link
Member

@CedNaru CedNaru commented Oct 5, 2024

I noticed that we never added the new PackedVector4Array to the Variant.h.
So I just took the opportunity to rework the file a bit.

The function bodies are almost unchanged, only the general structure of the file changed. Instead of the KtVariant namespace, I have put all methods into the new BufferToVariant and VariantToBuffer structures. Because everything is static, the structures act like a more fine-grained namespace. This is also how Godot does it through its codebase.

Note that I also removed the initializer function for the array of function pointers. It's now done statically, no "if" is required during runtime anymore.

Before :

    static void init_to_kt_methods(void (*to_kt_array[Variant::Type::VARIANT_MAX])(SharedBuffer*, const Variant&)) {
      // Long initializer...
     }

    static void send_variant_to_buffer(const Variant& variant, SharedBuffer* byte_buffer) {
        // must match the value order of godot_variant_type
        static void (*TO_KT_VARIANT_FROM[Variant::Type::VARIANT_MAX])(SharedBuffer*, const Variant&);
        if (unlikely(!TO_KT_VARIANT_FROM[0])) { init_to_kt_methods(TO_KT_VARIANT_FROM); }
        Variant::Type type = variant.get_type();
        TO_KT_VARIANT_FROM[type](byte_buffer, variant);
    }

After:

    static void write_variant(const Variant& variant, SharedBuffer* byte_buffer) {
        // must match the value order of godot_variant_type
        static void (*variant_writers[Variant::VARIANT_MAX])(SharedBuffer*, const Variant&) =  {
              // Long static initializer...
        };

        Variant::Type type = variant.get_type();
        variant_writers[type](byte_buffer, variant);
    }

@CedNaru CedNaru force-pushed the improvement/simplify_variant branch 5 times, most recently from 77bd281 to add6bb6 Compare October 5, 2024 09:03
@CedNaru CedNaru force-pushed the improvement/simplify_variant branch from add6bb6 to 6630d16 Compare October 5, 2024 14:46
@CedNaru CedNaru merged commit 53e8fee into master Oct 7, 2024
57 checks passed
@CedNaru CedNaru deleted the improvement/simplify_variant branch October 7, 2024 08:54
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