I have multiple use cases where I need to call an "entry" function, do a bunch of stuff, and then call an "exit" function. (In my case, "entry" is enabling a chip select line and "exit" is disabling it.)
I'm tempted to do the following, but it seems too obvious. How might this trip me up?
#define WITH_CHIP_SELECTED(...) \
ChipSelect(true); \
__VA_ARGS__ \
ChipSelect(false);
// usage:
WITH_CHIP_SELECTED(
BeginTheBeguine();
GetOutOfTown();
BrushUpYourShakespeare();
DontFencMeIn();
)
According to godbolt, the C-preprocessor will expand to:
ChipSelect(true);
BeginTheBeguine();
GetOutOfTown();
BrushUpYourShakespeare();
DontFencMeIn();
ChipSelect(false);
Yes, I know longjump and non-local exits could circumvent calling the exit function, but that is not an issue here. Otherwise, this seems ideal for what I want. Am I missing something fundamental?