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

Use struct instead of class for core structures #880

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("")

for type_name in used_classes:
if is_native_struct(type_name):
if is_struct_type(type_name):
result.append(f"struct {type_name};")
else:
result.append(f"class {type_name};")
Expand Down Expand Up @@ -1109,7 +1109,7 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
result.append("")

for type_name in used_classes:
if is_native_struct(type_name):
if is_struct_type(type_name):
result.append(f"struct {type_name};")
else:
result.append(f"class {type_name};")
Expand Down Expand Up @@ -1826,15 +1826,18 @@ def is_pod_type(type_name):


def is_included_type(type_name):
"""
Those are types for which we already have a class file implemented.
"""
# Types which we already have implemented.
return is_included_struct_type(type_name) or type_name in ["ObjectID"]


def is_included_struct_type(type_name):
# Struct types which we already have implemented.
return type_name in [
"AABB",
"Basis",
"Color",
"ObjectID",
"Plane",
"Projection",
"Quaternion",
"Rect2",
"Rect2i",
Expand All @@ -1846,7 +1849,6 @@ def is_included_type(type_name):
"Vector3i",
"Vector4",
"Vector4i",
"Projection",
]


Expand Down Expand Up @@ -1922,9 +1924,10 @@ def is_engine_class(type_name):
return type_name == "Object" or type_name in engine_classes


def is_native_struct(type_name):
def is_struct_type(type_name):
# This is used to determine which keyword to use for forward declarations.
global native_structures
return type_name in native_structures
return is_included_struct_type(type_name) or type_name in native_structures


def is_refcounted(type_name):
Expand Down
4 changes: 4 additions & 0 deletions include/godot_cpp/core/defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
#endif
#endif

#ifndef _NO_DISCARD_
#define _NO_DISCARD_ [[nodiscard]]
#endif

// Windows badly defines a lot of stuff we'll never use. Undefine it.
#ifdef _WIN32
#undef min // override standard definition
Expand Down
7 changes: 1 addition & 6 deletions include/godot_cpp/variant/aabb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@

namespace godot {

class AABB {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ AABB {
Vector3 position;
Vector3 size;

Expand Down
7 changes: 1 addition & 6 deletions include/godot_cpp/variant/basis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@

namespace godot {

class Basis {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Basis {
Vector3 rows[3] = {
Vector3(1, 0, 0),
Vector3(0, 1, 0),
Expand Down
7 changes: 1 addition & 6 deletions include/godot_cpp/variant/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ namespace godot {

class String;

class Color {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Color {
union {
struct {
float r;
Expand Down
7 changes: 1 addition & 6 deletions include/godot_cpp/variant/plane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@

namespace godot {

class Plane {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Plane {
Vector3 normal;
real_t d = 0;

Expand Down
17 changes: 6 additions & 11 deletions include/godot_cpp/variant/projection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,13 @@

namespace godot {

class AABB;
class Plane;
class Rect2;
class Transform3D;
class Vector2;
struct AABB;
struct Plane;
struct Rect2;
struct Transform3D;
struct Vector2;

class Projection {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Projection {
enum Planes {
PLANE_NEAR,
PLANE_FAR,
Expand Down
7 changes: 1 addition & 6 deletions include/godot_cpp/variant/quaternion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@

namespace godot {

class Quaternion {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Quaternion {
union {
struct {
real_t x;
Expand Down
11 changes: 3 additions & 8 deletions include/godot_cpp/variant/rect2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,11 @@

namespace godot {

class Rect2i;
class String;
class Transform2D;
struct Rect2i;
struct Transform2D;

class Rect2 {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Rect2 {
Point2 position;
Size2 size;

Expand Down
9 changes: 2 additions & 7 deletions include/godot_cpp/variant/rect2i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,10 @@

namespace godot {

class Rect2;
class String;
struct Rect2;

class Rect2i {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Rect2i {
Point2i position;
Size2i size;

Expand Down
7 changes: 1 addition & 6 deletions include/godot_cpp/variant/transform2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@

namespace godot {

class Transform2D {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Transform2D {
// Warning #1: basis of Transform2D is stored differently from Basis. In terms of columns array, the basis matrix looks like "on paper":
// M = (columns[0][0] columns[1][0])
// (columns[0][1] columns[1][1])
Expand Down
7 changes: 1 addition & 6 deletions include/godot_cpp/variant/transform3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@

namespace godot {

class Transform3D {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Transform3D {
Basis basis;
Vector3 origin;

Expand Down
9 changes: 2 additions & 7 deletions include/godot_cpp/variant/vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,9 @@
namespace godot {

class String;
class Vector2i;
struct Vector2i;

class Vector2 {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Vector2 {
static const int AXIS_COUNT = 2;

enum Axis {
Expand Down
9 changes: 2 additions & 7 deletions include/godot_cpp/variant/vector2i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,9 @@
namespace godot {

class String;
class Vector2;
struct Vector2;

class Vector2i {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Vector2i {
static const int AXIS_COUNT = 2;

enum Axis {
Expand Down
13 changes: 4 additions & 9 deletions include/godot_cpp/variant/vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,12 @@

namespace godot {

class Basis;
class String;
class Vector2;
class Vector3i;
struct Basis;
struct Vector2;
struct Vector3i;

class Vector3 {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Vector3 {
static const int AXIS_COUNT = 3;

enum Axis {
Expand Down
9 changes: 2 additions & 7 deletions include/godot_cpp/variant/vector3i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,9 @@
namespace godot {

class String;
class Vector3;
struct Vector3;

class Vector3i {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Vector3i {
static const int AXIS_COUNT = 3;

enum Axis {
Expand Down
7 changes: 1 addition & 6 deletions include/godot_cpp/variant/vector4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ namespace godot {

class String;

class Vector4 {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Vector4 {
static const int AXIS_COUNT = 4;

enum Axis {
Expand Down
9 changes: 2 additions & 7 deletions include/godot_cpp/variant/vector4i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,9 @@
namespace godot {

class String;
class Vector4;
struct Vector4;

class Vector4i {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
struct _NO_DISCARD_ Vector4i {
static const int AXIS_COUNT = 4;

enum Axis {
Expand Down
Loading