Compare commits

...

4 Commits

Author SHA1 Message Date
Thaddeus Crews 5e793467f7
Merge dfdc7bf971 into 0145e900f3 2024-01-20 03:52:40 +08:00
David Snopek 0145e900f3
Merge pull request #1357 from bruvzg/cpp_64
Switch to 64-bit ints.
2024-01-19 12:15:55 -06:00
Thaddeus Crews dfdc7bf971
Add type hints to `binding_generator.py` 2024-01-18 15:23:06 -06:00
bruvzg 59a5a8b104
Switch to 64-bit ints. 2024-01-10 15:36:36 +02:00
7 changed files with 387 additions and 281 deletions

View File

@ -4,9 +4,10 @@ import json
import re import re
import shutil import shutil
from pathlib import Path from pathlib import Path
from typing import Tuple, Set, Dict, List, Any
def generate_mod_version(argcount, const=False, returns=False): def generate_mod_version(argcount: int, const: bool = False, returns: bool = False) -> str:
s = """ s = """
#define MODBIND$VER($RETTYPE m_name$ARG) \\ #define MODBIND$VER($RETTYPE m_name$ARG) \\
virtual $RETVAL _##m_name($FUNCARGS) $CONST override; \\ virtual $RETVAL _##m_name($FUNCARGS) $CONST override; \\
@ -48,7 +49,7 @@ virtual $RETVAL _##m_name($FUNCARGS) $CONST override; \\
return s return s
def generate_wrappers(target): def generate_wrappers(target: Path) -> None:
max_versions = 12 max_versions = 12
txt = """ txt = """
@ -70,7 +71,7 @@ def generate_wrappers(target):
f.write(txt) f.write(txt)
def get_file_list(api_filepath, output_dir, headers=False, sources=False): def get_file_list(api_filepath: str, output_dir: str, headers: bool = False, sources: bool = False) -> List[str]:
api = {} api = {}
files = [] files = []
with open(api_filepath, encoding="utf-8") as api_file: with open(api_filepath, encoding="utf-8") as api_file:
@ -137,18 +138,18 @@ def get_file_list(api_filepath, output_dir, headers=False, sources=False):
return files return files
def print_file_list(api_filepath, output_dir, headers=False, sources=False): def print_file_list(api_filepath: str, output_dir: str, headers: bool = False, sources: bool = False) -> None:
print(*get_file_list(api_filepath, output_dir, headers, sources), sep=";", end=None) print(*get_file_list(api_filepath, output_dir, headers, sources), sep=";", end=None)
def scons_emit_files(target, source, env): def scons_emit_files(target: Any, source: Any, env: Any) -> Tuple[List[Any], Any]:
files = [env.File(f) for f in get_file_list(str(source[0]), target[0].abspath, True, True)] files = [env.File(f) for f in get_file_list(str(source[0]), target[0].abspath, True, True)]
env.Clean(target, files) env.Clean(target, files)
env["godot_cpp_gen_dir"] = target[0].abspath env["godot_cpp_gen_dir"] = target[0].abspath
return files, source return files, source
def scons_generate_bindings(target, source, env): def scons_generate_bindings(target: Any, source: Any, env: Any) -> None:
generate_bindings( generate_bindings(
str(source[0]), str(source[0]),
env["generate_template_get_node"], env["generate_template_get_node"],
@ -159,9 +160,9 @@ def scons_generate_bindings(target, source, env):
return None return None
def generate_bindings(api_filepath, use_template_get_node, bits="64", precision="single", output_dir="."): def generate_bindings(
api = None api_filepath: str, use_template_get_node: bool, bits: str = "64", precision: str = "single", output_dir: str = "."
) -> None:
target_dir = Path(output_dir) / "gen" target_dir = Path(output_dir) / "gen"
with open(api_filepath, encoding="utf-8") as api_file: with open(api_filepath, encoding="utf-8") as api_file:
@ -192,7 +193,7 @@ native_structures = []
singletons = [] singletons = []
def generate_builtin_bindings(api, output_dir, build_config): def generate_builtin_bindings(api: Dict[str, Any], output_dir: Path, build_config: str) -> None:
global builtin_classes global builtin_classes
core_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "core" core_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "core"
@ -222,7 +223,7 @@ def generate_builtin_bindings(api, output_dir, build_config):
# Create a file for Variant size, since that class isn't generated. # Create a file for Variant size, since that class isn't generated.
variant_size_filename = include_gen_folder / "variant_size.hpp" variant_size_filename = include_gen_folder / "variant_size.hpp"
with variant_size_filename.open("+w", encoding="utf-8") as variant_size_file: with variant_size_filename.open("+w", encoding="utf-8") as variant_size_file:
variant_size_source = [] variant_size_source: List[str] = []
add_header("variant_size.hpp", variant_size_source) add_header("variant_size.hpp", variant_size_source)
header_guard = "GODOT_CPP_VARIANT_SIZE_HPP" header_guard = "GODOT_CPP_VARIANT_SIZE_HPP"
@ -245,8 +246,8 @@ def generate_builtin_bindings(api, output_dir, build_config):
source_filename = source_gen_folder / (camel_to_snake(builtin_api["name"]) + ".cpp") source_filename = source_gen_folder / (camel_to_snake(builtin_api["name"]) + ".cpp")
# Check used classes for header include # Check used classes for header include
used_classes = set() used_classes: Set[str] = set()
fully_used_classes = set() fully_used_classes: Set[str] = set()
class_name = builtin_api["name"] class_name = builtin_api["name"]
@ -292,21 +293,25 @@ def generate_builtin_bindings(api, output_dir, build_config):
if type_name in used_classes: if type_name in used_classes:
used_classes.remove(type_name) used_classes.remove(type_name)
used_classes = list(used_classes) used_classes_list: List[str] = list(used_classes)
used_classes.sort() used_classes_list.sort()
fully_used_classes = list(fully_used_classes) fully_used_classes_list: List[str] = list(fully_used_classes)
fully_used_classes.sort() fully_used_classes_list.sort()
with header_filename.open("w+", encoding="utf-8") as header_file: with header_filename.open("w+", encoding="utf-8") as header_file:
header_file.write(generate_builtin_class_header(builtin_api, size, used_classes, fully_used_classes)) header_file.write(
generate_builtin_class_header(builtin_api, size, used_classes_list, fully_used_classes_list)
)
with source_filename.open("w+", encoding="utf-8") as source_file: with source_filename.open("w+", encoding="utf-8") as source_file:
source_file.write(generate_builtin_class_source(builtin_api, size, used_classes, fully_used_classes)) source_file.write(
generate_builtin_class_source(builtin_api, size, used_classes_list, fully_used_classes_list)
)
# Create a header with all builtin types for convenience. # Create a header with all builtin types for convenience.
builtin_header_filename = include_gen_folder / "builtin_types.hpp" builtin_header_filename = include_gen_folder / "builtin_types.hpp"
with builtin_header_filename.open("w+", encoding="utf-8") as builtin_header_file: with builtin_header_filename.open("w+", encoding="utf-8") as builtin_header_file:
builtin_header = [] builtin_header: List[str] = []
add_header("builtin_types.hpp", builtin_header) add_header("builtin_types.hpp", builtin_header)
builtin_header.append("#ifndef GODOT_CPP_BUILTIN_TYPES_HPP") builtin_header.append("#ifndef GODOT_CPP_BUILTIN_TYPES_HPP")
@ -326,7 +331,7 @@ def generate_builtin_bindings(api, output_dir, build_config):
# Create a header with bindings for builtin types. # Create a header with bindings for builtin types.
builtin_binds_filename = include_gen_folder / "builtin_binds.hpp" builtin_binds_filename = include_gen_folder / "builtin_binds.hpp"
with builtin_binds_filename.open("w+", encoding="utf-8") as builtin_binds_file: with builtin_binds_filename.open("w+", encoding="utf-8") as builtin_binds_file:
builtin_binds = [] builtin_binds: List[str] = []
add_header("builtin_binds.hpp", builtin_binds) add_header("builtin_binds.hpp", builtin_binds)
builtin_binds.append("#ifndef GODOT_CPP_BUILTIN_BINDS_HPP") builtin_binds.append("#ifndef GODOT_CPP_BUILTIN_BINDS_HPP")
@ -353,8 +358,8 @@ def generate_builtin_bindings(api, output_dir, build_config):
) )
def generate_builtin_class_vararg_method_implements_header(builtin_classes): def generate_builtin_class_vararg_method_implements_header(builtin_classes: List[Dict[str, Any]]) -> str:
result = [] result: List[str] = []
add_header("builtin_vararg_methods.hpp", result) add_header("builtin_vararg_methods.hpp", result)
@ -381,8 +386,10 @@ def generate_builtin_class_vararg_method_implements_header(builtin_classes):
return "\n".join(result) return "\n".join(result)
def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_classes): def generate_builtin_class_header(
result = [] builtin_api: Dict[str, Any], size: int, used_classes: List[str], fully_used_classes: List[str]
) -> str:
result: List[str] = []
class_name = builtin_api["name"] class_name = builtin_api["name"]
snake_class_name = camel_to_snake(class_name).upper() snake_class_name = camel_to_snake(class_name).upper()
@ -588,17 +595,17 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
# Special cases. # Special cases.
if class_name == "String": if class_name == "String":
result.append("\tstatic String utf8(const char *from, int len = -1);") result.append("\tstatic String utf8(const char *from, int64_t len = -1);")
result.append("\tvoid parse_utf8(const char *from, int len = -1);") result.append("\tvoid parse_utf8(const char *from, int64_t len = -1);")
result.append("\tstatic String utf16(const char16_t *from, int len = -1);") result.append("\tstatic String utf16(const char16_t *from, int64_t len = -1);")
result.append("\tvoid parse_utf16(const char16_t *from, int len = -1);") result.append("\tvoid parse_utf16(const char16_t *from, int64_t len = -1);")
result.append("\tCharString utf8() const;") result.append("\tCharString utf8() const;")
result.append("\tCharString ascii() const;") result.append("\tCharString ascii() const;")
result.append("\tChar16String utf16() const;") result.append("\tChar16String utf16() const;")
result.append("\tChar32String utf32() const;") result.append("\tChar32String utf32() const;")
result.append("\tCharWideString wide_string() const;") result.append("\tCharWideString wide_string() const;")
result.append("\tstatic String num_real(double p_num, bool p_trailing = true);") result.append("\tstatic String num_real(double p_num, bool p_trailing = true);")
result.append("\tError resize(int p_size);") result.append("\tError resize(int64_t p_size);")
if "members" in builtin_api: if "members" in builtin_api:
for member in builtin_api["members"]: for member in builtin_api["members"]:
@ -651,8 +658,8 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("\tString &operator+=(const wchar_t *p_str);") result.append("\tString &operator+=(const wchar_t *p_str);")
result.append("\tString &operator+=(const char32_t *p_str);") result.append("\tString &operator+=(const char32_t *p_str);")
result.append("\tconst char32_t &operator[](int p_index) const;") result.append("\tconst char32_t &operator[](int64_t p_index) const;")
result.append("\tchar32_t &operator[](int p_index);") result.append("\tchar32_t &operator[](int64_t p_index);")
result.append("\tconst char32_t *ptr() const;") result.append("\tconst char32_t *ptr() const;")
result.append("\tchar32_t *ptrw();") result.append("\tchar32_t *ptrw();")
@ -670,8 +677,8 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
return_type = "int32_t" return_type = "int32_t"
elif class_name == "PackedFloat32Array": elif class_name == "PackedFloat32Array":
return_type = "float" return_type = "float"
result.append(f"\tconst {return_type} &operator[](int p_index) const;") result.append(f"\tconst {return_type} &operator[](int64_t p_index) const;")
result.append(f"\t{return_type} &operator[](int p_index);") result.append(f"\t{return_type} &operator[](int64_t p_index);")
result.append(f"\tconst {return_type} *ptr() const;") result.append(f"\tconst {return_type} *ptr() const;")
result.append(f"\t{return_type} *ptrw();") result.append(f"\t{return_type} *ptrw();")
iterators = """ iterators = """
@ -742,8 +749,8 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append(iterators.replace("$TYPE", return_type)) result.append(iterators.replace("$TYPE", return_type))
if class_name == "Array": if class_name == "Array":
result.append("\tconst Variant &operator[](int p_index) const;") result.append("\tconst Variant &operator[](int64_t p_index) const;")
result.append("\tVariant &operator[](int p_index);") result.append("\tVariant &operator[](int64_t p_index);")
result.append("\tvoid set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script);") result.append("\tvoid set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script);")
result.append("\tvoid _ref(const Array &p_from) const;") result.append("\tvoid _ref(const Array &p_from) const;")
@ -782,8 +789,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
return "\n".join(result) return "\n".join(result)
def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_classes): def generate_builtin_class_source(
result = [] builtin_api: Dict[str, Any], size: int, used_classes: List[str], fully_used_classes: List[str]
) -> str:
result: List[str] = []
class_name = builtin_api["name"] class_name = builtin_api["name"]
snake_class_name = camel_to_snake(class_name) snake_class_name = camel_to_snake(class_name)
@ -922,7 +931,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
(encode, arg_name) = get_encoded_arg( (encode, arg_name) = get_encoded_arg(
argument["name"], argument["name"],
argument["type"], argument["type"],
argument["meta"] if "meta" in argument else None, argument["meta"] if "meta" in argument else "",
) )
result += encode result += encode
arguments.append(arg_name) arguments.append(arg_name)
@ -981,7 +990,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
(encode, arg_name) = get_encoded_arg( (encode, arg_name) = get_encoded_arg(
argument["name"], argument["name"],
argument["type"], argument["type"],
argument["meta"] if "meta" in argument else None, argument["meta"] if "meta" in argument else "",
) )
result += encode result += encode
arguments.append(arg_name) arguments.append(arg_name)
@ -1003,7 +1012,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
if f'set_{member["name"]}' not in method_list: if f'set_{member["name"]}' not in method_list:
result.append(f'void {class_name}::set_{member["name"]}({type_for_parameter(member["type"])}value) {{') result.append(f'void {class_name}::set_{member["name"]}({type_for_parameter(member["type"])}value) {{')
(encode, arg_name) = get_encoded_arg("value", member["type"], None) (encode, arg_name) = get_encoded_arg("value", member["type"])
result += encode result += encode
result.append( result.append(
f'\t_method_bindings.member_{member["name"]}_setter((GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr){arg_name});' f'\t_method_bindings.member_{member["name"]}_setter((GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr){arg_name});'
@ -1019,7 +1028,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
result.append( result.append(
f'{correct_type(operator["return_type"])} {class_name}::operator{operator["name"]}({type_for_parameter(operator["right_type"])}other) const {{' f'{correct_type(operator["return_type"])} {class_name}::operator{operator["name"]}({type_for_parameter(operator["right_type"])}other) const {{'
) )
(encode, arg_name) = get_encoded_arg("other", operator["right_type"], None) (encode, arg_name) = get_encoded_arg("other", operator["right_type"])
result += encode result += encode
result.append( result.append(
f'\treturn internal::_call_builtin_operator_ptr<{get_gdextension_type(correct_type(operator["return_type"]))}>(_method_bindings.operator_{get_operator_id_name(operator["name"])}_{operator["right_type"]}, (GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr){arg_name});' f'\treturn internal::_call_builtin_operator_ptr<{get_gdextension_type(correct_type(operator["return_type"]))}>(_method_bindings.operator_{get_operator_id_name(operator["name"])}_{operator["right_type"]}, (GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr){arg_name});'
@ -1043,7 +1052,6 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
(encode, arg_name) = get_encoded_arg( (encode, arg_name) = get_encoded_arg(
"other", "other",
class_name, class_name,
None,
) )
result += encode result += encode
result.append( result.append(
@ -1070,7 +1078,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
return "\n".join(result) return "\n".join(result)
def generate_engine_classes_bindings(api, output_dir, use_template_get_node): def generate_engine_classes_bindings(api: Dict[str, Any], output_dir: Path, use_template_get_node: bool) -> None:
global engine_classes global engine_classes
global singletons global singletons
global native_structures global native_structures
@ -1103,8 +1111,8 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
for class_api in api["classes"]: for class_api in api["classes"]:
# Check used classes for header include. # Check used classes for header include.
used_classes = set() used_classes: Set[str] = set()
fully_used_classes = set() fully_used_classes: Set[str] = set()
class_name = class_api["name"] class_name = class_api["name"]
@ -1201,19 +1209,23 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
if type_name in used_classes: if type_name in used_classes:
used_classes.remove(type_name) used_classes.remove(type_name)
used_classes = list(used_classes) used_classes_list: List[str] = list(used_classes)
used_classes.sort() used_classes_list.sort()
fully_used_classes = list(fully_used_classes) fully_used_classes_list: List[str] = list(fully_used_classes)
fully_used_classes.sort() fully_used_classes_list.sort()
with header_filename.open("w+", encoding="utf-8") as header_file: with header_filename.open("w+", encoding="utf-8") as header_file:
header_file.write( header_file.write(
generate_engine_class_header(class_api, used_classes, fully_used_classes, use_template_get_node) generate_engine_class_header(
class_api, used_classes_list, fully_used_classes_list, use_template_get_node
)
) )
with source_filename.open("w+", encoding="utf-8") as source_file: with source_filename.open("w+", encoding="utf-8") as source_file:
source_file.write( source_file.write(
generate_engine_class_source(class_api, used_classes, fully_used_classes, use_template_get_node) generate_engine_class_source(
class_api, used_classes_list, fully_used_classes_list, use_template_get_node
)
) )
for native_struct in api["native_structures"]: for native_struct in api["native_structures"]:
@ -1224,27 +1236,27 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
header_filename = include_gen_folder / (snake_struct_name + ".hpp") header_filename = include_gen_folder / (snake_struct_name + ".hpp")
result = [] result: List[str] = []
add_header(f"{snake_struct_name}.hpp", result) add_header(f"{snake_struct_name}.hpp", result)
header_guard = f"GODOT_CPP_{snake_struct_name.upper()}_HPP" header_guard = f"GODOT_CPP_{snake_struct_name.upper()}_HPP"
result.append(f"#ifndef {header_guard}") result.append(f"#ifndef {header_guard}")
result.append(f"#define {header_guard}") result.append(f"#define {header_guard}")
used_classes = [] used_classes_list = []
expanded_format = native_struct["format"].replace("(", " ").replace(")", ";").replace(",", ";") expanded_format = native_struct["format"].replace("(", " ").replace(")", ";").replace(",", ";")
for field in expanded_format.split(";"): for field in expanded_format.split(";"):
field_type = field.strip().split(" ")[0].split("::")[0] field_type = field.strip().split(" ")[0].split("::")[0]
if field_type != "" and not is_included_type(field_type) and not is_pod_type(field_type): if field_type != "" and not is_included_type(field_type) and not is_pod_type(field_type):
if not field_type in used_classes: if not field_type in used_classes_list:
used_classes.append(field_type) used_classes_list.append(field_type)
result.append("") result.append("")
for included in used_classes: for included in used_classes_list:
result.append(f"#include <godot_cpp/{get_include_path(included)}>") result.append(f"#include <godot_cpp/{get_include_path(included)}>")
if len(used_classes) == 0: if len(used_classes_list) == 0:
result.append("#include <godot_cpp/core/method_ptrcall.hpp>") result.append("#include <godot_cpp/core/method_ptrcall.hpp>")
result.append("") result.append("")
@ -1268,9 +1280,11 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
header_file.write("\n".join(result)) header_file.write("\n".join(result))
def generate_engine_class_header(class_api, used_classes, fully_used_classes, use_template_get_node): def generate_engine_class_header(
class_api: Dict[str, Any], used_classes: List[str], fully_used_classes: List[str], use_template_get_node: bool
) -> str:
global singletons global singletons
result = [] result: List[str] = []
class_name = class_api["name"] class_name = class_api["name"]
snake_class_name = camel_to_snake(class_name).upper() snake_class_name = camel_to_snake(class_name).upper()
@ -1473,7 +1487,7 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
method_signature += f'{correct_type(method["return_type"])} ' method_signature += f'{correct_type(method["return_type"])} '
elif "return_value" in method: elif "return_value" in method:
method_signature += ( method_signature += (
correct_type(method["return_value"]["type"], method["return_value"].get("meta", None)) + " " correct_type(method["return_value"]["type"], method["return_value"].get("meta", "")) + " "
) )
else: else:
method_signature += "void " method_signature += "void "
@ -1509,9 +1523,11 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
return "\n".join(result) return "\n".join(result)
def generate_engine_class_source(class_api, used_classes, fully_used_classes, use_template_get_node): def generate_engine_class_source(
class_api: Dict[str, Any], used_classes: List[str], fully_used_classes: List[str], use_template_get_node: bool
) -> str:
global singletons global singletons
result = [] result: List[str] = []
class_name = class_api["name"] class_name = class_api["name"]
snake_class_name = camel_to_snake(class_name) snake_class_name = camel_to_snake(class_name)
@ -1586,7 +1602,7 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
if not vararg: if not vararg:
if has_return: if has_return:
return_type = method["return_value"]["type"] return_type = method["return_value"]["type"]
meta_type = method["return_value"]["meta"] if "meta" in method["return_value"] else None meta_type = method["return_value"]["meta"] if "meta" in method["return_value"] else ""
if is_enum(return_type): if is_enum(return_type):
if method["is_static"]: if method["is_static"]:
method_call += f"return ({get_gdextension_type(correct_type(return_type, meta_type))})internal::_call_native_mb_ret<int64_t>(_gde_method_bind, nullptr" method_call += f"return ({get_gdextension_type(correct_type(return_type, meta_type))})internal::_call_native_mb_ret<int64_t>(_gde_method_bind, nullptr"
@ -1625,7 +1641,7 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
(encode, arg_name) = get_encoded_arg( (encode, arg_name) = get_encoded_arg(
argument["name"], argument["name"],
argument["type"], argument["type"],
argument["meta"] if "meta" in argument else None, argument["meta"] if "meta" in argument else "",
) )
result += encode result += encode
arguments.append(arg_name) arguments.append(arg_name)
@ -1672,7 +1688,7 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
return "\n".join(result) return "\n".join(result)
def generate_global_constants(api, output_dir): def generate_global_constants(api: Dict[str, Any], output_dir: Path) -> None:
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "classes" include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "classes"
source_gen_folder = Path(output_dir) / "src" / "classes" source_gen_folder = Path(output_dir) / "src" / "classes"
@ -1681,7 +1697,7 @@ def generate_global_constants(api, output_dir):
# Generate header # Generate header
header = [] header: List[str] = []
add_header("global_constants.hpp", header) add_header("global_constants.hpp", header)
header_filename = include_gen_folder / "global_constants.hpp" header_filename = include_gen_folder / "global_constants.hpp"
@ -1696,7 +1712,7 @@ def generate_global_constants(api, output_dir):
header.append("") header.append("")
for constant in api["global_constants"]: for constant in api["global_constants"]:
header.append(f'\tconst int {escape_identifier(constant["name"])} = {constant["value"]};') header.append(f'\tconst int64_t {escape_identifier(constant["name"])} = {constant["value"]};')
header.append("") header.append("")
@ -1723,8 +1739,8 @@ def generate_global_constants(api, output_dir):
header_file.write("\n".join(header)) header_file.write("\n".join(header))
def generate_version_header(api, output_dir): def generate_version_header(api: Dict[str, Any], output_dir: Path) -> None:
header = [] header: List[str] = []
header_filename = "version.hpp" header_filename = "version.hpp"
add_header(header_filename, header) add_header(header_filename, header)
@ -1752,7 +1768,7 @@ def generate_version_header(api, output_dir):
header_file.write("\n".join(header)) header_file.write("\n".join(header))
def generate_global_constant_binds(api, output_dir): def generate_global_constant_binds(api: Dict[str, Any], output_dir: Path) -> None:
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "classes" include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "classes"
source_gen_folder = Path(output_dir) / "src" / "classes" source_gen_folder = Path(output_dir) / "src" / "classes"
@ -1761,7 +1777,7 @@ def generate_global_constant_binds(api, output_dir):
# Generate header # Generate header
header = [] header: List[str] = []
add_header("global_constants_binds.hpp", header) add_header("global_constants_binds.hpp", header)
header_filename = include_gen_folder / "global_constants_binds.hpp" header_filename = include_gen_folder / "global_constants_binds.hpp"
@ -1793,7 +1809,7 @@ def generate_global_constant_binds(api, output_dir):
header_file.write("\n".join(header)) header_file.write("\n".join(header))
def generate_utility_functions(api, output_dir): def generate_utility_functions(api: Dict[str, Any], output_dir: Path) -> None:
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "variant" include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "variant"
source_gen_folder = Path(output_dir) / "src" / "variant" source_gen_folder = Path(output_dir) / "src" / "variant"
@ -1802,7 +1818,7 @@ def generate_utility_functions(api, output_dir):
# Generate header. # Generate header.
header = [] header: List[str] = []
add_header("utility_functions.hpp", header) add_header("utility_functions.hpp", header)
header_filename = include_gen_folder / "utility_functions.hpp" header_filename = include_gen_folder / "utility_functions.hpp"
@ -1843,7 +1859,7 @@ def generate_utility_functions(api, output_dir):
# Generate source. # Generate source.
source = [] source: List[str] = []
add_header("utility_functions.cpp", source) add_header("utility_functions.cpp", source)
source_filename = source_gen_folder / "utility_functions.cpp" source_filename = source_gen_folder / "utility_functions.cpp"
@ -1892,7 +1908,7 @@ def generate_utility_functions(api, output_dir):
(encode, arg_name) = get_encoded_arg( (encode, arg_name) = get_encoded_arg(
argument["name"], argument["name"],
argument["type"], argument["type"],
argument["meta"] if "meta" in argument else None, argument["meta"] if "meta" in argument else "",
) )
source += encode source += encode
arguments.append(arg_name) arguments.append(arg_name)
@ -1922,17 +1938,19 @@ def generate_utility_functions(api, output_dir):
# Helper functions. # Helper functions.
def camel_to_snake(name): def camel_to_snake(name: str) -> str:
name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name) name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
name = re.sub("([a-z0-9])([A-Z])", r"\1_\2", name) name = re.sub("([a-z0-9])([A-Z])", r"\1_\2", name)
return name.replace("2_D", "2D").replace("3_D", "3D").lower() return name.replace("2_D", "2D").replace("3_D", "3D").lower()
def make_function_parameters(parameters, include_default=False, for_builtin=False, is_vararg=False): def make_function_parameters(
parameters: List[Dict[str, Any]], include_default: bool = False, for_builtin: bool = False, is_vararg: bool = False
) -> str:
signature = [] signature = []
for index, par in enumerate(parameters): for index, par in enumerate(parameters):
parameter = type_for_parameter(par["type"], par["meta"] if "meta" in par else None) parameter = type_for_parameter(par["type"], par["meta"] if "meta" in par else "")
parameter_name = escape_identifier(par["name"]) parameter_name = escape_identifier(par["name"])
if len(parameter_name) == 0: if len(parameter_name) == 0:
parameter_name = "arg_" + str(index + 1) parameter_name = "arg_" + str(index + 1)
@ -1954,7 +1972,7 @@ def make_function_parameters(parameters, include_default=False, for_builtin=Fals
return ", ".join(signature) return ", ".join(signature)
def type_for_parameter(type_name, meta=None): def type_for_parameter(type_name: str, meta: str = "") -> str:
if type_name == "void": if type_name == "void":
return "Variant " return "Variant "
elif is_pod_type(type_name) and type_name != "Nil" or is_enum(type_name): elif is_pod_type(type_name) and type_name != "Nil" or is_enum(type_name):
@ -1965,7 +1983,7 @@ def type_for_parameter(type_name, meta=None):
return f"{correct_type(type_name)}" return f"{correct_type(type_name)}"
def get_include_path(type_name): def get_include_path(type_name: str) -> str:
base_dir = "" base_dir = ""
if type_name == "Object": if type_name == "Object":
base_dir = "core" base_dir = "core"
@ -1977,8 +1995,8 @@ def get_include_path(type_name):
return f"{base_dir}/{camel_to_snake(type_name)}.hpp" return f"{base_dir}/{camel_to_snake(type_name)}.hpp"
def get_encoded_arg(arg_name, type_name, type_meta): def get_encoded_arg(arg_name: str, type_name: str, type_meta: str = "") -> Tuple[List[str], str]:
result = [] result: List[str] = []
name = escape_identifier(arg_name) name = escape_identifier(arg_name)
arg_type = correct_type(type_name) arg_type = correct_type(type_name)
@ -1997,8 +2015,13 @@ def get_encoded_arg(arg_name, type_name, type_meta):
def make_signature( def make_signature(
class_name, function_data, for_header=False, use_template_get_node=True, for_builtin=False, static=False class_name: str,
): function_data: Dict[str, Any],
for_header: bool = False,
use_template_get_node: bool = True,
for_builtin: bool = False,
static: bool = False,
) -> str:
function_signature = "" function_signature = ""
is_vararg = "is_vararg" in function_data and function_data["is_vararg"] is_vararg = "is_vararg" in function_data and function_data["is_vararg"]
@ -2014,12 +2037,12 @@ def make_signature(
function_signature += "static " function_signature += "static "
return_type = "void" return_type = "void"
return_meta = None return_meta = ""
if "return_type" in function_data: if "return_type" in function_data:
return_type = correct_type(function_data["return_type"]) return_type = correct_type(function_data["return_type"])
elif "return_value" in function_data: elif "return_value" in function_data:
return_type = function_data["return_value"]["type"] return_type = function_data["return_value"]["type"]
return_meta = function_data["return_value"]["meta"] if "meta" in function_data["return_value"] else None return_meta = function_data["return_value"]["meta"] if "meta" in function_data["return_value"] else ""
function_signature += correct_type( function_signature += correct_type(
return_type, return_type,
@ -2059,14 +2082,14 @@ def make_signature(
def make_varargs_template( def make_varargs_template(
function_data, function_data: Dict[str, Any],
static=False, static: bool = False,
class_befor_signature="", class_before_signature: str = "",
with_public_declare=True, with_public_declare: bool = True,
with_indent=True, with_indent: bool = True,
for_builtin_classes=False, for_builtin_classes: bool = False,
): ) -> List[str]:
result = [] result: List[str] = []
function_signature = "" function_signature = ""
@ -2079,12 +2102,12 @@ def make_varargs_template(
function_signature += "static " function_signature += "static "
return_type = "void" return_type = "void"
return_meta = None return_meta = ""
if "return_type" in function_data: if "return_type" in function_data:
return_type = correct_type(function_data["return_type"]) return_type = correct_type(function_data["return_type"])
elif "return_value" in function_data: elif "return_value" in function_data:
return_type = function_data["return_value"]["type"] return_type = function_data["return_value"]["type"]
return_meta = function_data["return_value"]["meta"] if "meta" in function_data["return_value"] else None return_meta = function_data["return_value"]["meta"] if "meta" in function_data["return_value"] else ""
function_signature += correct_type( function_signature += correct_type(
return_type, return_type,
@ -2094,8 +2117,8 @@ def make_varargs_template(
if not function_signature.endswith("*"): if not function_signature.endswith("*"):
function_signature += " " function_signature += " "
if len(class_befor_signature) > 0: if len(class_before_signature) > 0:
function_signature += class_befor_signature + "::" function_signature += class_before_signature + "::"
function_signature += f'{escape_identifier(function_data["name"])}' function_signature += f'{escape_identifier(function_data["name"])}'
method_arguments = [] method_arguments = []
@ -2169,7 +2192,7 @@ def make_varargs_template(
# Engine idiosyncrasies. # Engine idiosyncrasies.
def is_pod_type(type_name): def is_pod_type(type_name: str) -> bool:
""" """
Those are types for which no class should be generated. Those are types for which no class should be generated.
""" """
@ -2192,12 +2215,12 @@ def is_pod_type(type_name):
] ]
def is_included_type(type_name): def is_included_type(type_name: str) -> bool:
# Types which we already have implemented. # Types which we already have implemented.
return is_included_struct_type(type_name) or type_name in ["ObjectID"] return is_included_struct_type(type_name) or type_name in ["ObjectID"]
def is_included_struct_type(type_name): def is_included_struct_type(type_name: str) -> bool:
# Struct types which we already have implemented. # Struct types which we already have implemented.
return type_name in [ return type_name in [
"AABB", "AABB",
@ -2219,7 +2242,7 @@ def is_included_struct_type(type_name):
] ]
def is_packed_array(type_name): def is_packed_array(type_name: str) -> bool:
""" """
Those are types for which we add our extra packed array functions. Those are types for which we add our extra packed array functions.
""" """
@ -2236,7 +2259,7 @@ def is_packed_array(type_name):
] ]
def needs_copy_instead_of_move(type_name): def needs_copy_instead_of_move(type_name: str) -> bool:
""" """
Those are types which need initialised data or we'll get warning spam so need a copy instead of move. Those are types which need initialised data or we'll get warning spam so need a copy instead of move.
""" """
@ -2245,15 +2268,15 @@ def needs_copy_instead_of_move(type_name):
] ]
def is_enum(type_name): def is_enum(type_name: str) -> bool:
return type_name.startswith("enum::") or type_name.startswith("bitfield::") return type_name.startswith("enum::") or type_name.startswith("bitfield::")
def is_bitfield(type_name): def is_bitfield(type_name: str) -> bool:
return type_name.startswith("bitfield::") return type_name.startswith("bitfield::")
def get_enum_class(enum_name: str): def get_enum_class(enum_name: str) -> str:
if "." in enum_name: if "." in enum_name:
if is_bitfield(enum_name): if is_bitfield(enum_name):
return enum_name.replace("bitfield::", "").split(".")[0] return enum_name.replace("bitfield::", "").split(".")[0]
@ -2263,21 +2286,21 @@ def get_enum_class(enum_name: str):
return "GlobalConstants" return "GlobalConstants"
def get_enum_fullname(enum_name: str): def get_enum_fullname(enum_name: str) -> str:
if is_bitfield(enum_name): if is_bitfield(enum_name):
return enum_name.replace("bitfield::", "BitField<") + ">" return enum_name.replace("bitfield::", "BitField<") + ">"
else: else:
return enum_name.replace("enum::", "") return enum_name.replace("enum::", "")
def get_enum_name(enum_name: str): def get_enum_name(enum_name: str) -> str:
if is_bitfield(enum_name): if is_bitfield(enum_name):
return enum_name.replace("bitfield::", "").split(".")[-1] return enum_name.replace("bitfield::", "").split(".")[-1]
else: else:
return enum_name.replace("enum::", "").split(".")[-1] return enum_name.replace("enum::", "").split(".")[-1]
def is_variant(type_name): def is_variant(type_name: str) -> bool:
return ( return (
type_name == "Variant" type_name == "Variant"
or type_name in builtin_classes or type_name in builtin_classes
@ -2286,22 +2309,22 @@ def is_variant(type_name):
) )
def is_engine_class(type_name): def is_engine_class(type_name: str) -> bool:
global engine_classes global engine_classes
return type_name == "Object" or type_name in engine_classes return type_name == "Object" or type_name in engine_classes
def is_struct_type(type_name): def is_struct_type(type_name: str) -> bool:
# This is used to determine which keyword to use for forward declarations. # This is used to determine which keyword to use for forward declarations.
global native_structures global native_structures
return is_included_struct_type(type_name) or type_name in native_structures return is_included_struct_type(type_name) or type_name in native_structures
def is_refcounted(type_name): def is_refcounted(type_name: str) -> bool:
return type_name in engine_classes and engine_classes[type_name] return type_name in engine_classes and engine_classes[type_name]
def is_included(type_name, current_type): def is_included(type_name: str, current_type: str) -> bool:
""" """
Check if a builtin type should be included. Check if a builtin type should be included.
This removes Variant and POD types from inclusion, and the current type. This removes Variant and POD types from inclusion, and the current type.
@ -2316,7 +2339,7 @@ def is_included(type_name, current_type):
return is_engine_class(to_include) or is_variant(to_include) return is_engine_class(to_include) or is_variant(to_include)
def correct_default_value(value, type_name): def correct_default_value(value: str, type_name: str) -> str:
value_map = { value_map = {
"null": "nullptr", "null": "nullptr",
'""': "String()", '""': "String()",
@ -2335,15 +2358,15 @@ def correct_default_value(value, type_name):
return value return value
def correct_typed_array(type_name): def correct_typed_array(type_name: str) -> str:
if type_name.startswith("typedarray::"): if type_name.startswith("typedarray::"):
return type_name.replace("typedarray::", "TypedArray<") + ">" return type_name.replace("typedarray::", "TypedArray<") + ">"
return type_name return type_name
def correct_type(type_name, meta=None): def correct_type(type_name: str, meta: str = "") -> str:
type_conversion = {"float": "double", "int": "int64_t", "Nil": "Variant"} type_conversion = {"float": "double", "int": "int64_t", "Nil": "Variant"}
if meta != None: if meta:
if "int" in meta: if "int" in meta:
return f"{meta}_t" return f"{meta}_t"
elif meta in type_conversion: elif meta in type_conversion:
@ -2374,7 +2397,7 @@ def correct_type(type_name, meta=None):
return type_name return type_name
def get_gdextension_type(type_name): def get_gdextension_type(type_name: str) -> str:
type_conversion_map = { type_conversion_map = {
"bool": "int8_t", "bool": "int8_t",
"uint8_t": "int64_t", "uint8_t": "int64_t",
@ -2395,7 +2418,7 @@ def get_gdextension_type(type_name):
return type_name return type_name
def escape_identifier(id): def escape_identifier(id: str) -> str:
cpp_keywords_map = { cpp_keywords_map = {
"class": "_class", "class": "_class",
"char": "_char", "char": "_char",
@ -2418,7 +2441,7 @@ def escape_identifier(id):
return id return id
def get_operator_id_name(op): def get_operator_id_name(op: str) -> str:
op_id_map = { op_id_map = {
"==": "equal", "==": "equal",
"!=": "not_equal", "!=": "not_equal",
@ -2450,7 +2473,7 @@ def get_operator_id_name(op):
return op_id_map[op] return op_id_map[op]
def get_default_value_for_type(type_name): def get_default_value_for_type(type_name: str) -> str:
if type_name == "int": if type_name == "int":
return "0" return "0"
if type_name == "float": if type_name == "float":
@ -2501,7 +2524,7 @@ header = """\
""" """
def add_header(filename, lines): def add_header(filename: str, lines: List[str]) -> None:
desired_length = len(header.split("\n")[0]) desired_length = len(header.split("\n")[0])
pad_spaces = desired_length - 6 - len(filename) pad_spaces = desired_length - 6 - len(filename)

View File

@ -52,6 +52,8 @@ class VMap;
template <class T> template <class T>
class CharStringT; class CharStringT;
SAFE_NUMERIC_TYPE_PUN_GUARANTEES(uint64_t)
// Silence a false positive warning (see GH-52119). // Silence a false positive warning (see GH-52119).
#if defined(__GNUC__) && !defined(__clang__) #if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
@ -69,52 +71,71 @@ class CowData {
template <class TS> template <class TS>
friend class CharStringT; friend class CharStringT;
public:
typedef int64_t Size;
typedef uint64_t USize;
static constexpr USize MAX_INT = INT64_MAX;
private: private:
// Function to find the next power of 2 to an integer.
static _FORCE_INLINE_ USize next_po2(USize x) {
if (x == 0) {
return 0;
}
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
if (sizeof(USize) == 8) {
x |= x >> 32;
}
return ++x;
}
static constexpr USize ALLOC_PAD = sizeof(USize) * 2; // For size and atomic refcount.
mutable T *_ptr = nullptr; mutable T *_ptr = nullptr;
// internal helpers // internal helpers
_FORCE_INLINE_ SafeNumeric<uint32_t> *_get_refcount() const { _FORCE_INLINE_ SafeNumeric<USize> *_get_refcount() const {
if (!_ptr) { if (!_ptr) {
return nullptr; return nullptr;
} }
return reinterpret_cast<SafeNumeric<uint32_t> *>(_ptr) - 2; return reinterpret_cast<SafeNumeric<USize> *>(_ptr) - 2;
} }
_FORCE_INLINE_ uint32_t *_get_size() const { _FORCE_INLINE_ USize *_get_size() const {
if (!_ptr) { if (!_ptr) {
return nullptr; return nullptr;
} }
return reinterpret_cast<uint32_t *>(_ptr) - 1; return reinterpret_cast<USize *>(_ptr) - 1;
} }
_FORCE_INLINE_ T *_get_data() const { _FORCE_INLINE_ USize _get_alloc_size(USize p_elements) const {
if (!_ptr) { return next_po2(p_elements * sizeof(T));
return nullptr;
}
return reinterpret_cast<T *>(_ptr);
} }
_FORCE_INLINE_ size_t _get_alloc_size(size_t p_elements) const { _FORCE_INLINE_ bool _get_alloc_size_checked(USize p_elements, USize *out) const {
return next_power_of_2(p_elements * sizeof(T));
}
_FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const {
if (unlikely(p_elements == 0)) { if (unlikely(p_elements == 0)) {
*out = 0; *out = 0;
return true; return true;
} }
#if defined(__GNUC__) #if defined(__GNUC__) && defined(IS_32_BIT)
size_t o; USize o;
size_t p; USize p;
if (__builtin_mul_overflow(p_elements, sizeof(T), &o)) { if (__builtin_mul_overflow(p_elements, sizeof(T), &o)) {
*out = 0; *out = 0;
return false; return false;
} }
*out = next_power_of_2(o); *out = next_po2(o);
if (__builtin_add_overflow(o, static_cast<size_t>(32), &p)) { if (__builtin_add_overflow(o, static_cast<USize>(32), &p)) {
return false; // No longer allocated here. return false; // No longer allocated here.
} }
#else #else
@ -128,22 +149,22 @@ private:
void _unref(void *p_data); void _unref(void *p_data);
void _ref(const CowData *p_from); void _ref(const CowData *p_from);
void _ref(const CowData &p_from); void _ref(const CowData &p_from);
uint32_t _copy_on_write(); USize _copy_on_write();
public: public:
void operator=(const CowData<T> &p_from) { _ref(p_from); } void operator=(const CowData<T> &p_from) { _ref(p_from); }
_FORCE_INLINE_ T *ptrw() { _FORCE_INLINE_ T *ptrw() {
_copy_on_write(); _copy_on_write();
return (T *)_get_data(); return _ptr;
} }
_FORCE_INLINE_ const T *ptr() const { _FORCE_INLINE_ const T *ptr() const {
return _get_data(); return _ptr;
} }
_FORCE_INLINE_ int size() const { _FORCE_INLINE_ Size size() const {
uint32_t *size = (uint32_t *)_get_size(); USize *size = (USize *)_get_size();
if (size) { if (size) {
return *size; return *size;
} else { } else {
@ -154,41 +175,42 @@ public:
_FORCE_INLINE_ void clear() { resize(0); } _FORCE_INLINE_ void clear() { resize(0); }
_FORCE_INLINE_ bool is_empty() const { return _ptr == nullptr; } _FORCE_INLINE_ bool is_empty() const { return _ptr == nullptr; }
_FORCE_INLINE_ void set(int p_index, const T &p_elem) { _FORCE_INLINE_ void set(Size p_index, const T &p_elem) {
ERR_FAIL_INDEX(p_index, size()); ERR_FAIL_INDEX(p_index, size());
_copy_on_write(); _copy_on_write();
_get_data()[p_index] = p_elem; _ptr[p_index] = p_elem;
} }
_FORCE_INLINE_ T &get_m(int p_index) { _FORCE_INLINE_ T &get_m(Size p_index) {
CRASH_BAD_INDEX(p_index, size()); CRASH_BAD_INDEX(p_index, size());
_copy_on_write(); _copy_on_write();
return _get_data()[p_index]; return _ptr[p_index];
} }
_FORCE_INLINE_ const T &get(int p_index) const { _FORCE_INLINE_ const T &get(Size p_index) const {
CRASH_BAD_INDEX(p_index, size()); CRASH_BAD_INDEX(p_index, size());
return _get_data()[p_index]; return _ptr[p_index];
} }
Error resize(int p_size); template <bool p_ensure_zero = false>
Error resize(Size p_size);
_FORCE_INLINE_ void remove_at(int p_index) { _FORCE_INLINE_ void remove_at(Size p_index) {
ERR_FAIL_INDEX(p_index, size()); ERR_FAIL_INDEX(p_index, size());
T *p = ptrw(); T *p = ptrw();
int len = size(); Size len = size();
for (int i = p_index; i < len - 1; i++) { for (Size i = p_index; i < len - 1; i++) {
p[i] = p[i + 1]; p[i] = p[i + 1];
} }
resize(len - 1); resize(len - 1);
} }
Error insert(int p_pos, const T &p_val) { Error insert(Size p_pos, const T &p_val) {
ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER); ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER);
resize(size() + 1); resize(size() + 1);
for (int i = (size() - 1); i > p_pos; i--) { for (Size i = (size() - 1); i > p_pos; i--) {
set(i, get(i - 1)); set(i, get(i - 1));
} }
set(p_pos, p_val); set(p_pos, p_val);
@ -196,11 +218,13 @@ public:
return OK; return OK;
} }
int find(const T &p_val, int p_from = 0) const; Size find(const T &p_val, Size p_from = 0) const;
Size rfind(const T &p_val, Size p_from = -1) const;
Size count(const T &p_val) const;
_FORCE_INLINE_ CowData() {} _FORCE_INLINE_ CowData() {}
_FORCE_INLINE_ ~CowData(); _FORCE_INLINE_ ~CowData();
_FORCE_INLINE_ CowData(CowData<T> &p_from) { _ref(p_from); } _FORCE_INLINE_ CowData(CowData<T> &p_from) { _ref(p_from); };
}; };
template <class T> template <class T>
@ -209,44 +233,45 @@ void CowData<T>::_unref(void *p_data) {
return; return;
} }
SafeNumeric<uint32_t> *refc = _get_refcount(); SafeNumeric<USize> *refc = _get_refcount();
if (refc->decrement() > 0) { if (refc->decrement() > 0) {
return; // still in use return; // still in use
} }
// clean up // clean up
if (!std::is_trivially_destructible<T>::value) { if (!std::is_trivially_destructible<T>::value) {
uint32_t *count = _get_size(); USize *count = _get_size();
T *data = (T *)(count + 1); T *data = (T *)(count + 1);
for (uint32_t i = 0; i < *count; ++i) { for (USize i = 0; i < *count; ++i) {
// call destructors // call destructors
data[i].~T(); data[i].~T();
} }
} }
// free mem // free mem
Memory::free_static((uint8_t *)p_data, true); Memory::free_static(((uint8_t *)p_data) - ALLOC_PAD, false);
} }
template <class T> template <class T>
uint32_t CowData<T>::_copy_on_write() { typename CowData<T>::USize CowData<T>::_copy_on_write() {
if (!_ptr) { if (!_ptr) {
return 0; return 0;
} }
SafeNumeric<uint32_t> *refc = _get_refcount(); SafeNumeric<USize> *refc = _get_refcount();
uint32_t rc = refc->get(); USize rc = refc->get();
if (unlikely(rc > 1)) { if (unlikely(rc > 1)) {
/* in use by more than me */ /* in use by more than me */
uint32_t current_size = *_get_size(); USize current_size = *_get_size();
uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size), true); USize *mem_new = (USize *)Memory::alloc_static(_get_alloc_size(current_size) + ALLOC_PAD, false);
mem_new += 2;
new (mem_new - 2) SafeNumeric<uint32_t>(1); // refcount new (mem_new - 2) SafeNumeric<USize>(1); //refcount
*(mem_new - 1) = current_size; // size *(mem_new - 1) = current_size; //size
T *_data = (T *)(mem_new); T *_data = (T *)(mem_new);
@ -255,8 +280,8 @@ uint32_t CowData<T>::_copy_on_write() {
memcpy(mem_new, _ptr, current_size * sizeof(T)); memcpy(mem_new, _ptr, current_size * sizeof(T));
} else { } else {
for (uint32_t i = 0; i < current_size; i++) { for (USize i = 0; i < current_size; i++) {
memnew_placement(&_data[i], T(_get_data()[i])); memnew_placement(&_data[i], T(_ptr[i]));
} }
} }
@ -269,10 +294,11 @@ uint32_t CowData<T>::_copy_on_write() {
} }
template <class T> template <class T>
Error CowData<T>::resize(int p_size) { template <bool p_ensure_zero>
Error CowData<T>::resize(Size p_size) {
ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);
int current_size = size(); Size current_size = size();
if (p_size == current_size) { if (p_size == current_size) {
return OK; return OK;
@ -286,27 +312,29 @@ Error CowData<T>::resize(int p_size) {
} }
// possibly changing size, copy on write // possibly changing size, copy on write
uint32_t rc = _copy_on_write(); USize rc = _copy_on_write();
size_t current_alloc_size = _get_alloc_size(current_size); USize current_alloc_size = _get_alloc_size(current_size);
size_t alloc_size; USize alloc_size;
ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY); ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY);
if (p_size > current_size) { if (p_size > current_size) {
if (alloc_size != current_alloc_size) { if (alloc_size != current_alloc_size) {
if (current_size == 0) { if (current_size == 0) {
// alloc from scratch // alloc from scratch
uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size, true); USize *ptr = (USize *)Memory::alloc_static(alloc_size + ALLOC_PAD, false);
ptr += 2;
ERR_FAIL_NULL_V(ptr, ERR_OUT_OF_MEMORY); ERR_FAIL_NULL_V(ptr, ERR_OUT_OF_MEMORY);
*(ptr - 1) = 0; // size, currently none *(ptr - 1) = 0; //size, currently none
new (ptr - 2) SafeNumeric<uint32_t>(1); // refcount new (ptr - 2) SafeNumeric<USize>(1); //refcount
_ptr = (T *)ptr; _ptr = (T *)ptr;
} else { } else {
uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true); USize *_ptrnew = (USize *)Memory::realloc_static(((uint8_t *)_ptr) - ALLOC_PAD, alloc_size + ALLOC_PAD, false);
ERR_FAIL_NULL_V(_ptrnew, ERR_OUT_OF_MEMORY); ERR_FAIL_NULL_V(_ptrnew, ERR_OUT_OF_MEMORY);
new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); // refcount _ptrnew += 2;
new (_ptrnew - 2) SafeNumeric<USize>(rc); //refcount
_ptr = (T *)(_ptrnew); _ptr = (T *)(_ptrnew);
} }
@ -315,11 +343,11 @@ Error CowData<T>::resize(int p_size) {
// construct the newly created elements // construct the newly created elements
if (!std::is_trivially_constructible<T>::value) { if (!std::is_trivially_constructible<T>::value) {
T *elems = _get_data(); for (Size i = *_get_size(); i < p_size; i++) {
memnew_placement(&_ptr[i], T);
for (int i = *_get_size(); i < p_size; i++) {
memnew_placement(&elems[i], T);
} }
} else if (p_ensure_zero) {
memset((void *)(_ptr + current_size), 0, (p_size - current_size) * sizeof(T));
} }
*_get_size() = p_size; *_get_size() = p_size;
@ -327,16 +355,17 @@ Error CowData<T>::resize(int p_size) {
} else if (p_size < current_size) { } else if (p_size < current_size) {
if (!std::is_trivially_destructible<T>::value) { if (!std::is_trivially_destructible<T>::value) {
// deinitialize no longer needed elements // deinitialize no longer needed elements
for (uint32_t i = p_size; i < *_get_size(); i++) { for (USize i = p_size; i < *_get_size(); i++) {
T *t = &_get_data()[i]; T *t = &_ptr[i];
t->~T(); t->~T();
} }
} }
if (alloc_size != current_alloc_size) { if (alloc_size != current_alloc_size) {
uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true); USize *_ptrnew = (USize *)Memory::realloc_static(((uint8_t *)_ptr) - ALLOC_PAD, alloc_size + ALLOC_PAD, false);
ERR_FAIL_NULL_V(_ptrnew, ERR_OUT_OF_MEMORY); ERR_FAIL_NULL_V(_ptrnew, ERR_OUT_OF_MEMORY);
new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); // refcount _ptrnew += 2;
new (_ptrnew - 2) SafeNumeric<USize>(rc); //refcount
_ptr = (T *)(_ptrnew); _ptr = (T *)(_ptrnew);
} }
@ -348,14 +377,14 @@ Error CowData<T>::resize(int p_size) {
} }
template <class T> template <class T>
int CowData<T>::find(const T &p_val, int p_from) const { typename CowData<T>::Size CowData<T>::find(const T &p_val, Size p_from) const {
int ret = -1; Size ret = -1;
if (p_from < 0 || size() == 0) { if (p_from < 0 || size() == 0) {
return ret; return ret;
} }
for (int i = p_from; i < size(); i++) { for (Size i = p_from; i < size(); i++) {
if (get(i) == p_val) { if (get(i) == p_val) {
ret = i; ret = i;
break; break;
@ -365,6 +394,36 @@ int CowData<T>::find(const T &p_val, int p_from) const {
return ret; return ret;
} }
template <class T>
typename CowData<T>::Size CowData<T>::rfind(const T &p_val, Size p_from) const {
const Size s = size();
if (p_from < 0) {
p_from = s + p_from;
}
if (p_from < 0 || p_from >= s) {
p_from = s - 1;
}
for (Size i = p_from; i >= 0; i--) {
if (get(i) == p_val) {
return i;
}
}
return -1;
}
template <class T>
typename CowData<T>::Size CowData<T>::count(const T &p_val) const {
Size amount = 0;
for (Size i = 0; i < size(); i++) {
if (get(i) == p_val) {
amount++;
}
}
return amount;
}
template <class T> template <class T>
void CowData<T>::_ref(const CowData *p_from) { void CowData<T>::_ref(const CowData *p_from) {
_ref(*p_from); _ref(*p_from);

View File

@ -48,6 +48,15 @@ namespace godot {
// value and, as an important benefit, you can be sure the value is properly synchronized // value and, as an important benefit, you can be sure the value is properly synchronized
// even with threads that are already running. // even with threads that are already running.
// These are used in very specific areas of the engine where it's critical that these guarantees are held
#define SAFE_NUMERIC_TYPE_PUN_GUARANTEES(m_type) \
static_assert(sizeof(SafeNumeric<m_type>) == sizeof(m_type)); \
static_assert(alignof(SafeNumeric<m_type>) == alignof(m_type)); \
static_assert(std::is_trivially_destructible<std::atomic<m_type>>::value);
#define SAFE_FLAG_TYPE_PUN_GUARANTEES \
static_assert(sizeof(SafeFlag) == sizeof(bool)); \
static_assert(alignof(SafeFlag) == alignof(bool));
template <class T> template <class T>
class SafeNumeric { class SafeNumeric {
std::atomic<T> value; std::atomic<T> value;

View File

@ -50,7 +50,7 @@ namespace godot {
template <class T> template <class T>
class VectorWriteProxy { class VectorWriteProxy {
public: public:
_FORCE_INLINE_ T &operator[](int p_index) { _FORCE_INLINE_ T &operator[](typename CowData<T>::Size p_index) {
CRASH_BAD_INDEX(p_index, ((Vector<T> *)(this))->_cowdata.size()); CRASH_BAD_INDEX(p_index, ((Vector<T> *)(this))->_cowdata.size());
return ((Vector<T> *)(this))->_cowdata.ptrw()[p_index]; return ((Vector<T> *)(this))->_cowdata.ptrw()[p_index];
@ -63,22 +63,26 @@ class Vector {
public: public:
VectorWriteProxy<T> write; VectorWriteProxy<T> write;
typedef typename CowData<T>::Size Size;
private: private:
CowData<T> _cowdata; CowData<T> _cowdata;
public: public:
bool push_back(T p_elem); bool push_back(T p_elem);
_FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } // alias _FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } //alias
void fill(T p_elem); void fill(T p_elem);
void remove_at(int p_index) { _cowdata.remove_at(p_index); } void remove_at(Size p_index) { _cowdata.remove_at(p_index); }
void erase(const T &p_val) { _FORCE_INLINE_ bool erase(const T &p_val) {
int idx = find(p_val); Size idx = find(p_val);
if (idx >= 0) { if (idx >= 0) {
remove_at(idx); remove_at(idx);
return true;
} }
return false;
} }
void reverse(); void reverse();
_FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); } _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
@ -86,37 +90,45 @@ public:
_FORCE_INLINE_ void clear() { resize(0); } _FORCE_INLINE_ void clear() { resize(0); }
_FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); } _FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); }
_FORCE_INLINE_ T get(int p_index) { return _cowdata.get(p_index); } _FORCE_INLINE_ T get(Size p_index) { return _cowdata.get(p_index); }
_FORCE_INLINE_ const T &get(int p_index) const { return _cowdata.get(p_index); } _FORCE_INLINE_ const T &get(Size p_index) const { return _cowdata.get(p_index); }
_FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); } _FORCE_INLINE_ void set(Size p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
_FORCE_INLINE_ int size() const { return _cowdata.size(); } _FORCE_INLINE_ Size size() const { return _cowdata.size(); }
Error resize(int p_size) { return _cowdata.resize(p_size); } Error resize(Size p_size) { return _cowdata.resize(p_size); }
_FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata.get(p_index); } Error resize_zeroed(Size p_size) { return _cowdata.template resize<true>(p_size); }
Error insert(int p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); } _FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); }
int find(const T &p_val, int p_from = 0) const { return _cowdata.find(p_val, p_from); } Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); }
Size find(const T &p_val, Size p_from = 0) const { return _cowdata.find(p_val, p_from); }
Size rfind(const T &p_val, Size p_from = -1) const { return _cowdata.rfind(p_val, p_from); }
Size count(const T &p_val) const { return _cowdata.count(p_val); }
void append_array(Vector<T> p_other); void append_array(Vector<T> p_other);
_FORCE_INLINE_ bool has(const T &p_val) const { return find(p_val) != -1; } _FORCE_INLINE_ bool has(const T &p_val) const { return find(p_val) != -1; }
template <class C> void sort() {
void sort_custom() { sort_custom<_DefaultComparator<T>>();
int len = _cowdata.size(); }
template <class Comparator, bool Validate = SORT_ARRAY_VALIDATE_ENABLED, class... Args>
void sort_custom(Args &&...args) {
Size len = _cowdata.size();
if (len == 0) { if (len == 0) {
return; return;
} }
T *data = ptrw(); T *data = ptrw();
SortArray<T, C> sorter; SortArray<T, Comparator, Validate> sorter{ args... };
sorter.sort(data, len); sorter.sort(data, len);
} }
void sort() { Size bsearch(const T &p_value, bool p_before) {
sort_custom<_DefaultComparator<T>>(); return bsearch_custom<_DefaultComparator<T>>(p_value, p_before);
} }
int bsearch(const T &p_value, bool p_before) { template <class Comparator, class Value, class... Args>
SearchArray<T> search; Size bsearch_custom(const Value &p_value, bool p_before, Args &&...args) {
SearchArray<T, Comparator> search{ args... };
return search.bisect(ptrw(), size(), p_value, p_before); return search.bisect(ptrw(), size(), p_value, p_before);
} }
@ -125,7 +137,7 @@ public:
} }
void ordered_insert(const T &p_val) { void ordered_insert(const T &p_val) {
int i; Size i;
for (i = 0; i < _cowdata.size(); i++) { for (i = 0; i < _cowdata.size(); i++) {
if (p_val < operator[](i)) { if (p_val < operator[](i)) {
break; break;
@ -140,33 +152,36 @@ public:
Vector<uint8_t> to_byte_array() const { Vector<uint8_t> to_byte_array() const {
Vector<uint8_t> ret; Vector<uint8_t> ret;
if (is_empty()) {
return ret;
}
ret.resize(size() * sizeof(T)); ret.resize(size() * sizeof(T));
memcpy(ret.ptrw(), ptr(), sizeof(T) * size()); memcpy(ret.ptrw(), ptr(), sizeof(T) * size());
return ret; return ret;
} }
Vector<T> slice(int p_begin, int p_end = INT_MAX) const { Vector<T> slice(Size p_begin, Size p_end = CowData<T>::MAX_INT) const {
Vector<T> result; Vector<T> result;
const int s = size(); const Size s = size();
int begin = Math::clamp(p_begin, -s, s); Size begin = CLAMP(p_begin, -s, s);
if (begin < 0) { if (begin < 0) {
begin += s; begin += s;
} }
int end = Math::clamp(p_end, -s, s); Size end = CLAMP(p_end, -s, s);
if (end < 0) { if (end < 0) {
end += s; end += s;
} }
ERR_FAIL_COND_V(begin > end, result); ERR_FAIL_COND_V(begin > end, result);
int result_size = end - begin; Size result_size = end - begin;
result.resize(result_size); result.resize(result_size);
const T *const r = ptr(); const T *const r = ptr();
T *const w = result.ptrw(); T *const w = result.ptrw();
for (int i = 0; i < result_size; ++i) { for (Size i = 0; i < result_size; ++i) {
w[i] = r[begin + i]; w[i] = r[begin + i];
} }
@ -174,11 +189,11 @@ public:
} }
bool operator==(const Vector<T> &p_arr) const { bool operator==(const Vector<T> &p_arr) const {
int s = size(); Size s = size();
if (s != p_arr.size()) { if (s != p_arr.size()) {
return false; return false;
} }
for (int i = 0; i < s; i++) { for (Size i = 0; i < s; i++) {
if (operator[](i) != p_arr[i]) { if (operator[](i) != p_arr[i]) {
return false; return false;
} }
@ -187,11 +202,11 @@ public:
} }
bool operator!=(const Vector<T> &p_arr) const { bool operator!=(const Vector<T> &p_arr) const {
int s = size(); Size s = size();
if (s != p_arr.size()) { if (s != p_arr.size()) {
return true; return true;
} }
for (int i = 0; i < s; i++) { for (Size i = 0; i < s; i++) {
if (operator[](i) != p_arr[i]) { if (operator[](i) != p_arr[i]) {
return true; return true;
} }
@ -268,7 +283,7 @@ public:
Error err = _cowdata.resize(p_init.size()); Error err = _cowdata.resize(p_init.size());
ERR_FAIL_COND(err); ERR_FAIL_COND(err);
int i = 0; Size i = 0;
for (const T &element : p_init) { for (const T &element : p_init) {
_cowdata.set(i++, element); _cowdata.set(i++, element);
} }
@ -280,7 +295,7 @@ public:
template <class T> template <class T>
void Vector<T>::reverse() { void Vector<T>::reverse() {
for (int i = 0; i < size() / 2; i++) { for (Size i = 0; i < size() / 2; i++) {
T *p = ptrw(); T *p = ptrw();
SWAP(p[i], p[size() - i - 1]); SWAP(p[i], p[size() - i - 1]);
} }
@ -288,13 +303,13 @@ void Vector<T>::reverse() {
template <class T> template <class T>
void Vector<T>::append_array(Vector<T> p_other) { void Vector<T>::append_array(Vector<T> p_other) {
const int ds = p_other.size(); const Size ds = p_other.size();
if (ds == 0) { if (ds == 0) {
return; return;
} }
const int bs = size(); const Size bs = size();
resize(bs + ds); resize(bs + ds);
for (int i = 0; i < ds; ++i) { for (Size i = 0; i < ds; ++i) {
ptrw()[bs + i] = p_other[i]; ptrw()[bs + i] = p_other[i];
} }
} }
@ -311,7 +326,7 @@ bool Vector<T>::push_back(T p_elem) {
template <class T> template <class T>
void Vector<T>::fill(T p_elem) { void Vector<T>::fill(T p_elem) {
T *p = ptrw(); T *p = ptrw();
for (int i = 0; i < size(); i++) { for (Size i = 0; i < size(); i++) {
p[i] = p_elem; p[i] = p_elem;
} }
} }

View File

@ -46,11 +46,11 @@ class CharProxy {
template <class TS> template <class TS>
friend class CharStringT; friend class CharStringT;
const int _index; const int64_t _index;
CowData<T> &_cowdata; CowData<T> &_cowdata;
static inline const T _null = 0; static inline const T _null = 0;
_FORCE_INLINE_ CharProxy(const int &p_index, CowData<T> &p_cowdata) : _FORCE_INLINE_ CharProxy(const int64_t &p_index, CowData<T> &p_cowdata) :
_index(p_index), _index(p_index),
_cowdata(p_cowdata) {} _cowdata(p_cowdata) {}
@ -90,19 +90,19 @@ class CharStringT {
public: public:
_FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); } _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
_FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); } _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
_FORCE_INLINE_ int size() const { return _cowdata.size(); } _FORCE_INLINE_ int64_t size() const { return _cowdata.size(); }
Error resize(int p_size) { return _cowdata.resize(p_size); } Error resize(int64_t p_size) { return _cowdata.resize(p_size); }
_FORCE_INLINE_ T get(int p_index) const { return _cowdata.get(p_index); } _FORCE_INLINE_ T get(int64_t p_index) const { return _cowdata.get(p_index); }
_FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); } _FORCE_INLINE_ void set(int64_t p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
_FORCE_INLINE_ const T &operator[](int p_index) const { _FORCE_INLINE_ const T &operator[](int64_t p_index) const {
if (unlikely(p_index == _cowdata.size())) { if (unlikely(p_index == _cowdata.size())) {
return _null; return _null;
} }
return _cowdata.get(p_index); return _cowdata.get(p_index);
} }
_FORCE_INLINE_ CharProxy<T> operator[](int p_index) { return CharProxy<T>(p_index, _cowdata); } _FORCE_INLINE_ CharProxy<T> operator[](int64_t p_index) { return CharProxy<T>(p_index, _cowdata); }
_FORCE_INLINE_ CharStringT() {} _FORCE_INLINE_ CharStringT() {}
_FORCE_INLINE_ CharStringT(const CharStringT<T> &p_str) { _cowdata._ref(p_str._cowdata); } _FORCE_INLINE_ CharStringT(const CharStringT<T> &p_str) { _cowdata._ref(p_str._cowdata); }
@ -112,7 +112,7 @@ public:
void operator=(const T *p_cstr); void operator=(const T *p_cstr);
bool operator<(const CharStringT<T> &p_right) const; bool operator<(const CharStringT<T> &p_right) const;
CharStringT<T> &operator+=(T p_char); CharStringT<T> &operator+=(T p_char);
int length() const { return size() ? size() - 1 : 0; } int64_t length() const { return size() ? size() - 1 : 0; }
const T *get_data() const; const T *get_data() const;
operator const T *() const { return get_data(); }; operator const T *() const { return get_data(); };

View File

@ -76,7 +76,7 @@ bool CharStringT<T>::operator<(const CharStringT<T> &p_right) const {
template <class T> template <class T>
CharStringT<T> &CharStringT<T>::operator+=(T p_char) { CharStringT<T> &CharStringT<T>::operator+=(T p_char) {
const int lhs_len = length(); const int64_t lhs_len = length();
resize(lhs_len + 2); resize(lhs_len + 2);
T *dst = ptrw(); T *dst = ptrw();
@ -172,23 +172,23 @@ String::String(const char32_t *from) {
internal::gdextension_interface_string_new_with_utf32_chars(_native_ptr(), from); internal::gdextension_interface_string_new_with_utf32_chars(_native_ptr(), from);
} }
String String::utf8(const char *from, int len) { String String::utf8(const char *from, int64_t len) {
String ret; String ret;
ret.parse_utf8(from, len); ret.parse_utf8(from, len);
return ret; return ret;
} }
void String::parse_utf8(const char *from, int len) { void String::parse_utf8(const char *from, int64_t len) {
internal::gdextension_interface_string_new_with_utf8_chars_and_len(_native_ptr(), from, len); internal::gdextension_interface_string_new_with_utf8_chars_and_len(_native_ptr(), from, len);
} }
String String::utf16(const char16_t *from, int len) { String String::utf16(const char16_t *from, int64_t len) {
String ret; String ret;
ret.parse_utf16(from, len); ret.parse_utf16(from, len);
return ret; return ret;
} }
void String::parse_utf16(const char16_t *from, int len) { void String::parse_utf16(const char16_t *from, int64_t len) {
internal::gdextension_interface_string_new_with_utf16_chars_and_len(_native_ptr(), from, len); internal::gdextension_interface_string_new_with_utf16_chars_and_len(_native_ptr(), from, len);
} }
@ -230,8 +230,8 @@ String rtoss(double p_val) {
} }
CharString String::utf8() const { CharString String::utf8() const {
int length = internal::gdextension_interface_string_to_utf8_chars(_native_ptr(), nullptr, 0); int64_t length = internal::gdextension_interface_string_to_utf8_chars(_native_ptr(), nullptr, 0);
int size = length + 1; int64_t size = length + 1;
CharString str; CharString str;
str.resize(size); str.resize(size);
internal::gdextension_interface_string_to_utf8_chars(_native_ptr(), str.ptrw(), length); internal::gdextension_interface_string_to_utf8_chars(_native_ptr(), str.ptrw(), length);
@ -242,8 +242,8 @@ CharString String::utf8() const {
} }
CharString String::ascii() const { CharString String::ascii() const {
int length = internal::gdextension_interface_string_to_latin1_chars(_native_ptr(), nullptr, 0); int64_t length = internal::gdextension_interface_string_to_latin1_chars(_native_ptr(), nullptr, 0);
int size = length + 1; int64_t size = length + 1;
CharString str; CharString str;
str.resize(size); str.resize(size);
internal::gdextension_interface_string_to_latin1_chars(_native_ptr(), str.ptrw(), length); internal::gdextension_interface_string_to_latin1_chars(_native_ptr(), str.ptrw(), length);
@ -254,8 +254,8 @@ CharString String::ascii() const {
} }
Char16String String::utf16() const { Char16String String::utf16() const {
int length = internal::gdextension_interface_string_to_utf16_chars(_native_ptr(), nullptr, 0); int64_t length = internal::gdextension_interface_string_to_utf16_chars(_native_ptr(), nullptr, 0);
int size = length + 1; int64_t size = length + 1;
Char16String str; Char16String str;
str.resize(size); str.resize(size);
internal::gdextension_interface_string_to_utf16_chars(_native_ptr(), str.ptrw(), length); internal::gdextension_interface_string_to_utf16_chars(_native_ptr(), str.ptrw(), length);
@ -266,8 +266,8 @@ Char16String String::utf16() const {
} }
Char32String String::utf32() const { Char32String String::utf32() const {
int length = internal::gdextension_interface_string_to_utf32_chars(_native_ptr(), nullptr, 0); int64_t length = internal::gdextension_interface_string_to_utf32_chars(_native_ptr(), nullptr, 0);
int size = length + 1; int64_t size = length + 1;
Char32String str; Char32String str;
str.resize(size); str.resize(size);
internal::gdextension_interface_string_to_utf32_chars(_native_ptr(), str.ptrw(), length); internal::gdextension_interface_string_to_utf32_chars(_native_ptr(), str.ptrw(), length);
@ -278,8 +278,8 @@ Char32String String::utf32() const {
} }
CharWideString String::wide_string() const { CharWideString String::wide_string() const {
int length = internal::gdextension_interface_string_to_wide_chars(_native_ptr(), nullptr, 0); int64_t length = internal::gdextension_interface_string_to_wide_chars(_native_ptr(), nullptr, 0);
int size = length + 1; int64_t size = length + 1;
CharWideString str; CharWideString str;
str.resize(size); str.resize(size);
internal::gdextension_interface_string_to_wide_chars(_native_ptr(), str.ptrw(), length); internal::gdextension_interface_string_to_wide_chars(_native_ptr(), str.ptrw(), length);
@ -289,7 +289,7 @@ CharWideString String::wide_string() const {
return str; return str;
} }
Error String::resize(int p_size) { Error String::resize(int64_t p_size) {
return (Error)internal::gdextension_interface_string_resize(_native_ptr(), p_size); return (Error)internal::gdextension_interface_string_resize(_native_ptr(), p_size);
} }
@ -390,11 +390,11 @@ String &String::operator+=(const char32_t *p_str) {
return *this; return *this;
} }
const char32_t &String::operator[](int p_index) const { const char32_t &String::operator[](int64_t p_index) const {
return *internal::gdextension_interface_string_operator_index_const((GDExtensionStringPtr)this, p_index); return *internal::gdextension_interface_string_operator_index_const((GDExtensionStringPtr)this, p_index);
} }
char32_t &String::operator[](int p_index) { char32_t &String::operator[](int64_t p_index) {
return *internal::gdextension_interface_string_operator_index((GDExtensionStringPtr)this, p_index); return *internal::gdextension_interface_string_operator_index((GDExtensionStringPtr)this, p_index);
} }

View File

@ -46,11 +46,11 @@
namespace godot { namespace godot {
const uint8_t &PackedByteArray::operator[](int p_index) const { const uint8_t &PackedByteArray::operator[](int64_t p_index) const {
return *internal::gdextension_interface_packed_byte_array_operator_index_const((GDExtensionTypePtr *)this, p_index); return *internal::gdextension_interface_packed_byte_array_operator_index_const((GDExtensionTypePtr *)this, p_index);
} }
uint8_t &PackedByteArray::operator[](int p_index) { uint8_t &PackedByteArray::operator[](int64_t p_index) {
return *internal::gdextension_interface_packed_byte_array_operator_index((GDExtensionTypePtr *)this, p_index); return *internal::gdextension_interface_packed_byte_array_operator_index((GDExtensionTypePtr *)this, p_index);
} }
@ -62,12 +62,12 @@ uint8_t *PackedByteArray::ptrw() {
return internal::gdextension_interface_packed_byte_array_operator_index((GDExtensionTypePtr *)this, 0); return internal::gdextension_interface_packed_byte_array_operator_index((GDExtensionTypePtr *)this, 0);
} }
const Color &PackedColorArray::operator[](int p_index) const { const Color &PackedColorArray::operator[](int64_t p_index) const {
const Color *color = (const Color *)internal::gdextension_interface_packed_color_array_operator_index_const((GDExtensionTypePtr *)this, p_index); const Color *color = (const Color *)internal::gdextension_interface_packed_color_array_operator_index_const((GDExtensionTypePtr *)this, p_index);
return *color; return *color;
} }
Color &PackedColorArray::operator[](int p_index) { Color &PackedColorArray::operator[](int64_t p_index) {
Color *color = (Color *)internal::gdextension_interface_packed_color_array_operator_index((GDExtensionTypePtr *)this, p_index); Color *color = (Color *)internal::gdextension_interface_packed_color_array_operator_index((GDExtensionTypePtr *)this, p_index);
return *color; return *color;
} }
@ -80,11 +80,11 @@ Color *PackedColorArray::ptrw() {
return (Color *)internal::gdextension_interface_packed_color_array_operator_index((GDExtensionTypePtr *)this, 0); return (Color *)internal::gdextension_interface_packed_color_array_operator_index((GDExtensionTypePtr *)this, 0);
} }
const float &PackedFloat32Array::operator[](int p_index) const { const float &PackedFloat32Array::operator[](int64_t p_index) const {
return *internal::gdextension_interface_packed_float32_array_operator_index_const((GDExtensionTypePtr *)this, p_index); return *internal::gdextension_interface_packed_float32_array_operator_index_const((GDExtensionTypePtr *)this, p_index);
} }
float &PackedFloat32Array::operator[](int p_index) { float &PackedFloat32Array::operator[](int64_t p_index) {
return *internal::gdextension_interface_packed_float32_array_operator_index((GDExtensionTypePtr *)this, p_index); return *internal::gdextension_interface_packed_float32_array_operator_index((GDExtensionTypePtr *)this, p_index);
} }
@ -96,11 +96,11 @@ float *PackedFloat32Array::ptrw() {
return internal::gdextension_interface_packed_float32_array_operator_index((GDExtensionTypePtr *)this, 0); return internal::gdextension_interface_packed_float32_array_operator_index((GDExtensionTypePtr *)this, 0);
} }
const double &PackedFloat64Array::operator[](int p_index) const { const double &PackedFloat64Array::operator[](int64_t p_index) const {
return *internal::gdextension_interface_packed_float64_array_operator_index_const((GDExtensionTypePtr *)this, p_index); return *internal::gdextension_interface_packed_float64_array_operator_index_const((GDExtensionTypePtr *)this, p_index);
} }
double &PackedFloat64Array::operator[](int p_index) { double &PackedFloat64Array::operator[](int64_t p_index) {
return *internal::gdextension_interface_packed_float64_array_operator_index((GDExtensionTypePtr *)this, p_index); return *internal::gdextension_interface_packed_float64_array_operator_index((GDExtensionTypePtr *)this, p_index);
} }
@ -112,11 +112,11 @@ double *PackedFloat64Array::ptrw() {
return internal::gdextension_interface_packed_float64_array_operator_index((GDExtensionTypePtr *)this, 0); return internal::gdextension_interface_packed_float64_array_operator_index((GDExtensionTypePtr *)this, 0);
} }
const int32_t &PackedInt32Array::operator[](int p_index) const { const int32_t &PackedInt32Array::operator[](int64_t p_index) const {
return *internal::gdextension_interface_packed_int32_array_operator_index_const((GDExtensionTypePtr *)this, p_index); return *internal::gdextension_interface_packed_int32_array_operator_index_const((GDExtensionTypePtr *)this, p_index);
} }
int32_t &PackedInt32Array::operator[](int p_index) { int32_t &PackedInt32Array::operator[](int64_t p_index) {
return *internal::gdextension_interface_packed_int32_array_operator_index((GDExtensionTypePtr *)this, p_index); return *internal::gdextension_interface_packed_int32_array_operator_index((GDExtensionTypePtr *)this, p_index);
} }
@ -128,11 +128,11 @@ int32_t *PackedInt32Array::ptrw() {
return internal::gdextension_interface_packed_int32_array_operator_index((GDExtensionTypePtr *)this, 0); return internal::gdextension_interface_packed_int32_array_operator_index((GDExtensionTypePtr *)this, 0);
} }
const int64_t &PackedInt64Array::operator[](int p_index) const { const int64_t &PackedInt64Array::operator[](int64_t p_index) const {
return *internal::gdextension_interface_packed_int64_array_operator_index_const((GDExtensionTypePtr *)this, p_index); return *internal::gdextension_interface_packed_int64_array_operator_index_const((GDExtensionTypePtr *)this, p_index);
} }
int64_t &PackedInt64Array::operator[](int p_index) { int64_t &PackedInt64Array::operator[](int64_t p_index) {
return *internal::gdextension_interface_packed_int64_array_operator_index((GDExtensionTypePtr *)this, p_index); return *internal::gdextension_interface_packed_int64_array_operator_index((GDExtensionTypePtr *)this, p_index);
} }
@ -144,12 +144,12 @@ int64_t *PackedInt64Array::ptrw() {
return internal::gdextension_interface_packed_int64_array_operator_index((GDExtensionTypePtr *)this, 0); return internal::gdextension_interface_packed_int64_array_operator_index((GDExtensionTypePtr *)this, 0);
} }
const String &PackedStringArray::operator[](int p_index) const { const String &PackedStringArray::operator[](int64_t p_index) const {
const String *string = (const String *)internal::gdextension_interface_packed_string_array_operator_index_const((GDExtensionTypePtr *)this, p_index); const String *string = (const String *)internal::gdextension_interface_packed_string_array_operator_index_const((GDExtensionTypePtr *)this, p_index);
return *string; return *string;
} }
String &PackedStringArray::operator[](int p_index) { String &PackedStringArray::operator[](int64_t p_index) {
String *string = (String *)internal::gdextension_interface_packed_string_array_operator_index((GDExtensionTypePtr *)this, p_index); String *string = (String *)internal::gdextension_interface_packed_string_array_operator_index((GDExtensionTypePtr *)this, p_index);
return *string; return *string;
} }
@ -162,12 +162,12 @@ String *PackedStringArray::ptrw() {
return (String *)internal::gdextension_interface_packed_string_array_operator_index((GDExtensionTypePtr *)this, 0); return (String *)internal::gdextension_interface_packed_string_array_operator_index((GDExtensionTypePtr *)this, 0);
} }
const Vector2 &PackedVector2Array::operator[](int p_index) const { const Vector2 &PackedVector2Array::operator[](int64_t p_index) const {
const Vector2 *vec = (const Vector2 *)internal::gdextension_interface_packed_vector2_array_operator_index_const((GDExtensionTypePtr *)this, p_index); const Vector2 *vec = (const Vector2 *)internal::gdextension_interface_packed_vector2_array_operator_index_const((GDExtensionTypePtr *)this, p_index);
return *vec; return *vec;
} }
Vector2 &PackedVector2Array::operator[](int p_index) { Vector2 &PackedVector2Array::operator[](int64_t p_index) {
Vector2 *vec = (Vector2 *)internal::gdextension_interface_packed_vector2_array_operator_index((GDExtensionTypePtr *)this, p_index); Vector2 *vec = (Vector2 *)internal::gdextension_interface_packed_vector2_array_operator_index((GDExtensionTypePtr *)this, p_index);
return *vec; return *vec;
} }
@ -180,12 +180,12 @@ Vector2 *PackedVector2Array::ptrw() {
return (Vector2 *)internal::gdextension_interface_packed_vector2_array_operator_index((GDExtensionTypePtr *)this, 0); return (Vector2 *)internal::gdextension_interface_packed_vector2_array_operator_index((GDExtensionTypePtr *)this, 0);
} }
const Vector3 &PackedVector3Array::operator[](int p_index) const { const Vector3 &PackedVector3Array::operator[](int64_t p_index) const {
const Vector3 *vec = (const Vector3 *)internal::gdextension_interface_packed_vector3_array_operator_index_const((GDExtensionTypePtr *)this, p_index); const Vector3 *vec = (const Vector3 *)internal::gdextension_interface_packed_vector3_array_operator_index_const((GDExtensionTypePtr *)this, p_index);
return *vec; return *vec;
} }
Vector3 &PackedVector3Array::operator[](int p_index) { Vector3 &PackedVector3Array::operator[](int64_t p_index) {
Vector3 *vec = (Vector3 *)internal::gdextension_interface_packed_vector3_array_operator_index((GDExtensionTypePtr *)this, p_index); Vector3 *vec = (Vector3 *)internal::gdextension_interface_packed_vector3_array_operator_index((GDExtensionTypePtr *)this, p_index);
return *vec; return *vec;
} }
@ -198,12 +198,12 @@ Vector3 *PackedVector3Array::ptrw() {
return (Vector3 *)internal::gdextension_interface_packed_vector3_array_operator_index((GDExtensionTypePtr *)this, 0); return (Vector3 *)internal::gdextension_interface_packed_vector3_array_operator_index((GDExtensionTypePtr *)this, 0);
} }
const Variant &Array::operator[](int p_index) const { const Variant &Array::operator[](int64_t p_index) const {
const Variant *var = (const Variant *)internal::gdextension_interface_array_operator_index_const((GDExtensionTypePtr *)this, p_index); const Variant *var = (const Variant *)internal::gdextension_interface_array_operator_index_const((GDExtensionTypePtr *)this, p_index);
return *var; return *var;
} }
Variant &Array::operator[](int p_index) { Variant &Array::operator[](int64_t p_index) {
Variant *var = (Variant *)internal::gdextension_interface_array_operator_index((GDExtensionTypePtr *)this, p_index); Variant *var = (Variant *)internal::gdextension_interface_array_operator_index((GDExtensionTypePtr *)this, p_index);
return *var; return *var;
} }