-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.hpp
52 lines (46 loc) · 1.84 KB
/
file.hpp
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
//Copyright (C) 2014-2017, 2019, 2021 I
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include"_windows.hpp"
namespace will{
class handle{
HANDLE h;
public:
handle():h{INVALID_HANDLE_VALUE}{}
explicit handle(HANDLE&& h):h{h}{}
handle(handle&& other):h{other.release()}{}
handle& operator=(handle&& other){h = other.release();return *this;}
expected<void, winapi_last_error> close(){
if(::CloseHandle(h) == 0)
return make_unexpected<winapi_last_error>(_T(__FUNCTION__));
return {};
}
expected<void, winapi_last_error> reset(::HANDLE&& other){
return close().map([&]{h = std::move(other);});
}
void swap(handle& other)noexcept{
std::swap(h, other.h);
}
HANDLE release(){
auto h = this->h;
this->h = INVALID_HANDLE_VALUE;
return h;
}
HANDLE get()const{return h;}
explicit operator bool()const{return h == INVALID_HANDLE_VALUE;}
~handle(){
if(h != INVALID_HANDLE_VALUE)
close();
}
};
inline expected<handle, winapi_last_error> create_file(LPCTSTR filename, DWORD desired_access, DWORD share_mode, LPSECURITY_ATTRIBUTES security_attributes, DWORD creation_disposition, DWORD flags_and_attributes, HANDLE template_file = nullptr){
auto h = ::CreateFile(filename, desired_access, share_mode, security_attributes, creation_disposition, flags_and_attributes, template_file);
if(h == INVALID_HANDLE_VALUE)
return make_unexpected<winapi_last_error>(_T(__FUNCTION__));
return handle{std::move(h)};
}
inline expected<handle, winapi_last_error> create_file(LPCTSTR filename, DWORD desired_access, DWORD share_mode, DWORD creation_disposition, DWORD flags_and_attributes, HANDLE template_file = nullptr){
return create_file(filename, desired_access, share_mode, nullptr, creation_disposition, flags_and_attributes, template_file);
}
}