ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
unique_handle.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifdef _WIN32
4# include "windows_api.hpp"
5
7{
8
9class unique_handle
10{
11public:
12 unique_handle() noexcept = default;
13 explicit unique_handle(HANDLE handle) noexcept : handle_(handle) {}
14
15 unique_handle(unique_handle const&) = delete;
16 auto operator=(unique_handle const&) -> unique_handle& = delete;
17
18 unique_handle(unique_handle&& other) noexcept : handle_(other.release()) {}
19
20 auto
21 operator=(unique_handle&& other) noexcept -> unique_handle&
22 {
23 if (this != &other)
24 reset(other.release());
25 return *this;
26 }
27
28 ~unique_handle()
29 {
30 reset();
31 }
32
33 [[nodiscard]] auto
34 get() const noexcept -> HANDLE
35 {
36 return handle_;
37 }
38
39 [[nodiscard]] explicit
40 operator bool() const noexcept
41 {
42 return handle_ != nullptr && handle_ != INVALID_HANDLE_VALUE;
43 }
44
45 [[nodiscard]] auto
46 release() noexcept -> HANDLE
47 {
48 auto* value = handle_;
49 handle_ = nullptr;
50 return value;
51 }
52
53 void
54 reset(HANDLE handle = nullptr) noexcept
55 {
56 if (handle_ == handle)
57 return;
58 if (*this)
59 CloseHandle(handle_);
60 handle_ = handle;
61 }
62
63private:
64 HANDLE handle_{ nullptr };
65};
66
67} // namespace threadschedule::detail
68#endif
Aggregates multiple thread_registry_backend instances into a single queryable view.
@ other
Standard round-robin time-sharing.