IntFlags.pushPop

A mixin string that may be used to (effectively) push a new IntFlags.local variable onto the stack at the beginning of a scope, and restore the previous one at the end.

Any flags raised during the scope must be manually checked, handled, and cleared before the end, otherwise a debugging assert will be triggered to warn that restoring the old IntFlags.local value would cause a loss of information.

struct IntFlags
enum string pushPop;

Examples

import checkedint.sticky : raise; // set IntFlagPolicy.sticky

string[] log;

void onlyZero(int x)
{
    mixin(IntFlags.pushPop);

    if (x < 0)
        raise(IntFlag.negOver);
    if (x > 0)
        raise(IntFlag.posOver);

    if (IntFlags.local)
        log ~= IntFlags.local.clear().toString();
}

IntFlags.local = IntFlag.imag;
onlyZero(-50);
onlyZero(22);
onlyZero(0);
assert(IntFlags.local.clear() == IntFlag.imag);

assert(log == ["{negative overflow}", "{positive overflow}"]);

Meta