mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary: Test-plan: Compile + clang-tidy Pull Request resolved: https://github.com/pytorch/pytorch/pull/61142 Reviewed By: VitalyFedyunin Differential Revision: D29529372 Pulled By: malfet fbshipit-source-id: 2ccde7712a51c28243b16bbb4d1d68086e0414a6
28 lines
476 B
C++
28 lines
476 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) {}
|
|
|
|
// NOLINTNEXTLINE(bugprone-exception-escape)
|
|
~ResourceGuard() {
|
|
if (!_released)
|
|
_destructor();
|
|
}
|
|
|
|
void release() {
|
|
_released = true;
|
|
}
|
|
};
|
|
|
|
} // namespace jit
|
|
} // namespace torch
|