DebugServer2
Loading...
Searching...
No Matches
ScopedJanitor.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11// This is a dummy implementation of the ScopeGuard idiom. We don't care about
12// exceptions here, we just want to run some cleanup code at scope exit.
13
14#pragma once
15
16#include <utility>
17
18namespace ds2 {
19namespace Utils {
20
21template <typename Callable> class ScopedJanitor {
22public:
23 ScopedJanitor(Callable &&c) : _enabled(true), _c(std::forward<Callable>(c)) {}
25 if (_enabled) {
26 _c();
27 }
28 }
29
30 ScopedJanitor(ScopedJanitor &&rhs) : _c(std::move(rhs._c)) { rhs.disable(); }
31
32 void disable() { _enabled = false; }
33
34 ScopedJanitor(ScopedJanitor const &rhs) = delete;
35 ScopedJanitor &operator=(ScopedJanitor const &rhs) = delete;
36 ScopedJanitor &operator=(ScopedJanitor &&rhs) = delete;
37
38protected:
39 bool _enabled;
40 Callable _c;
41};
42
43template <typename Callable> ScopedJanitor<Callable> MakeJanitor(Callable &&c) {
44 return ScopedJanitor<Callable>(std::forward<Callable>(c));
45}
46} // namespace Utils
47} // namespace ds2
Definition ScopedJanitor.h:21