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