ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
scope_exit.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2#ifndef TTG_UTIL_SCOPE_EXIT_H
3#define TTG_UTIL_SCOPE_EXIT_H
4
5//
6// N4189: Scoped Resource - Generic RAII Wrapper for the Standard Library
7// Peter Sommerlad and Andrew L. Sandoval
8// Adopted from https://github.com/tandasat/ScopedResource/tree/master
9//
10
11#include <type_traits>
12
13namespace ttg::detail {
14 template <typename EF>
16 {
17 // construction
18 explicit
19 scope_exit(EF &&f)
20 : exit_function(std::move(f))
21 , execute_on_destruction{ true }
22 { }
23
24 // move
26 : exit_function(std::move(rhs.exit_function))
27 , execute_on_destruction{ rhs.execute_on_destruction }
28 {
29 rhs.release();
30 }
31
32 // release
34 {
35 if (execute_on_destruction) this->exit_function();
36 }
37
38 void release()
39 {
40 this->execute_on_destruction = false;
41 }
42
43 private:
44 scope_exit(scope_exit const &) = delete;
45 void operator=(scope_exit const &) = delete;
46 scope_exit& operator=(scope_exit &&) = delete;
47 EF exit_function;
48 bool execute_on_destruction; // exposition only
49 };
50
51 template <typename EF>
52 auto make_scope_exit(EF &&exit_function)
53 {
54 return scope_exit<std::remove_reference_t<EF>>(std::forward<EF>(exit_function));
55 }
56
57} // namespace ttg::detail
58
59#endif // TTG_UTIL_SCOPE_EXIT_H
STL namespace.
auto make_scope_exit(EF &&exit_function)
Definition scope_exit.h:52
scope_exit(scope_exit &&rhs)
Definition scope_exit.h:25