Skip to content

Refactor variant_internal.h using C++20 concepts #12529

@radiantgurl

Description

@radiantgurl

Describe the project you are working on

Godot codebase cleanup

Describe the problem or limitation you are having in your project

Godot's codebase continues to expand, and without cleaning it up ever so often it will often result in sub-par performance code. This will result in hard to maintain and understand code, which will put a lot of burden on the maintainers and contributors of this project. The issue encountered is having to modify very many variant files (alongside finding where they are, this is around 52 files afaik), which is a lot of stress for both merge conflicts and for writing it.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

My prosposal branches from the nearly complete pull request #100749 Core: Bump C++ Standard to C++20, adding C++ concepts.

Thru the use of concepts, we can allow C++ to perform the binding for us by verifying certain properties of some objects. This allows the code to be written by the compiler instead of by hand and replaced in all the places where it's required, without the manual labour that goes into this.

Using this, we can stop worrying about missing code during implementation / modification of code. A very good example of something that can be remade is the VariantOps class, providing ways to do operations (such as + or -) between variants using concepts, with no issues.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

The Variant will require a few things merged inside of it, and the dynamic binding would be stored inside VariantTypeInfo and VariantTypeDescription, where one holds the info and the description is to be automatically generated from the info provided.

Registering a type becomes as easy as

template<>
class VariantTypeInfo<::AABB> {
public:
    constexpr VariantType id = VariantType::AABB;
    constexpr const char *name = "AABB";
    constexpr bool primitive = true;
    static void bind_methods(VariantMethodBinding &p_bind); // TBA, operators are to be added automatically.
}

And then an automatically generated TypeDescription with every operation automatically bound to it.

This will mean that two files will require to be modified from now on: variant_internal.h, where it will keep a list of all possible variant types inside a macro (and the macro switch statement for certain operations), variant.h, for the variant enum, and the file of the type itself, where it will be registered.

If this enhancement will not be used often, can it be worked around with a few lines of script?

No

Is there a reason why this should be core and not an add-on in the asset library?

N/A

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions