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

Forward declarations of godot-cpp classes. #1273

Open
ariesssss opened this issue Oct 17, 2023 · 7 comments
Open

Forward declarations of godot-cpp classes. #1273

ariesssss opened this issue Oct 17, 2023 · 7 comments
Labels
discussion enhancement This is an enhancement on the current functionality

Comments

@ariesssss
Copy link

Godot version

v4.1.1.stable.custom_build [bd6af8e0e]

godot-cpp version

4.1 4eed2d7

System information

Godot v4.1.1.stable (bd6af8e0e) - Linux Mint 21.2 (Victoria) - Vulkan (Forward+) - integrated Intel(R) Graphics (RPL-P) () - 13th Gen Intel(R) Core(TM) i7-1360P (16 Threads)

Issue description

When declaring, in a header file, a class member that is a pointer to a godot class, e.g. ::godot::Node * pointerToNode, I need to either #include <godot_cpp/classes/node.hpp> or add something like

namespace godot
{
  class Node;
}

I prefer the latter, because the #include slows down compilation. A godot header file with forward declarations for all godot classes would be nice.

I previously asked a question here: https://godotforums.org/d/36916-c-forward-declarations-for-godot-classes

Steps to reproduce

I hope you don't mind asking for this enhancement here, I thought of adding it to the godot repository itself, but it seems to me this issue is very much related to godot-cpp. Please let me know if I should file this issue elsewhere.

Minimal reproduction project

Any project will do.

@AThousandShips AThousandShips added enhancement This is an enhancement on the current functionality discussion labels Oct 17, 2023
@AThousandShips
Copy link
Member

I think that there's no bother to have to forward declare what you need, having one massive file to forward declare that you need to make sure contains everything seems unnecessary (and forces you to rebuild you entire plugin every time something changes in that file)

Just like you just include what you need, just forward declare what you need, programming with intentionality is important IMO

@ariesssss
Copy link
Author

That is certainly a valid point of view. I think it would be beneficial to add a file with forward declarations to godot-cpp, rather than each individual programmer create their own file. It saves time, avoids errors, remains up-to-date as godot-cpp evolves, all the usual arguments for including anything in a library. Some forward declarations can be non-trivial, e.g. template forward declarations. E.g. there is iosfwd, with forward declarations for the C++ input/output library.

forces you to rebuild you entire plugin every time something changes in that file

For me this is a relatively rare event, I'll take it. Whereas speed of compilation during the edit-compile-bugfix cycle is important, because I may do that many times per day. Forward declarations reduce compilation time.

I have a high regard for the boost library, I count 332 fwd files in the code.

It seems that generating the forward declarations header file can (partially?) be done from the JSON API file, is that correct?

@AThousandShips
Copy link
Member

AThousandShips commented Oct 17, 2023

I don't mean anyone should make their own file, simply that people should use forward declarations only where they are needed, which IMHO is the appropriate approach

I have a high regard for the boost library, I count 332 fwd files in the code.

Me too about the library, but I'm not sure those are intended for the end users but for internal support, which doesn't apply here

@dsnopek
Copy link
Collaborator

dsnopek commented Oct 17, 2023

For the most part, I agree with @AThousandShips, and think that making your own forward declarations for the classes you're using is just a normal part of using C++.

However, if making headers of forward declarations is becoming common practice for C++ libraries (I wasn't previously aware of this, but Googling, I can see iosfwd and the Boost *_fwd.hpp files that @ariesssss mentions) I really don't see any harm in generating such a file in binding_generator.py. Then it's there if folks want to use it, and those who don't can just ignore it.

PR's welcome, as always :-)

@ariesssss
Copy link
Author

I guess you are thinking of me when it comes to opening a PR. I looked at it, my brain is definitely not wired correctly for Python. But who knows, I might have a go at it.

@Zylann
Copy link
Collaborator

Zylann commented Oct 26, 2023

I think having a file for every Godot class with a single forward declaration in them is pointless. It doesn't help compared to a single line namespace godot { class X; }. If anything, it makes things worse by adding indirection.
Libraries that have forward-declarations headers such as <iosfwd> at least declare more than one thing in one go, because things like <iostream> define more than one thing as well.

@dsnopek
Copy link
Collaborator

dsnopek commented Oct 26, 2023

I think having a file for every Godot class with a single forward declaration in them is pointless.

Assuming I understood the idea correctly, I thought it was to have a single header file (ex #include <godot_cpp/classfwd.hpp>) that contained forward declarations for all the classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement This is an enhancement on the current functionality
Projects
None yet
Development

No branches or pull requests

4 participants