pytorch/torch/csrc/jit/resource_guard.h
Michael Suo f636dc9276 clang format world (#15524)
Summary:
The PR clang-formats everything in `torch/csrc/jit/` and adds it to the pre-commit hook.

Here is a list of non-mechanical changes:
- I went over each file and fixed up whenever I could tell that clang-format was clobbering comment formatting.
- Made the macros in register_prim_ops a little more clang-format friendly by omitting trailing commas
- Refactored autodiff.cpp to use a helper class with explicit state rather than a bunch of capturing lambdas
- Small improvements to the precommit hook clang-format
Pull Request resolved: https://github.com/pytorch/pytorch/pull/15524

Differential Revision: D13547989

Pulled By: suo

fbshipit-source-id: 3ff1541bb06433ccfe6de6e33f29227a2b5bb493
2018-12-26 06:55:01 -08:00

27 lines
429 B
C++

#pragma once
#include <functional>
namespace torch {
namespace jit {
class ResourceGuard {
std::function<void()> _destructor;
bool _released;
public:
ResourceGuard(std::function<void()> destructor)
: _destructor(std::move(destructor)), _released(false) {}
~ResourceGuard() {
if (!_released)
_destructor();
}
void release() {
_released = true;
}
};
} // namespace jit
} // namespace torch