58 lines · c
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___UTILITY_SCOPE_GUARD_H11#define _LIBCPP___UTILITY_SCOPE_GUARD_H12 13#include <__config>14#include <__utility/move.h>15 16#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)17# pragma GCC system_header18#endif19 20_LIBCPP_PUSH_MACROS21#include <__undef_macros>22 23_LIBCPP_BEGIN_NAMESPACE_STD24 25template <class _Func>26class __scope_guard {27 _Func __func_;28 29public:30 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __scope_guard(_Func __func) : __func_(std::move(__func)) {}31 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__scope_guard() { __func_(); }32 33 __scope_guard(const __scope_guard&) = delete;34 __scope_guard& operator=(const __scope_guard&) = delete;35 __scope_guard& operator=(__scope_guard&&) = delete;36 37// C++14 doesn't have mandatory RVO, so we have to provide a declaration even though no compiler will ever generate38// a call to the move constructor.39#if _LIBCPP_STD_VER <= 1440 __scope_guard(__scope_guard&&);41#else42 __scope_guard(__scope_guard&&) = delete;43#endif44};45 46_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__scope_guard);47 48template <class _Func>49_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __scope_guard<_Func> __make_scope_guard(_Func __func) {50 return __scope_guard<_Func>(std::move(__func));51}52 53_LIBCPP_END_NAMESPACE_STD54 55_LIBCPP_POP_MACROS56 57#endif // _LIBCPP___UTILITY_SCOPE_GUARD_H58