file_descriptor.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_
  6. #define BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_
  7. #include <boost/winapi/basic_types.hpp>
  8. #include <boost/winapi/handles.hpp>
  9. #include <boost/winapi/file_management.hpp>
  10. #include <string>
  11. #include <boost/process/v1/filesystem.hpp>
  12. #include <boost/core/exchange.hpp>
  13. namespace boost { namespace process { BOOST_PROCESS_V1_INLINE namespace v1 { namespace detail { namespace windows {
  14. struct file_descriptor
  15. {
  16. enum mode_t
  17. {
  18. read = 1,
  19. write = 2,
  20. read_write = 3
  21. };
  22. static ::boost::winapi::DWORD_ desired_access(mode_t mode)
  23. {
  24. switch(mode)
  25. {
  26. case read:
  27. return ::boost::winapi::GENERIC_READ_;
  28. case write:
  29. return ::boost::winapi::GENERIC_WRITE_;
  30. case read_write:
  31. return ::boost::winapi::GENERIC_READ_
  32. | ::boost::winapi::GENERIC_WRITE_;
  33. default:
  34. return 0u;
  35. }
  36. }
  37. file_descriptor() = default;
  38. file_descriptor(const boost::process::v1::filesystem::path& p, mode_t mode = read_write)
  39. : file_descriptor(p.native(), mode)
  40. {
  41. }
  42. file_descriptor(const std::string & path , mode_t mode = read_write)
  43. #if defined(BOOST_NO_ANSI_APIS)
  44. : file_descriptor(::boost::process::v1::detail::convert(path), mode)
  45. #else
  46. : file_descriptor(path.c_str(), mode)
  47. #endif
  48. {}
  49. file_descriptor(const std::wstring & path, mode_t mode = read_write)
  50. : file_descriptor(path.c_str(), mode) {}
  51. file_descriptor(const char* path, mode_t mode = read_write)
  52. #if defined(BOOST_NO_ANSI_APIS)
  53. : file_descriptor(std::string(path), mode)
  54. #else
  55. : _handle(
  56. ::boost::winapi::create_file(
  57. path,
  58. desired_access(mode),
  59. ::boost::winapi::FILE_SHARE_READ_ |
  60. ::boost::winapi::FILE_SHARE_WRITE_,
  61. nullptr,
  62. ::boost::winapi::OPEN_ALWAYS_,
  63. ::boost::winapi::FILE_ATTRIBUTE_NORMAL_,
  64. nullptr
  65. ))
  66. #endif
  67. {
  68. }
  69. file_descriptor(const wchar_t * path, mode_t mode = read_write)
  70. : _handle(
  71. ::boost::winapi::create_file(
  72. path,
  73. desired_access(mode),
  74. ::boost::winapi::FILE_SHARE_READ_ |
  75. ::boost::winapi::FILE_SHARE_WRITE_,
  76. nullptr,
  77. ::boost::winapi::OPEN_ALWAYS_,
  78. ::boost::winapi::FILE_ATTRIBUTE_NORMAL_,
  79. nullptr
  80. ))
  81. {
  82. }
  83. file_descriptor(const file_descriptor & ) = delete;
  84. file_descriptor(file_descriptor &&other)
  85. : _handle( boost::exchange(other._handle, ::boost::winapi::INVALID_HANDLE_VALUE_) )
  86. {
  87. }
  88. file_descriptor& operator=(const file_descriptor & ) = delete;
  89. file_descriptor& operator=(file_descriptor &&other)
  90. {
  91. if (_handle != ::boost::winapi::INVALID_HANDLE_VALUE_)
  92. ::boost::winapi::CloseHandle(_handle);
  93. _handle = boost::exchange(other._handle, ::boost::winapi::INVALID_HANDLE_VALUE_);
  94. return *this;
  95. }
  96. ~file_descriptor()
  97. {
  98. if (_handle != ::boost::winapi::INVALID_HANDLE_VALUE_)
  99. ::boost::winapi::CloseHandle(_handle);
  100. }
  101. ::boost::winapi::HANDLE_ handle() const { return _handle;}
  102. private:
  103. ::boost::winapi::HANDLE_ _handle = ::boost::winapi::INVALID_HANDLE_VALUE_;
  104. };
  105. }}}}}
  106. #endif /* BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_ */