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 <cstdint>
  15. #include <type_traits>
  16. namespace boost {
  17. namespace beast {
  18. /*
  19. file_mode acesss sharing seeking file std mode
  20. --------------------------------------------------------------------------------------
  21. read read-only shared random must exist "rb"
  22. scan read-only shared sequential must exist "rbS"
  23. write read/write exclusive random create/truncate "wb+"
  24. write_new read/write exclusive random must not exist "wbx"
  25. write_existing read/write exclusive random must exist "rb+"
  26. append write-only exclusive sequential create/truncate "ab"
  27. append_existing write-only exclusive sequential must exist "ab"
  28. */
  29. /** File open modes
  30. These modes are used when opening files using
  31. instances of the <em>File</em> concept.
  32. @see file_stdio
  33. */
  34. enum class file_mode
  35. {
  36. /// Random read-only access to an existing file
  37. read,
  38. /// Sequential read-only access to an existing file
  39. scan,
  40. /** Random reading and writing to a new or truncated file
  41. This mode permits random-access reading and writing
  42. for the specified file. If the file does not exist
  43. prior to the function call, it is created with an
  44. initial size of zero bytes. Otherwise if the file
  45. already exists, the size is truncated to zero bytes.
  46. */
  47. write,
  48. /** Random reading and writing to a new file only
  49. This mode permits random-access reading and writing
  50. for the specified file. The file will be created with
  51. an initial size of zero bytes. If the file already exists
  52. prior to the function call, an error is returned and
  53. no file is opened.
  54. */
  55. write_new,
  56. /** Random write-only access to existing file
  57. If the file does not exist, an error is generated.
  58. */
  59. write_existing,
  60. /** Appending to a new or existing file
  61. The current file position shall be set to the end of
  62. the file prior to each write.
  63. @li If the file does not exist, it is created.
  64. @li If the file exists, the new data gets appended.
  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