Overte C++ Documentation
Finally.h
1 //
2 // Created by Bradley Austin Davis on 2015/09/01
3 // Copyright 2013-2105 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 
9 // Simulates a java finally block by executing a lambda when an instance leaves
10 // scope
11 
12 #include <functional>
13 
14 #pragma once
15 #ifndef hifi_Finally_h
16 #define hifi_Finally_h
17 
18 class Finally {
19 public:
20  template <typename F>
21  Finally(F f) : _f(f) {}
22  ~Finally() { _f(); }
23  void trigger() {
24  _f();
25  _f = [] {};
26  }
27 private:
28  std::function<void()> _f;
29 };
30 
31 #endif