OmniSciDB  04ee39c94c
scope.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #ifndef SHARED_SCOPE_H
18 #define SHARED_SCOPE_H
19 
20 #include <functional>
21 
22 class ScopeGuard {
23  public:
24  template <class Callable>
25  ScopeGuard(Callable&& at_exit) : at_exit_(std::forward<Callable>(at_exit)) {}
26 
27  // make it non-copyable
28  ScopeGuard(const ScopeGuard&) = delete;
29  void operator=(const ScopeGuard&) = delete;
30 
31  ScopeGuard(ScopeGuard&& other) : at_exit_(std::move(other.at_exit_)) {
32  other.at_exit_ = nullptr;
33  }
34 
36  if (at_exit_) {
37  // note that at_exit_ must not throw
38  at_exit_();
39  }
40  }
41 
42  private:
43  std::function<void()> at_exit_;
44 };
45 
46 #endif // SHARED_SCOPE_H
~ScopeGuard()
Definition: scope.h:35
void operator=(const ScopeGuard &)=delete
std::function< void()> at_exit_
Definition: scope.h:43
ScopeGuard(ScopeGuard &&other)
Definition: scope.h:31
ScopeGuard(Callable &&at_exit)
Definition: scope.h:25