godot-cpp icon indicating copy to clipboard operation
godot-cpp copied to clipboard

Forward declarations of godot-cpp classes.

Open ariesssss opened this issue 2 years ago • 7 comments

Godot version

v4.1.1.stable.custom_build [bd6af8e0e]

godot-cpp version

4.1 4eed2d7be017ef06521665604990114c9fff7c77

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.

ariesssss avatar Oct 17 '23 09:10 ariesssss

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

AThousandShips avatar Oct 17 '23 11:10 AThousandShips

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?

ariesssss avatar Oct 17 '23 13:10 ariesssss

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

AThousandShips avatar Oct 17 '23 13:10 AThousandShips

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 :-)

dsnopek avatar Oct 17 '23 13:10 dsnopek

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.

ariesssss avatar Oct 20 '23 11:10 ariesssss

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.

Zylann avatar Oct 26 '23 14:10 Zylann

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.

dsnopek avatar Oct 26 '23 14:10 dsnopek