SPH
Finally.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "common/Assert.h"
9 #include "common/Traits.h"
10 
12 
13 template <typename TFunctor>
14 class Finally {
15 private:
16  TFunctor functor;
17  bool moved = false;
18 
19 public:
20  Finally(TFunctor&& functor)
21  : functor(std::forward<TFunctor>(functor)) {}
22 
23  Finally(Finally&& other)
24  : functor(std::move(other.functor)) {
25  other.moved = true;
26  }
27 
29  if (!moved) {
30  functor();
31  }
32  }
33 };
34 
36 template <typename TFunctor>
37 Finally<std::decay_t<TFunctor>> finally(TFunctor&& functor) {
38  return Finally<std::decay_t<TFunctor>>(std::forward<TFunctor>(functor));
39 }
40 
41 
Custom assertions.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
#define NAMESPACE_SPH_END
Definition: Object.h:12
Few non-standard type traits.
Finally(TFunctor &&functor)
Definition: Finally.h:20
~Finally()
Definition: Finally.h:28
Finally(Finally &&other)
Definition: Finally.h:23
Overload of std::swap for Sph::Array.
Definition: Array.h:578