Page 1 of 1

Circular include best ways to avoid?

Posted: Thu Nov 14, 2024 11:02 pm
by emperor-limitless
so I have file a.pbi, it contains a structure that uses something from b.pbi as a list, then I have procedures that take pointers of a in b.pbi, as well as a prototype function. As you would have figured, I ran into a include sircular loop, if I use XIncludeFile, well, it will loop, and if I include somewhere else one of them will be included before the other so they just both break anyway
so any ideas? I would like to avoid putting 2 directly unrelated structures in one place, because I'll eventually have a messy bunch of files, in C/C++ this will be done by headers, but I'm not sure if this would work considering there are no structure declarations to my knowledge, only procedure declarations, which fixes half the problem.
I'm aware that I likely could go rewrite the code to avoid the problem, but I'm curious if there's no solution to that?

Re: Circular include best ways to avoid?

Posted: Fri Nov 15, 2024 4:16 am
by Kulrom
You already have spaghetti code. Make a separate file in which you declare all the structures.

Re: Circular include best ways to avoid?

Posted: Fri Nov 15, 2024 10:50 am
by spikey
emperor-limitless wrote: Thu Nov 14, 2024 11:02 pm but I'm curious if there's no solution to that?
Not automagically no. Although PB doesn't specifically use a header file arrangement there's no reason why you can't use either a '.pb' or a '.pbi' file to contain header information like structures and enumerations, they don't have to include procedures too.

Personally I dislike multilevel including so I usually put something like this at the top of dependent files.

Code: Select all

CompilerIf Not Defined(SOMETHING, #PB_Structure)
  CompilerError "Structure SOMETHING is not defined. Forgot hdrSomething.pbi?"
CompilerEndIf
but otherwise you can happily XInclude 'hdrSomething.pbi' in a '.pbi' and 'b.pbi' right at the top and you'll get the outcome you're looking for.

Re: Circular include best ways to avoid?

Posted: Fri Nov 15, 2024 10:56 am
by Fred
Pointers doesn't need forward structure declaration, it's resolved when accessing the structure (so prototype and procedure declaration should work).

Re: Circular include best ways to avoid?

Posted: Fri Nov 15, 2024 11:40 am
by emperor-limitless
ah I see, certainly interesting. Most of my time I used C# which doesn't care about where something is defined so this is going to be an interesting experience to learn. I just hope no one get annoyed at me because I ask a lot of questions :)