xref: /freebsd/contrib/llvm-project/libcxx/include/__utility/scope_guard.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric // -*- C++ -*-
2*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
3*700637cbSDimitry Andric //
4*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*700637cbSDimitry Andric //
8*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
9*700637cbSDimitry Andric 
10*700637cbSDimitry Andric #ifndef _LIBCPP___UTILITY_SCOPE_GUARD_H
11*700637cbSDimitry Andric #define _LIBCPP___UTILITY_SCOPE_GUARD_H
12*700637cbSDimitry Andric 
13*700637cbSDimitry Andric #include <__config>
14*700637cbSDimitry Andric #include <__utility/move.h>
15*700637cbSDimitry Andric 
16*700637cbSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17*700637cbSDimitry Andric #  pragma GCC system_header
18*700637cbSDimitry Andric #endif
19*700637cbSDimitry Andric 
20*700637cbSDimitry Andric _LIBCPP_PUSH_MACROS
21*700637cbSDimitry Andric #include <__undef_macros>
22*700637cbSDimitry Andric 
23*700637cbSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
24*700637cbSDimitry Andric 
25*700637cbSDimitry Andric template <class _Func>
26*700637cbSDimitry Andric class __scope_guard {
27*700637cbSDimitry Andric   _Func __func_;
28*700637cbSDimitry Andric 
29*700637cbSDimitry Andric public:
__scope_guard(_Func __func)30*700637cbSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __scope_guard(_Func __func) : __func_(std::move(__func)) {}
~__scope_guard()31*700637cbSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__scope_guard() { __func_(); }
32*700637cbSDimitry Andric 
33*700637cbSDimitry Andric   __scope_guard(const __scope_guard&)            = delete;
34*700637cbSDimitry Andric   __scope_guard& operator=(const __scope_guard&) = delete;
35*700637cbSDimitry Andric   __scope_guard& operator=(__scope_guard&&)      = delete;
36*700637cbSDimitry Andric 
37*700637cbSDimitry Andric // C++14 doesn't have mandatory RVO, so we have to provide a declaration even though no compiler will ever generate
38*700637cbSDimitry Andric // a call to the move constructor.
39*700637cbSDimitry Andric #if _LIBCPP_STD_VER <= 14
40*700637cbSDimitry Andric   __scope_guard(__scope_guard&&);
41*700637cbSDimitry Andric #else
42*700637cbSDimitry Andric   __scope_guard(__scope_guard&&) = delete;
43*700637cbSDimitry Andric #endif
44*700637cbSDimitry Andric };
45*700637cbSDimitry Andric 
46*700637cbSDimitry Andric template <class _Func>
__make_scope_guard(_Func __func)47*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __scope_guard<_Func> __make_scope_guard(_Func __func) {
48*700637cbSDimitry Andric   return __scope_guard<_Func>(std::move(__func));
49*700637cbSDimitry Andric }
50*700637cbSDimitry Andric 
51*700637cbSDimitry Andric _LIBCPP_END_NAMESPACE_STD
52*700637cbSDimitry Andric 
53*700637cbSDimitry Andric _LIBCPP_POP_MACROS
54*700637cbSDimitry Andric 
55*700637cbSDimitry Andric #endif // _LIBCPP___UTILITY_SCOPE_GUARD_H
56