File indexing completed on 2025-01-18 09:50:08
0001
0002
0003
0004
0005
0006 #ifndef BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_
0007 #define BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_
0008
0009 #include <boost/winapi/basic_types.hpp>
0010 #include <boost/winapi/handles.hpp>
0011 #include <boost/winapi/file_management.hpp>
0012 #include <string>
0013 #include <boost/process/filesystem.hpp>
0014 #include <boost/core/exchange.hpp>
0015
0016 namespace boost { namespace process { namespace detail { namespace windows {
0017
0018 struct file_descriptor
0019 {
0020 enum mode_t
0021 {
0022 read = 1,
0023 write = 2,
0024 read_write = 3
0025 };
0026 static ::boost::winapi::DWORD_ desired_access(mode_t mode)
0027 {
0028 switch(mode)
0029 {
0030 case read:
0031 return ::boost::winapi::GENERIC_READ_;
0032 case write:
0033 return ::boost::winapi::GENERIC_WRITE_;
0034 case read_write:
0035 return ::boost::winapi::GENERIC_READ_
0036 | ::boost::winapi::GENERIC_WRITE_;
0037 default:
0038 return 0u;
0039 }
0040 }
0041
0042 file_descriptor() = default;
0043 file_descriptor(const boost::process::filesystem::path& p, mode_t mode = read_write)
0044 : file_descriptor(p.native(), mode)
0045 {
0046 }
0047
0048 file_descriptor(const std::string & path , mode_t mode = read_write)
0049 #if defined(BOOST_NO_ANSI_APIS)
0050 : file_descriptor(::boost::process::detail::convert(path), mode)
0051 #else
0052 : file_descriptor(path.c_str(), mode)
0053 #endif
0054 {}
0055 file_descriptor(const std::wstring & path, mode_t mode = read_write)
0056 : file_descriptor(path.c_str(), mode) {}
0057
0058 file_descriptor(const char* path, mode_t mode = read_write)
0059 #if defined(BOOST_NO_ANSI_APIS)
0060 : file_descriptor(std::string(path), mode)
0061 #else
0062 : _handle(
0063 ::boost::winapi::create_file(
0064 path,
0065 desired_access(mode),
0066 ::boost::winapi::FILE_SHARE_READ_ |
0067 ::boost::winapi::FILE_SHARE_WRITE_,
0068 nullptr,
0069 ::boost::winapi::OPEN_ALWAYS_,
0070
0071 ::boost::winapi::FILE_ATTRIBUTE_NORMAL_,
0072 nullptr
0073 ))
0074 #endif
0075 {
0076 }
0077 file_descriptor(const wchar_t * path, mode_t mode = read_write)
0078 : _handle(
0079 ::boost::winapi::create_file(
0080 path,
0081 desired_access(mode),
0082 ::boost::winapi::FILE_SHARE_READ_ |
0083 ::boost::winapi::FILE_SHARE_WRITE_,
0084 nullptr,
0085 ::boost::winapi::OPEN_ALWAYS_,
0086
0087 ::boost::winapi::FILE_ATTRIBUTE_NORMAL_,
0088 nullptr
0089 ))
0090 {
0091
0092 }
0093 file_descriptor(const file_descriptor & ) = delete;
0094 file_descriptor(file_descriptor &&other)
0095 : _handle( boost::exchange(other._handle, ::boost::winapi::INVALID_HANDLE_VALUE_) )
0096 {
0097 }
0098
0099 file_descriptor& operator=(const file_descriptor & ) = delete;
0100 file_descriptor& operator=(file_descriptor &&other)
0101 {
0102 if (_handle != ::boost::winapi::INVALID_HANDLE_VALUE_)
0103 ::boost::winapi::CloseHandle(_handle);
0104 _handle = boost::exchange(other._handle, ::boost::winapi::INVALID_HANDLE_VALUE_);
0105 return *this;
0106 }
0107
0108 ~file_descriptor()
0109 {
0110 if (_handle != ::boost::winapi::INVALID_HANDLE_VALUE_)
0111 ::boost::winapi::CloseHandle(_handle);
0112 }
0113
0114 ::boost::winapi::HANDLE_ handle() const { return _handle;}
0115
0116 private:
0117 ::boost::winapi::HANDLE_ _handle = ::boost::winapi::INVALID_HANDLE_VALUE_;
0118 };
0119
0120 }}}}
0121
0122 #endif