How to prevent duplicate include?
-
My include classes are each within its own namespace.
How do I prevent them from being included twice?
For example, I have my expanded Math library and some of these includes rely on it, so I want to include MathX.js. But the main script will likely always have it.
Using the preprocessor
#if
#else
#endif
works great but the preprocessor is global so if I then want to include it in another script processor, that idea goes bust.Any way to prevent code execution within a single script processsor?
-
@aaronventure I only put includes directly in
on init
to avoid this issue. -
what do you mean? Where else would one put them? I get an error "Duplicate namespace" nonetheless.
-
@aaronventure said in How to prevent duplicate include?:
Where else would one put them?
Ah I thought you meant you were attempting to include them within another namespace.
@aaronventure said in How to prevent duplicate include?:
"Duplicate namespace"
That means you have two namespaces with the same name, so change the name of one of them.
-
@d-healey Alright here's an example.
I have Animation.js which also relies on MathX.js
I don't want to have t remember that, so if I load in Animation.js after I alreaded included MathX.js, the Animation.js should skip its own include for the MathX,js .
This can be done with the preprocessor, but the preprocessor constants are global so I can't do the same trick in another script processor.
The include call does not respect HISEScript's if/then -- I can't declare a HISEScript const inside the include file that I can then check for using if/then.
-
@aaronventure said in How to prevent duplicate include?:
Animation.js should skip its own include for the MathX,js .
This is what I mean by I put all my includes directly in
on init
not within another script file. That way I have to explicitly state what includes are available, the worst that happens is when I include a file it tells me that it can't find X function/namespace so I have to add another include. -
@aaronventure Haven't tried, but you could start all your namespaces with a variable of the same name, and check
isDefined(MyNamespace.MyVar)
in dependencies. If not defined just throw a message in the console for which include is required.
It's far from being automated but at least you get all the required includes at init showing in the console.A use of Console.assert might be helpful as well in the include's functions that depend on others includes/namespaces
-
@ustk yep that's what I'll do, thanks
-
@aaronventure I'm seeing a good use case for this in my latest project. I tried @ustk suggestion but it didn't work. Did you find a solution?
-
@d-healey Yeah, for each namespace, I have a exists constant.
Then in the ones that require other namespaces, I have, for example
if (MathX.exists == false) { Console.print("Controls.js requires MathX."); }
-
@aaronventure ah I get it now, thanks.
-
@Christoph-Hart What about adding
#pragma once
? Or just ignoring duplicate includes - I can't think of a downside to that but maybe there is.