1 // Copyright (c) 2016 Klemens D. Morgenstern
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)
6 #ifndef BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_
7 #define BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_
9 #include <boost/winapi/basic_types.hpp>
10 #include <boost/winapi/handles.hpp>
11 #include <boost/winapi/file_management.hpp>
13 #include <boost/filesystem/path.hpp>
14 #include <boost/core/exchange.hpp>
16 namespace boost { namespace process { namespace detail { namespace windows {
18 struct file_descriptor
26 static ::boost::winapi::DWORD_ desired_access(mode_t mode)
31 return ::boost::winapi::GENERIC_READ_;
33 return ::boost::winapi::GENERIC_WRITE_;
35 return ::boost::winapi::GENERIC_READ_
36 | ::boost::winapi::GENERIC_WRITE_;
42 file_descriptor() = default;
43 file_descriptor(const boost::filesystem::path& p, mode_t mode = read_write)
44 : file_descriptor(p.native(), mode)
48 file_descriptor(const std::string & path , mode_t mode = read_write)
49 #if defined(BOOST_NO_ANSI_APIS)
50 : file_descriptor(::boost::process::detail::convert(path), mode)
52 : file_descriptor(path.c_str(), mode)
55 file_descriptor(const std::wstring & path, mode_t mode = read_write)
56 : file_descriptor(path.c_str(), mode) {}
58 file_descriptor(const char* path, mode_t mode = read_write)
59 #if defined(BOOST_NO_ANSI_APIS)
60 : file_descriptor(std::string(path), mode)
63 ::boost::winapi::create_file(
66 ::boost::winapi::FILE_SHARE_READ_ |
67 ::boost::winapi::FILE_SHARE_WRITE_,
69 ::boost::winapi::OPEN_ALWAYS_,
71 ::boost::winapi::FILE_ATTRIBUTE_NORMAL_,
77 file_descriptor(const wchar_t * path, mode_t mode = read_write)
79 ::boost::winapi::create_file(
82 ::boost::winapi::FILE_SHARE_READ_ |
83 ::boost::winapi::FILE_SHARE_WRITE_,
85 ::boost::winapi::OPEN_ALWAYS_,
87 ::boost::winapi::FILE_ATTRIBUTE_NORMAL_,
93 file_descriptor(const file_descriptor & ) = delete;
94 file_descriptor(file_descriptor &&other)
95 : _handle( boost::exchange(other._handle, ::boost::winapi::INVALID_HANDLE_VALUE_) )
99 file_descriptor& operator=(const file_descriptor & ) = delete;
100 file_descriptor& operator=(file_descriptor &&other)
102 if (_handle != ::boost::winapi::INVALID_HANDLE_VALUE_)
103 ::boost::winapi::CloseHandle(_handle);
104 _handle = boost::exchange(other._handle, ::boost::winapi::INVALID_HANDLE_VALUE_);
110 if (_handle != ::boost::winapi::INVALID_HANDLE_VALUE_)
111 ::boost::winapi::CloseHandle(_handle);
114 ::boost::winapi::HANDLE_ handle() const { return _handle;}
117 ::boost::winapi::HANDLE_ _handle = ::boost::winapi::INVALID_HANDLE_VALUE_;
122 #endif /* BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_ */