Gorgon Game Engine
ScopeGuard.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 
5 namespace Gorgon {namespace Utils {
6 
11  class ScopeGuard {
12  public:
14  template<class F_>
15  ScopeGuard(F_ &&fn) : fn(std::forward<F_>(fn)) {}
16 
18  ScopeGuard() = default;
19 
21  ScopeGuard(ScopeGuard &&other) : fn(std::move(other.fn)) {
22  }
23 
25  ScopeGuard(const ScopeGuard&) = delete;
26 
29  fn = std::move(other.fn);
30 
31  return *this;
32  }
33 
35  ScopeGuard &operator = (const ScopeGuard&) = delete;
36 
38  void Disarm() {
39  fn = nullptr;
40  }
41 
43  template<class F_>
44  void Arm(F_ &&value) {
45  fn = std::forward<F_>(value);
46  }
47 
50  if(fn)
51  fn();
52  }
53 
54  private:
55  std::function<void()> fn;
56  };
57 
58 }}
Gorgon::Utils::ScopeGuard::operator=
ScopeGuard & operator=(ScopeGuard &&other)
Move assignment.
Definition: ScopeGuard.h:28
Gorgon::Utils::ScopeGuard::ScopeGuard
ScopeGuard(ScopeGuard &&other)
Move constructor.
Definition: ScopeGuard.h:21
Gorgon::Utils::ScopeGuard::ScopeGuard
ScopeGuard()=default
Construct an empty scope guard.
Gorgon::Utils::ScopeGuard::Disarm
void Disarm()
Disarm this scope guard, after this function it will not fire.
Definition: ScopeGuard.h:38
Gorgon::Utils::ScopeGuard::~ScopeGuard
~ScopeGuard()
Destructor.
Definition: ScopeGuard.h:49
Gorgon::Utils::ScopeGuard
This class guards a scope and as soon as the object runs out of scope calls the given function.
Definition: ScopeGuard.h:11
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Utils::ScopeGuard::ScopeGuard
ScopeGuard(F_ &&fn)
Construct a scope guard with the given function.
Definition: ScopeGuard.h:15
Gorgon::Utils::ScopeGuard::Arm
void Arm(F_ &&value)
Arms the scope guard with the given function.
Definition: ScopeGuard.h:44
Gorgon::Utils::ScopeGuard::ScopeGuard
ScopeGuard(const ScopeGuard &)=delete
No copying.