Line data Source code
1 0 : // Distributed under the MIT License.
2 : // See LICENSE.txt for details.
3 :
4 : #pragma once
5 :
6 : #include <utility>
7 :
8 : /// An object that calls a functor when it goes out of scope.
9 : ///
10 : /// \snippet Test_CleanupRoutine.cpp cleanup_routine
11 : template <typename F>
12 1 : class CleanupRoutine {
13 : public:
14 0 : CleanupRoutine(F routine) : routine_(std::move(routine)) {}
15 :
16 : // Copying/moving don't make sense, and it's not clear what a
17 : // moved-out-of instance would be expected to do.
18 0 : CleanupRoutine(const CleanupRoutine&) = delete;
19 0 : CleanupRoutine(CleanupRoutine&&) = delete;
20 0 : CleanupRoutine& operator=(const CleanupRoutine&) = delete;
21 0 : CleanupRoutine& operator=(CleanupRoutine&&) = delete;
22 :
23 0 : ~CleanupRoutine() { routine_(); }
24 :
25 : private:
26 0 : F routine_;
27 : };
|