file.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Copyright (c) 2024 Klemens Morgenstern (klemens.morgenstern@gmx.net)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_COBALT_IO_FILE_HPP
  8. #define BOOST_COBALT_IO_FILE_HPP
  9. #include <boost/asio/basic_file.hpp>
  10. #include <boost/cobalt/io/detail/config.hpp>
  11. #include <boost/cobalt/config.hpp>
  12. #include <boost/system/result.hpp>
  13. #if !defined(BOOST_ASIO_HAS_FILE)
  14. #include <fcntl.h>
  15. #include <boost/asio/posix/basic_stream_descriptor.hpp>
  16. #endif
  17. namespace boost::cobalt::io
  18. {
  19. struct BOOST_SYMBOL_VISIBLE file
  20. #if defined(BOOST_ASIO_HAS_FILE)
  21. : asio::file_base
  22. #endif
  23. {
  24. #if !defined(BOOST_ASIO_HAS_FILE)
  25. enum flags
  26. {
  27. read_only = O_RDONLY,
  28. write_only = O_WRONLY,
  29. read_write = O_RDWR,
  30. append = O_APPEND,
  31. create = O_CREAT,
  32. exclusive = O_EXCL,
  33. truncate = O_TRUNC,
  34. sync_all_on_write = O_SYNC
  35. };
  36. // Implement bitmask operations as shown in C++ Std [lib.bitmask.types].
  37. friend flags operator&(flags x, flags y)
  38. {
  39. return static_cast<flags>(
  40. static_cast<unsigned int>(x) & static_cast<unsigned int>(y));
  41. }
  42. friend flags operator|(flags x, flags y)
  43. {
  44. return static_cast<flags>(
  45. static_cast<unsigned int>(x) | static_cast<unsigned int>(y));
  46. }
  47. friend flags operator^(flags x, flags y)
  48. {
  49. return static_cast<flags>(
  50. static_cast<unsigned int>(x) ^ static_cast<unsigned int>(y));
  51. }
  52. friend flags operator~(flags x)
  53. {
  54. return static_cast<flags>(~static_cast<unsigned int>(x));
  55. }
  56. friend flags& operator&=(flags& x, flags y)
  57. {
  58. x = x & y;
  59. return x;
  60. }
  61. friend flags& operator|=(flags& x, flags y)
  62. {
  63. x = x | y;
  64. return x;
  65. }
  66. friend flags& operator^=(flags& x, flags y)
  67. {
  68. x = x ^ y;
  69. return x;
  70. }
  71. /// Basis for seeking in a file.
  72. enum seek_basis
  73. {
  74. seek_set = SEEK_SET,
  75. seek_cur = SEEK_CUR,
  76. seek_end = SEEK_END
  77. };
  78. #endif
  79. #if !defined(BOOST_ASIO_HAS_FILE)
  80. using native_handle_type = int;
  81. #else
  82. using native_handle_type = asio::basic_file<executor>::native_handle_type;
  83. #endif
  84. BOOST_COBALT_IO_DECL system::result<void> assign(const native_handle_type & native_file);
  85. BOOST_COBALT_IO_DECL system::result<void> cancel();
  86. BOOST_COBALT_IO_DECL executor get_executor();
  87. BOOST_COBALT_IO_DECL bool is_open() const;
  88. BOOST_COBALT_IO_DECL system::result<void> close();
  89. BOOST_COBALT_IO_DECL native_handle_type native_handle();
  90. BOOST_COBALT_IO_DECL system::result<void> open(const char * path, flags open_flags);
  91. BOOST_COBALT_IO_DECL system::result<void> open(const std::string & path, flags open_flags);
  92. BOOST_COBALT_IO_DECL system::result<native_handle_type> release();
  93. BOOST_COBALT_IO_DECL system::result<void> resize(std::uint64_t n);
  94. BOOST_COBALT_IO_DECL system::result<std::uint64_t> size() const;
  95. BOOST_COBALT_IO_DECL system::result<void> sync_all();
  96. BOOST_COBALT_IO_DECL system::result<void> sync_data();
  97. #if defined(BOOST_ASIO_HAS_FILE)
  98. file(asio::basic_file<executor> & file) : file_(file) {}
  99. private:
  100. asio::basic_file<executor> & file_;
  101. #else
  102. explicit file(executor exec) : file_(exec) {}
  103. file(executor exec, int fd) : file_(exec, fd) {}
  104. protected:
  105. boost::asio::posix::basic_stream_descriptor<executor> file_;
  106. #endif
  107. };
  108. }
  109. #endif //BOOST_COBALT_IO_FILE_HPP