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