Skip to content

Commit fa22d53

Browse files
committed
Move builtins' clone constructors into the pxd
1 parent 8c51e7a commit fa22d53

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

src/godot/builtins_pxd/class.pxd.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
{% from 'builtins_pxd/constructor.pxd.j2' import render_constructors with context %}
12
{% from 'builtins_pxd/method.pxd.j2' import render_method, render_method_signature with context %}
23
{% from 'builtins_pxd/member.pxd.j2' import
34
render_member,
45
render_member_getter_signature,
56
render_member_setter_signature
67
with context %}
78

9+
810
{% macro render_class_resume(spec) %}
911
# cdef class {{ spec.name }}:
1012
{% for m in spec.members %}
@@ -23,6 +25,8 @@ with context %}
2325
cdef class {{ spec.name }}:
2426
cdef {{ spec.c_struct_name }} _gd_data
2527

28+
{{ render_constructors(spec) | indent }}
29+
2630
@staticmethod
2731
cdef inline {{ spec.name }} new():
2832
# Call to __new__ bypasses __init__ constructor
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{# In Godot you would do `var v2 = Vector2(v1)`, but it's more Pythonic #}
2+
{# to provide a clone method and do `v2 = v1.clone()` (and it simplifies #}
3+
{# constructor implementation !) #}
4+
{% macro render_clone_constructor(spec) -%}
5+
cpdef inline {{ spec.name }} clone(self):
6+
cdef {{ spec.name }} obj = {{ spec.name }}.__new__({{ spec.name }})
7+
{% if spec.is_transparent_c_struct %}
8+
obj._gd_data = self._gd_data
9+
{% else %}
10+
{# Opaque builtin, must retrieve and use the clone constructor function #}
11+
cdef GDNativeTypePtr[1] args = [&self._gd_data]
12+
__{{ spec.name }}_constructor_{{ spec.clone_constructor_index }}(&obj._gd_data, args)
13+
{% endif %}
14+
return obj
15+
{%- endmacro %}
16+
17+
18+
{#########################################################################
19+
# Render constructors
20+
#########################################################################}
21+
22+
23+
{% macro render_constructors(spec) -%}
24+
{{ render_clone_constructor(spec) }}
25+
{% endmacro %}

src/godot/builtins_pyx/constructor.pyx.j2

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
{#########################################################################
2-
# Render default constructor
3-
#########################################################################}
4-
5-
61
{% macro render_default_constructor(spec) -%}
72
{% if spec.is_transparent_c_struct %}
83

@@ -25,38 +20,11 @@ def __cinit__(self):
2520
{%- endmacro %}
2621

2722

28-
{#########################################################################
29-
# Render clone constructor
30-
#########################################################################}
31-
32-
{# In Godot you would do `var v2 = Vector2(v1)`, but it's more Pythonic #}
33-
{# to provide a clone method and do `v2 = v1.clone()` (and it simplifies #}
34-
{# constructor implementation !) #}
35-
{% macro render_clone_constructor(spec) -%}
36-
def clone(self):
37-
cdef {{ spec.name }} obj = {{ spec.name }}.__new__({{ spec.name }})
38-
{% if spec.is_transparent_c_struct %}
39-
obj._gd_data = self._gd_data
40-
{% else %}
41-
{# Opaque builtin, must retrieve and use the clone constructor function #}
42-
cdef GDNativeTypePtr[1] args = [&self._gd_data]
43-
__{{ spec.name }}_constructor_{{ spec.clone_constructor_index }}(&obj._gd_data, args)
44-
{% endif %}
45-
return obj
46-
{%- endmacro %}
47-
48-
49-
{#########################################################################
50-
# Render constructors
51-
#########################################################################}
52-
53-
5423
{% macro render_constructors(spec) -%}
5524
{# Default constructor #}
5625
{% if spec.name != "GDString" and not spec.is_packed_array %}
5726
{{ render_default_constructor(spec) }}
5827
{% endif %}
59-
{{ render_clone_constructor(spec) }}
6028

6129
{# And now for the custom constructors ! #}
6230

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pxd_godot_builtins = custom_target(
9292
files(
9393
'godot/builtins.pxd.j2',
9494
'godot/builtins_pxd/class.pxd.j2',
95+
'godot/builtins_pxd/constructor.pxd.j2',
9596
'godot/builtins_pxd/member.pxd.j2',
9697
'godot/builtins_pxd/method.pxd.j2',
9798
),

0 commit comments

Comments
 (0)