libdl  0.0.1
Simple yet powerful deep learning
Loading...
Searching...
No Matches
scopeguard.hpp
1#pragma once
2
3#include <assert.h>
4#include <functional>
5
6namespace dl::utils {
16 class ScopeGuard final {
17 private:
18 std::function<void()> onScopeExit;
19
20 ScopeGuard(const ScopeGuard&) = delete;
21 ScopeGuard& operator=(const ScopeGuard&) = delete;
22
23 public:
24 explicit ScopeGuard(auto&& onScopeExit) try : onScopeExit(std::forward<decltype(onScopeExit)>(onScopeExit)) {
25 } catch (...) {
26 onScopeExit();
27 throw;
28 }
29
30 explicit ScopeGuard(ScopeGuard&& other) : onScopeExit(std::move(other.onScopeExit)) {
31 assert(other.onScopeExit == nullptr);
32 }
33
34 ~ScopeGuard() {
35 if (onScopeExit != nullptr)
36 onScopeExit();
37 }
38 };
39} // namespace dl::utils
T forward(T... args)
T move(T... args)