2 * Copyright 2013 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef FOLLY_SCOPEGUARD_H_
18 #define FOLLY_SCOPEGUARD_H_
24 #include "folly/Preprocessor.h"
29 * ScopeGuard is a general implementation of the "Initialization is
30 * Resource Acquisition" idiom. Basically, it guarantees that a function
31 * is executed upon leaving the currrent scope unless otherwise told.
33 * The makeGuard() function is used to create a new ScopeGuard object.
34 * It can be instantiated with a lambda function, a std::function<void()>,
35 * a functor, or a void(*)() function pointer.
38 * Usage example: Add a friend to memory iff it is also added to the db.
40 * void User::addFriend(User& newFriend) {
41 * // add the friend to memory
42 * friends_.push_back(&newFriend);
44 * // If the db insertion that follows fails, we should
45 * // remove it from memory.
46 * // (You could also declare this as "auto guard = makeGuard(...)")
47 * ScopeGuard guard = makeGuard([&] { friends_.pop_back(); });
49 * // this will throw an exception upon error, which
50 * // makes the ScopeGuard execute UserCont::pop_back()
51 * // once the Guard's destructor is called.
52 * db_->addFriend(GetName(), newFriend.GetName());
54 * // an exception was not thrown, so don't execute
59 * Examine ScopeGuardTest.cpp for some more sample usage.
62 * Andrei's and Petru Marginean's CUJ article:
63 * http://drdobbs.com/184403758
64 * and the loki library:
65 * http://loki-lib.sourceforge.net/index.php?n=Idioms.ScopeGuardPointer
66 * and triendl.kj article:
67 * http://www.codeproject.com/KB/cpp/scope_guard.aspx
69 class ScopeGuardImplBase {
71 void dismiss() noexcept {
77 : dismissed_(false) {}
79 ScopeGuardImplBase(ScopeGuardImplBase&& other)
80 : dismissed_(other.dismissed_) {
81 other.dismissed_ = true;
87 template<typename FunctionType>
88 class ScopeGuardImpl : public ScopeGuardImplBase {
90 explicit ScopeGuardImpl(const FunctionType& fn)
93 explicit ScopeGuardImpl(FunctionType&& fn)
94 : function_(std::move(fn)) {}
96 ScopeGuardImpl(ScopeGuardImpl&& other)
97 : ScopeGuardImplBase(std::move(other)),
98 function_(std::move(other.function_)) {
101 ~ScopeGuardImpl() noexcept {
108 void* operator new(size_t) = delete;
110 void execute() noexcept { function_(); }
112 FunctionType function_;
115 template<typename FunctionType>
116 ScopeGuardImpl<typename std::decay<FunctionType>::type>
117 makeGuard(FunctionType&& fn) {
118 return ScopeGuardImpl<typename std::decay<FunctionType>::type>(
119 std::forward<FunctionType>(fn));
123 * This is largely unneeded if you just use auto for your guards.
125 typedef ScopeGuardImplBase&& ScopeGuard;
129 * Internal use for the macro SCOPE_EXIT below
131 enum class ScopeGuardOnExit {};
133 template <typename FunctionType>
134 ScopeGuardImpl<typename std::decay<FunctionType>::type>
135 operator+(detail::ScopeGuardOnExit, FunctionType&& fn) {
136 return ScopeGuardImpl<typename std::decay<FunctionType>::type>(
137 std::forward<FunctionType>(fn));
139 } // namespace detail
144 auto FB_ANONYMOUS_VARIABLE(SCOPE_EXIT_STATE) \
145 = ::folly::detail::ScopeGuardOnExit() + [&]
147 #endif // FOLLY_SCOPEGUARD_H_