summaryrefslogtreecommitdiffstats
path: root/src/common/windows/util.hpp
blob: 939f6bb45f0b1fa19898786e261e2a18b6e9293d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once

#include <utility>
#include <type_traits>

template <typename Fn>
struct on_scope_exit {
    static_assert(std::is_nothrow_move_constructible<Fn>::value,
        "Fn must be nothrow move constructible");

    explicit on_scope_exit(Fn&& fn) noexcept
        : _fn(std::move(fn)) {};
    on_scope_exit(const on_scope_exit&) = delete;
    on_scope_exit& operator=(const on_scope_exit&) = delete;
    ~on_scope_exit() noexcept {
        this->_fn();
    }

  private:
    Fn _fn;
};