mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
Reland of #116560 and fixes the issued reported by #116695 Pull Request resolved: https://github.com/pytorch/pytorch/pull/117088 Approved by: https://github.com/albanD
28 lines
465 B
C++
28 lines
465 B
C++
#pragma once
|
|
#include <functional>
|
|
|
|
namespace torch {
|
|
namespace jit {
|
|
|
|
class ResourceGuard {
|
|
std::function<void()> _destructor;
|
|
bool _released{false};
|
|
|
|
public:
|
|
ResourceGuard(std::function<void()> destructor)
|
|
: _destructor(std::move(destructor)) {}
|
|
|
|
// NOLINTNEXTLINE(bugprone-exception-escape)
|
|
~ResourceGuard() {
|
|
if (!_released)
|
|
_destructor();
|
|
}
|
|
|
|
void release() {
|
|
_released = true;
|
|
}
|
|
};
|
|
|
|
} // namespace jit
|
|
} // namespace torch
|