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