I think anyone who has written any significant C++ change has been at the point where the problem boiled down to "iterate all instances of a type, maybe filter and then do something with them". Often this is centered around an enum ArbitraryTypes for which an NUM_ARBITRARY_TYPES or GC.getNumArbitraryTypes(). You then want to either access CvArbitraryInfo or call something like SomeObject::someMethod(ArbitraryTypes eType).
This usually produces code like this:
This is not only very tedious to write, and can quickly become unreadable and unsafe, especially when multiple nested loops are involved. Which types were iI and iJ again? The compiler can't help you because you're casting anyway. Not to mention that unless properly refactored, those casts combined with meaningless variable names add a lot of needless business to the code.
Of course that could be somewhat improved with better naming conventions (hope is lost for Firaxis code though), but shouldn't a higher abstraction level of this be the preferred solution?
So I have no practical experience with C++ so I apologise for inaccurate terminology after this point, but how can we generify this problem? Is there a template or pattern I can use?
Some iterator generator that produces an iterator<ArbitraryTypes> of length NUM_ARBITRARY_TYPES would already help, but plain for looping over an iterator is still very tedious. Is there a template or macro that can help with this?
Some fantasy pseudocode:
As I said, I don't have enough C++ experience to produce sufficiently generic C++ sugar like that but I am tired of typing so much boilerplate. I know there are people who are better than me and can maybe help out with what's possible, and where to start, or even point me to mods that have already improved something in that area.
This usually produces code like this:
Code:
for (int iI = 0; iI < NUM_ARBITRARY_TYPES; iI++)
{
CvArbitraryInfo* kArbitraryInfo = GC.getArbitraryInfo((ArbitraryTypes)iI);
// do some stuff with your info class or whatever
}
Of course that could be somewhat improved with better naming conventions (hope is lost for Firaxis code though), but shouldn't a higher abstraction level of this be the preferred solution?
So I have no practical experience with C++ so I apologise for inaccurate terminology after this point, but how can we generify this problem? Is there a template or pattern I can use?
Some iterator generator that produces an iterator<ArbitraryTypes> of length NUM_ARBITRARY_TYPES would already help, but plain for looping over an iterator is still very tedious. Is there a template or macro that can help with this?
Some fantasy pseudocode:
Code:
MAGIC_TYPE_FOR(ArbitraryTypes, NUM_ARBITRARY_TYPES)
{
// this is a loop body where I have eArbitraryType of type ArbitraryTypes available (fine if needed to be declared earlier) and will iterate through all NUM_ARBITRARY_TYPES values and then stop.
}
As I said, I don't have enough C++ experience to produce sufficiently generic C++ sugar like that but I am tired of typing so much boilerplate. I know there are people who are better than me and can maybe help out with what's possible, and where to start, or even point me to mods that have already improved something in that area.