file_descriptor.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/filesystem/path.hpp>
  12. namespace boost { namespace process { namespace detail { namespace windows {
  13. struct file_descriptor
  14. {
  15. enum mode_t
  16. {
  17. read = 1,
  18. write = 2,
  19. read_write = 3
  20. };
  21. static ::boost::winapi::DWORD_ desired_access(mode_t mode)
  22. {
  23. switch(mode)
  24. {
  25. case read:
  26. return ::boost::winapi::GENERIC_READ_;
  27. case write:
  28. return ::boost::winapi::GENERIC_WRITE_;
  29. case read_write:
  30. return ::boost::winapi::GENERIC_READ_
  31. | ::boost::winapi::GENERIC_WRITE_;
  32. default:
  33. return 0u;
  34. }
  35. }
  36. file_descriptor() = default;
  37. file_descriptor(const boost::filesystem::path& p, mode_t mode = read_write)
  38. : file_descriptor(p.native(), mode)
  39. {
  40. }
  41. file_descriptor(const std::string & path , mode_t mode = read_write)
  42. #if defined(BOOST_NO_ANSI_APIS)
  43. : file_descriptor(::boost::process::detail::convert(path), mode)
  44. #else
  45. : file_descriptor(path.c_str(), mode)
  46. #endif
  47. {}
  48. file_descriptor(const std::wstring & path, mode_t mode = read_write)
  49. : file_descriptor(path.c_str(), mode) {}
  50. file_descriptor(const char* path, mode_t mode = read_write)
  51. #if defined(BOOST_NO_ANSI_APIS)
  52. : file_descriptor(std::string(path), mode)
  53. #else
  54. : _handle(
  55. ::boost::winapi::create_file(
  56. path,
  57. desired_access(mode),
  58. ::boost::winapi::FILE_SHARE_READ_ |
  59. ::boost::winapi::FILE_SHARE_WRITE_,
  60. nullptr,
  61. ::boost::winapi::OPEN_ALWAYS_,
  62. ::boost::winapi::FILE_ATTRIBUTE_NORMAL_,
  63. nullptr
  64. ))
  65. #endif
  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. ::boost::winapi::FILE_ATTRIBUTE_NORMAL_,
  78. nullptr
  79. ))
  80. {
  81. }
  82. file_descriptor(const file_descriptor & ) = delete;
  83. file_descriptor(file_descriptor && ) = default;
  84. file_descriptor& operator=(const file_descriptor & ) = delete;
  85. file_descriptor& operator=(file_descriptor && ) = default;
  86. ~file_descriptor()
  87. {
  88. if (_handle != ::boost::winapi::INVALID_HANDLE_VALUE_)
  89. ::boost::winapi::CloseHandle(_handle);
  90. }
  91. ::boost::winapi::HANDLE_ handle() const { return _handle;}
  92. private:
  93. ::boost::winapi::HANDLE_ _handle = ::boost::winapi::INVALID_HANDLE_VALUE_;
  94. };
  95. }}}}
  96. #endif /* BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_ */