xref: /aosp_15_r20/system/libbase/include/android-base/scopeguard.h (revision 8f0ba417480079999ba552f1087ae592091b9d02)
1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker  *
4*8f0ba417SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker  *
8*8f0ba417SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker  *
10*8f0ba417SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker  * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker  */
16*8f0ba417SAndroid Build Coastguard Worker 
17*8f0ba417SAndroid Build Coastguard Worker #pragma once
18*8f0ba417SAndroid Build Coastguard Worker 
19*8f0ba417SAndroid Build Coastguard Worker #include <utility>  // for std::move, std::forward
20*8f0ba417SAndroid Build Coastguard Worker 
21*8f0ba417SAndroid Build Coastguard Worker namespace android {
22*8f0ba417SAndroid Build Coastguard Worker namespace base {
23*8f0ba417SAndroid Build Coastguard Worker 
24*8f0ba417SAndroid Build Coastguard Worker // ScopeGuard ensures that the specified functor is executed no matter how the
25*8f0ba417SAndroid Build Coastguard Worker // current scope exits.
26*8f0ba417SAndroid Build Coastguard Worker template <typename F>
27*8f0ba417SAndroid Build Coastguard Worker class ScopeGuard {
28*8f0ba417SAndroid Build Coastguard Worker  public:
ScopeGuard(F f)29*8f0ba417SAndroid Build Coastguard Worker   constexpr ScopeGuard(F f) : f_(std::move(f)), active_(true) {}
30*8f0ba417SAndroid Build Coastguard Worker 
ScopeGuard(ScopeGuard && that)31*8f0ba417SAndroid Build Coastguard Worker   constexpr ScopeGuard(ScopeGuard&& that) noexcept : f_(std::move(that.f_)), active_(that.active_) {
32*8f0ba417SAndroid Build Coastguard Worker     that.active_ = false;
33*8f0ba417SAndroid Build Coastguard Worker   }
34*8f0ba417SAndroid Build Coastguard Worker 
35*8f0ba417SAndroid Build Coastguard Worker   template <typename Functor>
ScopeGuard(ScopeGuard<Functor> && that)36*8f0ba417SAndroid Build Coastguard Worker   constexpr ScopeGuard(ScopeGuard<Functor>&& that) noexcept
37*8f0ba417SAndroid Build Coastguard Worker       : f_(std::move(that.f_)), active_(that.active_) {
38*8f0ba417SAndroid Build Coastguard Worker     that.active_ = false;
39*8f0ba417SAndroid Build Coastguard Worker   }
40*8f0ba417SAndroid Build Coastguard Worker 
noexcept(noexcept (f_ ()))41*8f0ba417SAndroid Build Coastguard Worker   ~ScopeGuard() noexcept(noexcept(f_())) {
42*8f0ba417SAndroid Build Coastguard Worker     if (active_) f_();
43*8f0ba417SAndroid Build Coastguard Worker   }
44*8f0ba417SAndroid Build Coastguard Worker 
45*8f0ba417SAndroid Build Coastguard Worker   ScopeGuard() = delete;
46*8f0ba417SAndroid Build Coastguard Worker   ScopeGuard(const ScopeGuard&) = delete;
47*8f0ba417SAndroid Build Coastguard Worker   void operator=(const ScopeGuard&) = delete;
48*8f0ba417SAndroid Build Coastguard Worker   void operator=(ScopeGuard&& that) = delete;
49*8f0ba417SAndroid Build Coastguard Worker 
Disable()50*8f0ba417SAndroid Build Coastguard Worker   void Disable() noexcept { active_ = false; }
51*8f0ba417SAndroid Build Coastguard Worker 
active()52*8f0ba417SAndroid Build Coastguard Worker   constexpr bool active() const noexcept { return active_; }
53*8f0ba417SAndroid Build Coastguard Worker 
54*8f0ba417SAndroid Build Coastguard Worker  private:
55*8f0ba417SAndroid Build Coastguard Worker   template <typename Functor>
56*8f0ba417SAndroid Build Coastguard Worker   friend class ScopeGuard;
57*8f0ba417SAndroid Build Coastguard Worker 
58*8f0ba417SAndroid Build Coastguard Worker   F f_;
59*8f0ba417SAndroid Build Coastguard Worker   bool active_;
60*8f0ba417SAndroid Build Coastguard Worker };
61*8f0ba417SAndroid Build Coastguard Worker 
62*8f0ba417SAndroid Build Coastguard Worker template <typename F>
make_scope_guard(F && f)63*8f0ba417SAndroid Build Coastguard Worker auto make_scope_guard(F&& f) {
64*8f0ba417SAndroid Build Coastguard Worker   return ScopeGuard<std::remove_reference_t<F>>(std::forward<F>(f));
65*8f0ba417SAndroid Build Coastguard Worker }
66*8f0ba417SAndroid Build Coastguard Worker 
67*8f0ba417SAndroid Build Coastguard Worker }  // namespace base
68*8f0ba417SAndroid Build Coastguard Worker }  // namespace android
69