Skip to content

Comments

general: rework the way files are included for better maintainability#1607

Merged
ruevs merged 3 commits intosolvespace:masterfrom
iscgar:iscgar/clean-up-file-inclusion
Sep 12, 2025
Merged

general: rework the way files are included for better maintainability#1607
ruevs merged 3 commits intosolvespace:masterfrom
iscgar:iscgar/clean-up-file-inclusion

Conversation

@iscgar
Copy link
Contributor

@iscgar iscgar commented Jul 2, 2025

Before this change, every header file was included in solvespace.h, inside the SolveSpace namespace. This meant that no header file could itself include a header file, unless it was included from solvespace.h as well, as otherwise standard library symbols would be injected into the SolveSpace namespace and used from there, causing compilation and linking issues.

It also meant that no header file could be included on its own, because its dependencies were only satisfied by the inclusion order in solvespace.h, which means that the only way to include anything was to just include solvespace.h.

An additional annoyance for developers was that code completion tools broke because of this inclusion pattern, making working on SolveSpace a tad more difficult.

This PR introduces as minimal changes as possible to make it so that the headers files are properly included at the top of solvespace.h, outside the SolveSpace namespace. As a consequence of this change, each header file needs its dependencies to be explicitly satisfied, so do that. This also required shuffling the order of a few definitions in some files, as well as extracting some small definitions into their own files, so they could be depended upon without causing a circular inclusion.

This is the latest iteration of the change that I mentioned here, after working with it in my local branches and tweaking it.

@phkahler
Copy link
Member

phkahler commented Jul 7, 2025

@iscgar I like some of this. Splitting out the handles into their own thing is IMHO a good idea. It might be good to separate out their functions as well, but this is a good start.

I'm not sure what the point of the "solvespace" namespace is. I presume it's more for the library than the application? Like when should something be in that namespace?

@iscgar
Copy link
Contributor Author

iscgar commented Jul 7, 2025

Yes, it's mostly for the benefit of the library, but it's also useful in general to avoid conflicts with global symbols defined by dependencies of SolveSpace. The need to put even source files that are not used by the library into the namespace was mainly for consistency, as otherwise we'd still need to add using namespace SolveSpace in the source files (the existing approach of putting that in the solvespace.h header was wrong, as it ends up polluting the global namesapce of anything that includes it).

Regarding the handles, I did it mainly to reduce the dependance on dsc.h in places that don't need it. Extracting the functions as well is a bit more complex due to interdependecies between the handle types, so I left most of them intact (that's why they are defined at the end of solvespace.h, after all of the handle types have been defined). As part of my 64-bit handles work I refactored this as well, and handles are a bit more self-contained, but I wanted to keep the diff for this PR as small as possible, just to achieve the stated goal. We can continue to iterate from there as needed afterwards.

@phkahler
Copy link
Member

@rpavlik can you weigh in on this namespace stuff?

@iscgar iscgar force-pushed the iscgar/clean-up-file-inclusion branch from f3f8959 to a8f2267 Compare August 30, 2025 19:27
@iscgar
Copy link
Contributor Author

iscgar commented Aug 30, 2025

Rebased on master to resolve conflicts after the merging of #1567 and #1613.

@phkahler
Copy link
Member

phkahler commented Sep 8, 2025

@ruevs do you want weigh in before I merge this?

@ruevs
Copy link
Member

ruevs commented Sep 9, 2025

@phkahler I'm fine with this 99%. The only thing I do not understand it the large rearrangement of code in sketch.h. @iscgar what necessitated it?

@iscgar
Copy link
Contributor Author

iscgar commented Sep 9, 2025

@phkahler I'm fine with this 99%. The only thing I do not understand it the large rearrangement of code in sketch.h. @iscgar what necessitated it?

I don't remember why I originally did it this way, but I think it was in order to reduce the number of forward declarations needed to get the code to work properly and to simplify the definition dependency, since Entity is defined in the same file and there's noting requiring it to be defined later.

Additionally, this branch is also the base of my (still WIP) List<T> and IdList<T> branch. I added some compile time assertions to simplify the code, but they require a complete type to work, so EntityList couldn't be instantiated until the definition of Entity was visible, but Group needs this definition for linked sketches, so the easiest fix for me was to move the definition of Entity to be before Group. This of course isn't a reason for pushing this change to master, though (at least not before my IdList work is ready for submission).

@ruevs
Copy link
Member

ruevs commented Sep 11, 2025

@iscgar in this case may I suggest that in this PR we commit a sketch.h that is as close as possible to cb561ba to avoid confusion.

I've made the necessary changes here https://github.com/ruevs/solvespace/commits/iscgar/clean-up-file-inclusion/ as a new commit. (it may better to interactively rebase and change 47a2e98 but I'll do it if you are OK with it. Or you can do it).

If/when you make a PR revamping List and IdList you can make the necessary changes.

@iscgar iscgar force-pushed the iscgar/clean-up-file-inclusion branch from a8f2267 to 09bc2a1 Compare September 11, 2025 20:10
Before this change, every header file was included in `solvespace.h`,
inside the `SolveSpace` namespace. This meant that no header file could
itself include a header file, unless it was included from `solvespace.h`
as well, as otherwise standard library symbols would be injected into
the `SolveSpace` namespace and used from there, causing compilation and
linking issues.

It also meant that no header file could be included on its own, because
its dependencies were only satisfied by the inclusion order in `solvespace.h`,
which means that the only way to include anything was to just include
`solvespace.h`.

Change it so that the headers files are properly included at the top of
`solvespace.h`, outside the `SolveSpace` namespace. As a consequence of
this change, each header file needs its dependencies to be explicitly
satisfied, so do that. This also required shuffling the order of a few
definitions in some files, as well as extracting some small definitions
into their own files, so they could be depended upon without causing a
circular inclusion.

While at it, remove the `using namespace SolveSpace` declaration from
`solvespace.h` in order to avoid polluting the global scope, and add the
needed namespace declaration in every place that required it.
This makes it easier to avoid including dsc.h when it's not needed.
Only include files that are actually needed, instead of using the huge
dependency that is `solvespace.h`.
@iscgar iscgar force-pushed the iscgar/clean-up-file-inclusion branch from 09bc2a1 to 68620cf Compare September 11, 2025 20:21
@iscgar
Copy link
Contributor Author

iscgar commented Sep 11, 2025

@iscgar in this case may I suggest that in this PR we commit a sketch.h that is as close as possible to cb561ba to avoid confusion.

I've made the necessary changes here https://github.com/ruevs/solvespace/commits/iscgar/clean-up-file-inclusion/ as a new commit. (it may better to interactively rebase and change 47a2e98 but I'll do it if you are OK with it. Or you can do it).

I've already made a similar change in my branch, and I'm only now seeing that I forgot to push it. Sorry about that. My changes are a bit different than yours, and I think they are reasonably minimal, but I'm fine with going with your changes, as the main thing I care about is the inclusion dependencies, and they're not affected by these changes.

If/when you make a PR revamping List and IdList you can make the necessary changes.

Sure thing.

@iscgar
Copy link
Contributor Author

iscgar commented Sep 11, 2025

The macOS failure is caused by Homebrew installing CMake 4 by default. We can pin the CI to the last supported version, or just bite the bullet and update the dependencies. I already updated the other dependencies in #1611, and I have updated versions of cairo and pixman locally, but becuase they are forked in the SolveSpace organsation it's not clear to me how I should contribute those changes. The reason is that the convention seems to be to branch from a release and apply the SolveSpace patches in the side branch, which is impossible to do with a PR flow. Any ideas on how to tackle this are welcome.

@ruevs
Copy link
Member

ruevs commented Sep 12, 2025

I've already made a similar change in my branch, and I'm only now seeing that I forgot to push it. Sorry about that. My changes are a bit different than yours, and I think they are reasonably minimal

This is good - it avoids all but one forward declaration with minimal rearrangement. There is a little bit more clean up possible in sketch.h see ruevs@ddf7d7b

Rebased here:
https://github.com/ruevs/solvespace/commits/iscgar/clean-up-file-inclusion/

@iscgar
Copy link
Contributor Author

iscgar commented Sep 12, 2025

I've already made a similar change in my branch, and I'm only now seeing that I forgot to push it. Sorry about that. My changes are a bit different than yours, and I think they are reasonably minimal

This is good - it avoids all but one forward declaration with minimal rearrangement. There is a little bit more clean up possible in sketch.h see ruevs@ddf7d7b

Rebased here: https://github.com/ruevs/solvespace/commits/iscgar/clean-up-file-inclusion/

The idea behind adding those include directives was to make sketch.h self contained. With your changes it only works if it's included after specific files, since Expr, ExprVector, ExprQuaternion, and the defintions from the other headers are referenced in sketch.h. While this does work for the build because anywhere sketch.h is included its dependencies are included before it, it won't work for e.g. providing reliable completion suggestions inside sketch.h itself, and in general I'm not a fan of relying on sibling include files to provide the necessary defintions.

@ruevs ruevs merged commit 1436d2c into solvespace:master Sep 12, 2025
3 of 4 checks passed
@ruevs
Copy link
Member

ruevs commented Sep 12, 2025

I'm convinced :-) Merged.

@ruevs
Copy link
Member

ruevs commented Sep 15, 2025

updated versions of cairo and pixman

I forgot about them. Done here: #1624

@iscgar iscgar deleted the iscgar/clean-up-file-inclusion branch September 16, 2025 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants