file_base.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // file_base.hpp
  3. // ~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_FILE_BASE_HPP
  11. #define BOOST_ASIO_FILE_BASE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_FILE) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #if !defined(BOOST_ASIO_WINDOWS)
  19. # include <fcntl.h>
  20. #endif // !defined(BOOST_ASIO_WINDOWS)
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. /// The file_base class is used as a base for the basic_stream_file and
  25. /// basic_random_access_file class templates so that we have a common place to
  26. /// define flags.
  27. class file_base
  28. {
  29. public:
  30. #if defined(GENERATING_DOCUMENTATION)
  31. /// A bitmask type (C++ Std [lib.bitmask.types]).
  32. typedef unspecified flags;
  33. /// Open the file for reading.
  34. static const flags read_only = implementation_defined;
  35. /// Open the file for writing.
  36. static const flags write_only = implementation_defined;
  37. /// Open the file for reading and writing.
  38. static const flags read_write = implementation_defined;
  39. /// Open the file in append mode.
  40. static const flags append = implementation_defined;
  41. /// Create the file if it does not exist.
  42. static const flags create = implementation_defined;
  43. /// Ensure a new file is created. Must be combined with @c create.
  44. static const flags exclusive = implementation_defined;
  45. /// Open the file with any existing contents truncated.
  46. static const flags truncate = implementation_defined;
  47. /// Open the file so that write operations automatically synchronise the file
  48. /// data and metadata to disk.
  49. static const flags sync_all_on_write = implementation_defined;
  50. #else
  51. enum flags
  52. {
  53. #if defined(BOOST_ASIO_WINDOWS)
  54. read_only = 1,
  55. write_only = 2,
  56. read_write = 4,
  57. append = 8,
  58. create = 16,
  59. exclusive = 32,
  60. truncate = 64,
  61. sync_all_on_write = 128
  62. #else // defined(BOOST_ASIO_WINDOWS)
  63. read_only = O_RDONLY,
  64. write_only = O_WRONLY,
  65. read_write = O_RDWR,
  66. append = O_APPEND,
  67. create = O_CREAT,
  68. exclusive = O_EXCL,
  69. truncate = O_TRUNC,
  70. sync_all_on_write = O_SYNC
  71. #endif // defined(BOOST_ASIO_WINDOWS)
  72. };
  73. // Implement bitmask operations as shown in C++ Std [lib.bitmask.types].
  74. friend flags operator&(flags x, flags y)
  75. {
  76. return static_cast<flags>(
  77. static_cast<unsigned int>(x) & static_cast<unsigned int>(y));
  78. }
  79. friend flags operator|(flags x, flags y)
  80. {
  81. return static_cast<flags>(
  82. static_cast<unsigned int>(x) | static_cast<unsigned int>(y));
  83. }
  84. friend flags operator^(flags x, flags y)
  85. {
  86. return static_cast<flags>(
  87. static_cast<unsigned int>(x) ^ static_cast<unsigned int>(y));
  88. }
  89. friend flags operator~(flags x)
  90. {
  91. return static_cast<flags>(~static_cast<unsigned int>(x));
  92. }
  93. friend flags& operator&=(flags& x, flags y)
  94. {
  95. x = x & y;
  96. return x;
  97. }
  98. friend flags& operator|=(flags& x, flags y)
  99. {
  100. x = x | y;
  101. return x;
  102. }
  103. friend flags& operator^=(flags& x, flags y)
  104. {
  105. x = x ^ y;
  106. return x;
  107. }
  108. #endif
  109. /// Basis for seeking in a file.
  110. enum seek_basis
  111. {
  112. #if defined(GENERATING_DOCUMENTATION)
  113. /// Seek to an absolute position.
  114. seek_set = implementation_defined,
  115. /// Seek to an offset relative to the current file position.
  116. seek_cur = implementation_defined,
  117. /// Seek to an offset relative to the end of the file.
  118. seek_end = implementation_defined
  119. #else
  120. seek_set = SEEK_SET,
  121. seek_cur = SEEK_CUR,
  122. seek_end = SEEK_END
  123. #endif
  124. };
  125. protected:
  126. /// Protected destructor to prevent deletion through this type.
  127. ~file_base()
  128. {
  129. }
  130. };
  131. } // namespace asio
  132. } // namespace boost
  133. #include <boost/asio/detail/pop_options.hpp>
  134. #endif // defined(BOOST_ASIO_HAS_FILE)
  135. // || defined(GENERATING_DOCUMENTATION)
  136. #endif // BOOST_ASIO_FILE_BASE_HPP