#include <iostream>
struct Settings
{
Settings(int v_) : v(v_) {}
int v;
};
//http://stackoverflow.com/a/11255852
#define useHardwareWith(settings) \
acquireHardware(settings); \
for( \
struct \
{ \
struct inner \
{ \
inner() : done_(false) \
{ \
} \
void run() \
{ \
done_ = true; \
} \
~inner() \
{ \
releaseHardware(); \
} \
bool done_; \
}; \
bool done() const \
{ \
return inner_.done_; \
} \
bool run() \
{ \
inner_.done_ = true; \
} \
inner inner_; \
} magic; !magic.done() ; magic.run())
void acquireHardware(const Settings& settings)
{
std::cout << "acquire(" << settings.v << ")\n";
}
void releaseHardware()
{
std::cout << "release()\n";
}
void doHardwareStuff(const Settings& settings)
{
useHardwareWith(settings)
{
if (settings.v == 0)
{
std::cout << "side-exit\n";
return;
}
std::cout << "Doing important hardware stuff\n";
}
}
int main()
{
Settings settings(0);
doHardwareStuff(settings);
doHardwareStuff(Settings(1));
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKc3RydWN0IFNldHRpbmdzCnsKICAgIFNldHRpbmdzKGludCB2XykgOiB2KHZfKSB7fQogICAgaW50IHY7Cn07CgovL2h0dHA6Ly9zdGFja292ZXJmbG93LmNvbS9hLzExMjU1ODUyCiNkZWZpbmUgdXNlSGFyZHdhcmVXaXRoKHNldHRpbmdzKSAgICAgICAgICAgICAgICAgICBcCmFjcXVpcmVIYXJkd2FyZShzZXR0aW5ncyk7ICAgICAgICAgICAgICAgICAgICAgICAgICBcCmZvciggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgc3RydWN0ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgeyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICBzdHJ1Y3QgaW5uZXIJCQkJCQkJCQlcCiAgICB7CQkJCQkJCQkJCQkJXAogICAgICBpbm5lcigpIDogZG9uZV8oZmFsc2UpCQkJCQkJXAogICAgICB7CQkJCQkJCQkJCQkJXAogICAgICB9CQkJCQkJCQkJCQkJXAogICAgICB2b2lkIHJ1bigpCQkJCQkJCQkJXAogICAgICB7CQkJCQkJCQkJCQkJXAogICAgICAJZG9uZV8gPSB0cnVlOwkJCQkJCQkJXAogICAgICB9CQkJCQkJCQkJCQkJXAogICAgICB+aW5uZXIoKQkJCQkJCQkJCQlcCiAgICAgIHsJCQkJCQkJCQkJCQlcCiAgICAgIAlyZWxlYXNlSGFyZHdhcmUoKTsJCQkJCQkJXAogICAgICB9CQkJCQkJCQkJCQkJXAogICAgICBib29sIGRvbmVfOwkJCQkJCQkJCVwKICAgIH07ICAgCQkJCQkJCQkJCQlcCiAgICBib29sIGRvbmUoKSBjb25zdCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICB7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICAgIHJldHVybiBpbm5lcl8uZG9uZV87ICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICB9ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICBib29sIHJ1bigpICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICB7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICAgIGlubmVyXy5kb25lXyA9IHRydWU7ICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICB9ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICBpbm5lciBpbm5lcl87ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgfSBtYWdpYzsgIW1hZ2ljLmRvbmUoKSA7IG1hZ2ljLnJ1bigpKQoKdm9pZCBhY3F1aXJlSGFyZHdhcmUoY29uc3QgU2V0dGluZ3MmIHNldHRpbmdzKQp7CiAgc3RkOjpjb3V0IDw8ICJhY3F1aXJlKCIgPDwgc2V0dGluZ3MudiA8PCAiKVxuIjsKfQoKdm9pZCByZWxlYXNlSGFyZHdhcmUoKQp7CiAgc3RkOjpjb3V0IDw8ICJyZWxlYXNlKClcbiI7Cn0KCnZvaWQgZG9IYXJkd2FyZVN0dWZmKGNvbnN0IFNldHRpbmdzJiBzZXR0aW5ncykKewogIHVzZUhhcmR3YXJlV2l0aChzZXR0aW5ncykKICB7CiAgCWlmIChzZXR0aW5ncy52ID09IDApCiAgCXsKICAJCXN0ZDo6Y291dCA8PCAic2lkZS1leGl0XG4iOwogIAkJcmV0dXJuOwogIAl9CiAgICBzdGQ6OmNvdXQgPDwgIkRvaW5nIGltcG9ydGFudCBoYXJkd2FyZSBzdHVmZlxuIjsKICB9Cn0KCmludCBtYWluKCkKewogIFNldHRpbmdzIHNldHRpbmdzKDApOwogIGRvSGFyZHdhcmVTdHVmZihzZXR0aW5ncyk7CiAgZG9IYXJkd2FyZVN0dWZmKFNldHRpbmdzKDEpKTsKfQo=