aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/handle.hpp
blob: 5f83f2442c933f2b55f88acd8f6860733bca93f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once

#include <Windows.h>

#include <cassert>

#include <memory>
#include <utility>

class Handle
{
public:
    Handle() = default;

    explicit Handle(HANDLE raw)
        : impl{raw}
    { }

    Handle(Handle&& other) noexcept
    {
        swap(other);
    }

    Handle& operator=(Handle other) noexcept
    {
        swap(other);
        return *this;
    }

    void swap(Handle& other) noexcept
    {
        using std::swap;
        swap(impl, other.impl);
    }

    operator HANDLE() const
    {
        return impl.get();
    }

private:
    struct Close
    {
        void operator()(HANDLE raw) const
        {
            if (raw == NULL || raw == INVALID_HANDLE_VALUE)
                return;
            const auto ret = CloseHandle(raw);
            assert(ret);
        }
    };

    std::unique_ptr<void, Close> impl;

    Handle(const Handle&) = delete;
};

namespace std
{
    void swap(Handle& a, Handle& b) noexcept
    {
        a.swap(b);
    }
}