File: scope.hpp

package info (click to toggle)
libhx 5.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,664 kB
  • sloc: ansic: 10,332; sh: 5,230; cpp: 133; makefile: 116
file content (35 lines) | stat: -rw-r--r-- 740 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma once
#include <exception>
#include <utility>

namespace HX {

/*
 * Modeled upon the C++ standards proposal P0052r10 / Library Fundamentals v3.
 * Not yet present in GNU stdlibc++ or clang libc++.
 */
template<typename F> class scope_exit {
	private:
	F m_func;
	bool m_eod = false;

	public:
	explicit scope_exit(F &&f) : m_func(std::move(f)), m_eod(true) {}
	scope_exit(scope_exit &&o) : m_func(std::move(o.m_func)), m_eod(o.m_eod) {
		o.m_eod = false;
	}
	~scope_exit() try {
		if (m_eod)
			m_func();
	} catch (...) {
	}
	void operator=(scope_exit &&) = delete;
	void release() noexcept { m_eod = false; }
};

template<typename F> scope_exit<F> make_scope_exit(F &&f)
{
	return scope_exit<F>(std::move(f));
}

} /* namespace */