2 * Copyright 2012 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_
23 #include <glog/logging.h>
25 #include "folly/Preprocessor.h"
30 * ScopeGuard is a general implementation of the "Initilization is
31 * Resource Acquisition" idiom. Basically, it guarantees that a function
32 * is executed upon leaving the currrent scope unless otherwise told.
34 * The makeGuard() function is used to create a new ScopeGuard object.
35 * It can be instantiated with a lambda function, a std::function<void()>,
36 * a functor, or a void(*)() function pointer.
39 * Usage example: Add a friend to memory iff it is also added to the db.
41 * void User::addFriend(User& newFriend) {
42 * // add the friend to memory
43 * friends_.push_back(&newFriend);
45 * // If the db insertion that follows fails, we should
46 * // remove it from memory.
47 * // (You could also declare this as "auto guard = makeGuard(...)")
48 * ScopeGuard guard = makeGuard([&] { friends_.pop_back(); });
50 * // this will throw an exception upon error, which
51 * // makes the ScopeGuard execute UserCont::pop_back()
52 * // once the Guard's destructor is called.
53 * db_->addFriend(GetName(), newFriend.GetName());
55 * // an exception was not thrown, so don't execute
60 * Examine ScopeGuardTest.cpp for some more sample usage.
63 * Andrei's and Petru Marginean's CUJ article:
64 * http://drdobbs.com/184403758
65 * and the loki library:
66 * http://loki-lib.sourceforge.net/index.php?n=Idioms.ScopeGuardPointer
67 * and triendl.kj article:
68 * http://www.codeproject.com/KB/cpp/scope_guard.aspx
70 class ScopeGuardImplBase {
72 void dismiss() noexcept {
78 : dismissed_(false) {}
80 ScopeGuardImplBase(ScopeGuardImplBase&& other)
81 : dismissed_(other.dismissed_) {
82 other.dismissed_ = true;
88 template<typename FunctionType>
89 class ScopeGuardImpl : public ScopeGuardImplBase {
91 explicit ScopeGuardImpl(const FunctionType& fn)
94 explicit ScopeGuardImpl(FunctionType&& fn)
95 : function_(std::move(fn)) {}
97 ScopeGuardImpl(ScopeGuardImpl&& other)
98 : ScopeGuardImplBase(std::move(other)),
99 function_(std::move(other.function_)) {
102 ~ScopeGuardImpl() noexcept {
109 void* operator new(size_t) = delete;
111 void execute() noexcept {
114 } catch (const std::exception& ex) {
115 LOG(FATAL) << "ScopeGuard cleanup function threw a " <<
116 typeid(ex).name() << "exception: " << ex.what();
118 LOG(FATAL) << "ScopeGuard cleanup function threw a non-exception object";
122 FunctionType function_;
125 template<typename FunctionType>
126 ScopeGuardImpl<typename std::decay<FunctionType>::type>
127 makeGuard(FunctionType&& fn) {
128 return ScopeGuardImpl<typename std::decay<FunctionType>::type>(
129 std::forward<FunctionType>(fn));
133 * This is largely unneeded if you just use auto for your guards.
135 typedef ScopeGuardImplBase&& ScopeGuard;
139 * Internal use for the macro SCOPE_EXIT below
141 enum class ScopeGuardOnExit {};
143 template <typename FunctionType>
144 ScopeGuardImpl<typename std::decay<FunctionType>::type>
145 operator+(detail::ScopeGuardOnExit, FunctionType&& fn) {
146 return ScopeGuardImpl<typename std::decay<FunctionType>::type>(
147 std::forward<FunctionType>(fn));
149 } // namespace detail
154 auto FB_ANONYMOUS_VARIABLE(SCOPE_EXIT_STATE) \
155 = ::folly::detail::ScopeGuardOnExit() + [&]
157 #endif // FOLLY_SCOPEGUARD_H_