file_base.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // Copyright (c) 2015-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  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. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_CORE_FILE_BASE_HPP
  10. #define BOOST_BEAST_CORE_FILE_BASE_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/error.hpp>
  13. #include <boost/type_traits/make_void.hpp>
  14. #include <type_traits>
  15. namespace boost {
  16. namespace beast {
  17. /*
  18. file_mode acesss sharing seeking file std mode
  19. --------------------------------------------------------------------------------------
  20. read read-only shared random must exist "rb"
  21. scan read-only shared sequential must exist "rbS"
  22. write read/write exclusive random create/truncate "wb+"
  23. write_new read/write exclusive random must not exist "wbx"
  24. write_existing read/write exclusive random must exist "rb+"
  25. append write-only exclusive sequential create/truncate "ab"
  26. append_existing write-only exclusive sequential must exist "ab"
  27. */
  28. /** File open modes
  29. These modes are used when opening files using
  30. instances of the <em>File</em> concept.
  31. @see file_stdio
  32. */
  33. enum class file_mode
  34. {
  35. /// Random read-only access to an existing file
  36. read,
  37. /// Sequential read-only access to an existing file
  38. scan,
  39. /** Random reading and writing to a new or truncated file
  40. This mode permits random-access reading and writing
  41. for the specified file. If the file does not exist
  42. prior to the function call, it is created with an
  43. initial size of zero bytes. Otherwise if the file
  44. already exists, the size is truncated to zero bytes.
  45. */
  46. write,
  47. /** Random reading and writing to a new file only
  48. This mode permits random-access reading and writing
  49. for the specified file. The file will be created with
  50. an initial size of zero bytes. If the file already exists
  51. prior to the function call, an error is returned and
  52. no file is opened.
  53. */
  54. write_new,
  55. /** Random write-only access to existing file
  56. If the file does not exist, an error is generated.
  57. */
  58. write_existing,
  59. /** Appending to a new or truncated file
  60. The current file position shall be set to the end of
  61. the file prior to each write.
  62. @li If the file does not exist, it is created.
  63. @li If the file exists, it is truncated to
  64. zero size upon opening.
  65. */
  66. append,
  67. /** Appending to an existing file
  68. The current file position shall be set to the end of
  69. the file prior to each write.
  70. If the file does not exist, an error is generated.
  71. */
  72. append_existing
  73. };
  74. /** Determine if `T` meets the requirements of <em>File</em>.
  75. Metafunctions are used to perform compile time checking of template
  76. types. This type will be `std::true_type` if `T` meets the requirements,
  77. else the type will be `std::false_type`.
  78. @par Example
  79. Use with `static_assert`:
  80. @code
  81. template<class File>
  82. void f(File& file)
  83. {
  84. static_assert(is_file<File>::value,
  85. "File type requirements not met");
  86. ...
  87. @endcode
  88. Use with `std::enable_if` (SFINAE):
  89. @code
  90. template<class File>
  91. typename std::enable_if<is_file<File>::value>::type
  92. f(File& file);
  93. @endcode
  94. */
  95. #if BOOST_BEAST_DOXYGEN
  96. template<class T>
  97. struct is_file : std::integral_constant<bool, ...>{};
  98. #else
  99. template<class T, class = void>
  100. struct is_file : std::false_type {};
  101. template<class T>
  102. struct is_file<T, boost::void_t<decltype(
  103. std::declval<bool&>() = std::declval<T const&>().is_open(),
  104. std::declval<T&>().close(std::declval<error_code&>()),
  105. std::declval<T&>().open(
  106. std::declval<char const*>(),
  107. std::declval<file_mode>(),
  108. std::declval<error_code&>()),
  109. std::declval<std::uint64_t&>() = std::declval<T&>().size(
  110. std::declval<error_code&>()),
  111. std::declval<std::uint64_t&>() = std::declval<T&>().pos(
  112. std::declval<error_code&>()),
  113. std::declval<T&>().seek(
  114. std::declval<std::uint64_t>(),
  115. std::declval<error_code&>()),
  116. std::declval<std::size_t&>() = std::declval<T&>().read(
  117. std::declval<void*>(),
  118. std::declval<std::size_t>(),
  119. std::declval<error_code&>()),
  120. std::declval<std::size_t&>() = std::declval<T&>().write(
  121. std::declval<void const*>(),
  122. std::declval<std::size_t>(),
  123. std::declval<error_code&>())
  124. )>> : std::integral_constant<bool,
  125. std::is_default_constructible<T>::value &&
  126. std::is_destructible<T>::value
  127. > {};
  128. #endif
  129. } // beast
  130. } // boost
  131. #endif