]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // Copyright (c) 2016 Klemens D. Morgenstern |
2 | // | |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | #ifndef BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_ | |
7 | #define BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_ | |
8 | ||
9 | #include <boost/winapi/basic_types.hpp> | |
10 | #include <boost/winapi/handles.hpp> | |
11 | #include <boost/winapi/file_management.hpp> | |
12 | #include <string> | |
13 | #include <boost/filesystem/path.hpp> | |
14 | ||
15 | namespace boost { namespace process { namespace detail { namespace windows { | |
16 | ||
17 | struct file_descriptor | |
18 | { | |
19 | enum mode_t | |
20 | { | |
21 | read = 1, | |
22 | write = 2, | |
23 | read_write = 3 | |
24 | }; | |
25 | static ::boost::winapi::DWORD_ desired_access(mode_t mode) | |
26 | { | |
27 | switch(mode) | |
28 | { | |
29 | case read: | |
30 | return ::boost::winapi::GENERIC_READ_; | |
31 | case write: | |
32 | return ::boost::winapi::GENERIC_WRITE_; | |
33 | case read_write: | |
34 | return ::boost::winapi::GENERIC_READ_ | |
35 | | ::boost::winapi::GENERIC_WRITE_; | |
36 | default: | |
37 | return 0u; | |
38 | } | |
39 | } | |
40 | ||
41 | file_descriptor() = default; | |
42 | file_descriptor(const boost::filesystem::path& p, mode_t mode = read_write) | |
43 | : file_descriptor(p.native(), mode) | |
44 | { | |
45 | } | |
46 | ||
47 | file_descriptor(const std::string & path , mode_t mode = read_write) | |
48 | : file_descriptor(path.c_str(), mode) {} | |
49 | file_descriptor(const std::wstring & path, mode_t mode = read_write) | |
50 | : file_descriptor(path.c_str(), mode) {} | |
51 | ||
52 | file_descriptor(const char* path, mode_t mode = read_write) | |
53 | : _handle( | |
54 | ::boost::winapi::create_file( | |
55 | path, | |
56 | desired_access(mode), | |
57 | ::boost::winapi::FILE_SHARE_READ_ | | |
58 | ::boost::winapi::FILE_SHARE_WRITE_, | |
59 | nullptr, | |
60 | ::boost::winapi::OPEN_ALWAYS_, | |
61 | ||
62 | ::boost::winapi::FILE_ATTRIBUTE_NORMAL_, | |
63 | nullptr | |
64 | )) | |
65 | { | |
66 | ||
67 | } | |
68 | file_descriptor(const wchar_t * path, mode_t mode = read_write) | |
69 | : _handle( | |
70 | ::boost::winapi::create_file( | |
71 | path, | |
72 | desired_access(mode), | |
73 | ::boost::winapi::FILE_SHARE_READ_ | | |
74 | ::boost::winapi::FILE_SHARE_WRITE_, | |
75 | nullptr, | |
76 | ::boost::winapi::OPEN_ALWAYS_, | |
77 | ||
78 | ::boost::winapi::FILE_ATTRIBUTE_NORMAL_, | |
79 | nullptr | |
80 | )) | |
81 | { | |
82 | ||
83 | } | |
84 | file_descriptor(const file_descriptor & ) = delete; | |
85 | file_descriptor(file_descriptor && ) = default; | |
86 | ||
87 | file_descriptor& operator=(const file_descriptor & ) = delete; | |
88 | file_descriptor& operator=(file_descriptor && ) = default; | |
89 | ||
90 | ~file_descriptor() | |
91 | { | |
92 | if (_handle != ::boost::winapi::INVALID_HANDLE_VALUE_) | |
93 | ::boost::winapi::CloseHandle(_handle); | |
94 | } | |
95 | ||
96 | ::boost::winapi::HANDLE_ handle() const { return _handle;} | |
97 | ||
98 | private: | |
99 | ::boost::winapi::HANDLE_ _handle = ::boost::winapi::INVALID_HANDLE_VALUE_; | |
100 | }; | |
101 | ||
102 | }}}} | |
103 | ||
104 | #endif /* BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_ */ |