file_base.hpp 3.9 KB

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