Transportman wrote:Thanks for the response, but I still cannot get it to work, it replaces the argument with the name of the #define instead of the value of the define. And I can't understand why HEAD_CAPACITY is replaced just fine.
"cpp" is a C pre-processor, and it applies C conventions on names in replacements. In particular, it does not break names to replace parts of it with #define content.
It finds "switch_VEHICLEIDENTIFIER_capacity_engine" as name, and compares that with "VEHICLEIDENTIFIER" #define name, and concludes the names are not the same, and does not do replacement.
If finds "HEAD_CAPACITY" as name, and compares that with "HEAD_CAPACITY" #define name, and concludes it's the same, and replace the HEAD_CAPACITY with the content of the #define.
To get VEHICLEIDENTIFIER replaced, the ## magic comes in (
https://gcc.gnu.org/onlinedocs/cpp/Conc ... catenation ). Basically, it allows parameters from the #define to get replaced before joining them together to a legal C name. The documentation doesn't say anything about replacing definitions first, it doesn't seem designed to do this at all. Your best bet imho is not to try this, and instead specify the value of VEHICLEIDENTIFIER as parameter
Code: Select all
#define HEAD_CAPACITY infinite
#define ENGINECAPACITYSWITCH(VEHICLEIDENTIFIER, VEHICLE_NAME, TYPE) \
switch(FEAT_TRAINS, SELF, switch_ ## VEHICLEIDENTIFIER ## _capacity_engine, cargo_classes) { \
bitmask(CC_MAIL): HEAD_CAPACITY/2; \
bitmask(CC_ARMOURED): HEAD_CAPACITY/4; \
HEAD_CAPACITY; \
}
ENGINECAPACITYSWITCH(vi, abc, nonetype)
results in
Code: Select all
switch(FEAT_TRAINS, SELF, switch_vi_capacity_engine, cargo_classes) { bitmask(CC_MAIL): infinite/2; bitmask(CC_ARMOURED): infinite/4; infinite; }
Far from ideal, I agree. Basically, you need something better than cpp. m4 would work, but it comes with its own quirks. Andythenorth basically dropped cpp, and uses Python with templating instead. Perhaps someone needs to make a better processing language on top of nml (like cpp, but better), or nml could be extended.
The latter doesn't work nicely with the parser though in an experiment I once tried, so it involves much more work than it seems at first sight.
Being a retired OpenTTD developer does not mean I know what I am doing.