]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/defer.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
10 namespace ROCKSDB_NAMESPACE
{
12 // Defers the execution of the provided function until the Defer
13 // object goes out of scope.
17 // Status DeferTest() {
19 // Defer defer([&s]() {
24 // // do something ...
25 // if (!s.ok()) return;
26 // // do some other things ...
30 // The above code ensures that cleanups will always happen on returning.
32 // Without the help of Defer, you can
33 // 1. every time when !s.ok(), do the cleanup;
34 // 2. instead of returning when !s.ok(), continue the work only when s.ok(),
35 // but sometimes, this might lead to nested blocks of "if (s.ok()) {...}".
37 // With the help of Defer, you can centralize the cleanup logic inside the
38 // lambda passed to Defer, and you can return immediately on failure when necessary.
41 Defer(std::function
<void()>&& fn
) : fn_(std::move(fn
)) {}
45 Defer(const Defer
&) = delete;
46 Defer
& operator=(const Defer
&) = delete;
49 std::function
<void()> fn_
;
52 } // namespace ROCKSDB_NAMESPACE