Honeycomb  0.1
Component-Model Framework
ScopeGuard.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/Core/Core.h"
5 
6 namespace honey
7 {
8 
10 template<class F>
12 {
13 public:
14  ScopeGuard_(F&& func) : _func(forward<F>(func)), _engaged(true) {}
15  ScopeGuard_(ScopeGuard_&& rhs) noexcept : _func(move(rhs._func)), _engaged(move(rhs._engaged)) { rhs.release(); }
16  ~ScopeGuard_() { if (_engaged) _func(); }
17 
19  void release() { _engaged = false; }
20 private:
21  F _func;
22  bool _engaged;
23 };
24 
26 
27 template<class F>
28 ScopeGuard_<F> ScopeGuard(F&& func) { return ScopeGuard_<F>(forward<F>(func)); }
29 
30 }
ScopeGuard_< F > ScopeGuard(F &&func)
Create a scope guard of deduced type. Call with lambda: auto guard = ScopeGuard([] {...
Definition: ScopeGuard.h:28
Inherit to declare that class is not copyable.
Definition: Meta.h:286
ScopeGuard_(F &&func)
Definition: ScopeGuard.h:14
~ScopeGuard_()
Definition: ScopeGuard.h:16
void release()
Disengage the guard so the function isn't run at scope exit.
Definition: ScopeGuard.h:19
ScopeGuard_(ScopeGuard_ &&rhs) noexcept
Definition: ScopeGuard.h:15
Run a function at scope exit. See ScopeGuard() to create.
Definition: ScopeGuard.h:11
Global Honeycomb namespace.