Skip to content

Commit d8d3efe

Browse files
committed
file class has throwing overloads
1 parent ed2c1df commit d8d3efe

11 files changed

Lines changed: 268 additions & 39 deletions

File tree

include/boost/http_proto/file.hpp

Lines changed: 195 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define BOOST_HTTP_PROTO_FILE_HPP
1313

1414
#include <boost/http_proto/detail/config.hpp>
15+
#include <boost/http_proto/detail/except.hpp>
1516
#include <boost/http_proto/detail/file_posix.hpp>
1617
#include <boost/http_proto/detail/file_stdio.hpp>
1718
#include <boost/http_proto/detail/file_win32.hpp>
@@ -27,19 +28,20 @@ namespace http_proto {
2728
file_source to enable streaming HTTP message
2829
bodies to and from files.
2930
30-
@par Example
31+
@par Example 1
3132
@code
32-
file f;
33-
system::error_code ec;
34-
35-
f.open("example.zip", file_mode::write_new, ec);
36-
if(ec.failed())
37-
throw system::system_error(ec);
33+
file f("example.zip", file_mode::scan);
34+
response.set_payload_size(f.size());
35+
serializer.start<file_source>(response, std::move(f));
36+
@endcode
3837
39-
parser.set_body<file_sink>(std::move(f));
38+
@par Example 2
39+
@code
40+
parser.set_body<file_sink>("example.zip", file_mode::write_new);
4041
@endcode
4142
4243
@see
44+
@ref file_mode,
4345
@ref file_sink,
4446
@ref file_source.
4547
*/
@@ -63,9 +65,34 @@ class file
6365
using native_handle_type = impl_type::native_handle_type;
6466

6567
/** Constructor.
68+
69+
There is no open file initially.
6670
*/
6771
file() = default;
6872

73+
/** Constructor.
74+
75+
Open a file at the given path with the specified mode.
76+
77+
@par Exception Safety
78+
Exception thrown if operation fails.
79+
80+
@throw system_error
81+
Operation fails.
82+
83+
@param path The UTF-8 encoded path to the file.
84+
85+
@param mode The file mode to use.
86+
87+
@see
88+
@ref file_mode,
89+
@ref open.
90+
*/
91+
file(char const* path, file_mode mode)
92+
{
93+
open(path, mode);
94+
}
95+
6996
/** Constructor.
7097
7198
The moved-from object behaves as if default-constructed.
@@ -80,6 +107,12 @@ class file
80107
operator=(
81108
file&& other) noexcept = default;
82109

110+
/** Destructor
111+
112+
If the file is open it is first closed.
113+
*/
114+
~file() = default;
115+
83116
/** Returns the native handle associated with the file.
84117
*/
85118
native_handle_type
@@ -110,6 +143,9 @@ class file
110143

111144
/** Close the file if open.
112145
146+
Note that, The descriptor is closed even if the function
147+
reports an error.
148+
113149
@param ec Set to the error, if any occurred.
114150
*/
115151
void
@@ -118,20 +154,67 @@ class file
118154
impl_.close(ec);
119155
}
120156

157+
/** Close the file if open.
158+
159+
Note that, The descriptor is closed even if the function
160+
reports an error.
161+
162+
@par Exception Safety
163+
Exception thrown if operation fails.
164+
165+
@throw system_error
166+
Operation fails.
167+
*/
168+
void
169+
close()
170+
{
171+
system::error_code ec;
172+
impl_.close(ec);
173+
if(ec.failed())
174+
detail::throw_system_error(ec);
175+
}
176+
121177
/** Open a file at the given path with the specified mode.
122178
123179
@param path The UTF-8 encoded path to the file.
124180
125181
@param mode The file mode to use.
126182
127183
@param ec Set to the error, if any occurred.
184+
185+
@see
186+
@ref file_mode.
128187
*/
129188
void
130189
open(char const* path, file_mode mode, system::error_code& ec)
131190
{
132191
impl_.open(path, mode, ec);
133192
}
134193

194+
/** Open a file at the given path with the specified mode.
195+
196+
@param path The UTF-8 encoded path to the file.
197+
198+
@param mode The file mode to use.
199+
200+
@par Exception Safety
201+
Exception thrown if operation fails.
202+
203+
@throw system_error
204+
Operation fails.
205+
206+
@see
207+
@ref file_mode.
208+
*/
209+
void
210+
open(char const* path, file_mode mode)
211+
{
212+
system::error_code ec;
213+
impl_.open(path, mode, ec);
214+
if(ec.failed())
215+
detail::throw_system_error(ec);
216+
}
217+
135218
/** Return the size of the open file in bytes.
136219
137220
@param ec Set to the error, if any occurred.
@@ -142,6 +225,24 @@ class file
142225
return impl_.size(ec);
143226
}
144227

228+
/** Return the size of the open file in bytes.
229+
230+
@par Exception Safety
231+
Exception thrown if operation fails.
232+
233+
@throw system_error
234+
Operation fails.
235+
*/
236+
std::uint64_t
237+
size() const
238+
{
239+
system::error_code ec;
240+
auto r = impl_.size(ec);
241+
if(ec.failed())
242+
detail::throw_system_error(ec);
243+
return r;
244+
}
245+
145246
/** Return the current position in the file, in bytes from the beginning.
146247
147248
@param ec Set to the error, if any occurred.
@@ -152,6 +253,24 @@ class file
152253
return impl_.pos(ec);
153254
}
154255

256+
/** Return the current position in the file, in bytes from the beginning.
257+
258+
@par Exception Safety
259+
Exception thrown if operation fails.
260+
261+
@throw system_error
262+
Operation fails.
263+
*/
264+
std::uint64_t
265+
pos() const
266+
{
267+
system::error_code ec;
268+
auto r = impl_.pos(ec);
269+
if(ec.failed())
270+
detail::throw_system_error(ec);
271+
return r;
272+
}
273+
155274
/** Set the current position in the file.
156275
157276
@param offset The byte offset from the beginning of the file.
@@ -164,6 +283,25 @@ class file
164283
impl_.seek(offset, ec);
165284
}
166285

286+
/** Set the current position in the file.
287+
288+
@par Exception Safety
289+
Exception thrown if operation fails.
290+
291+
@throw system_error
292+
Operation fails.
293+
294+
@param offset The byte offset from the beginning of the file.
295+
*/
296+
void
297+
seek(std::uint64_t offset)
298+
{
299+
system::error_code ec;
300+
impl_.seek(offset, ec);
301+
if(ec.failed())
302+
detail::throw_system_error(ec);
303+
}
304+
167305
/** Read data from the file.
168306
169307
@return The number of bytes read. Returns
@@ -182,6 +320,31 @@ class file
182320
return impl_.read(buffer, n, ec);
183321
}
184322

323+
/** Read data from the file.
324+
325+
@par Exception Safety
326+
Exception thrown if operation fails.
327+
328+
@throw system_error
329+
Operation fails.
330+
331+
@return The number of bytes read. Returns
332+
0 on end-of-file.
333+
334+
@param buffer The buffer to store the read data.
335+
336+
@param n The number of bytes to read.
337+
*/
338+
std::size_t
339+
read(void* buffer, std::size_t n)
340+
{
341+
system::error_code ec;
342+
auto r = impl_.read(buffer, n, ec);
343+
if(ec.failed())
344+
detail::throw_system_error(ec);
345+
return r;
346+
}
347+
185348
/** Write data to the file.
186349
187350
@return The number of bytes written.
@@ -199,6 +362,30 @@ class file
199362
{
200363
return impl_.write(buffer, n, ec);
201364
}
365+
366+
/** Write data to the file.
367+
368+
@par Exception Safety
369+
Exception thrown if operation fails.
370+
371+
@throw system_error
372+
Operation fails.
373+
374+
@return The number of bytes written.
375+
376+
@param buffer The buffer containing the data to write.
377+
378+
@param n The number of bytes to write.
379+
*/
380+
std::size_t
381+
write(void const* buffer, std::size_t n)
382+
{
383+
system::error_code ec;
384+
auto r = impl_.write(buffer, n, ec);
385+
if(ec.failed())
386+
detail::throw_system_error(ec);
387+
return r;
388+
}
202389
};
203390

204391
} // http_proto

include/boost/http_proto/file_sink.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,11 @@ namespace http_proto {
2727
2828
@par Example
2929
@code
30-
file f;
31-
system::error_code ec;
32-
f.open("example.zip", file_mode::write_new, ec);
33-
if(ec.failed())
34-
throw system::system_error(ec);
35-
parser.set_body<file_sink>(std::move(f));
30+
parser.set_body<file_sink>("example.zip", file_mode::write_new);
3631
@endcode
3732
3833
@see
34+
@ref file_source,
3935
@ref file,
4036
@ref parser,
4137
@ref sink.

include/boost/http_proto/file_source.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@ namespace http_proto {
2727
2828
@par Example
2929
@code
30-
file f;
31-
system::error_code ec;
32-
f.open("example.zip", file_mode::scan, ec);
33-
if(ec.failed())
34-
throw system::system_error(ec);
30+
file f("example.zip", file_mode::scan);
31+
response.set_payload_size(f.size());
3532
serializer.start<file_source>(response, std::move(f));
3633
@endcode
3734
3835
@see
36+
@ref file_sink,
3937
@ref file,
4038
@ref serializer,
4139
@ref source.

include/boost/http_proto/parser.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -462,13 +462,7 @@ class parser
462462
463463
read_header(stream, pr);
464464
465-
http_proto::file file;
466-
system::error_code ec;
467-
file.open("./index.html", file_mode::write_new, ec);
468-
if(ec.failed())
469-
return ec;
470-
471-
pr.set_body<file_body>(std::move(file));
465+
pr.set_body<file_sink>("example.zip", file_mode::write_new);
472466
473467
read(stream, pr);
474468
@endcode
@@ -497,6 +491,8 @@ class parser
497491
@return A reference to the constructed Sink object.
498492
499493
@see
494+
@ref sink,
495+
@ref file_sink,
500496
@ref parse.
501497
*/
502498
template<

include/boost/http_proto/serializer.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,9 @@ class serializer
313313
314314
@par Example
315315
@code
316-
file f;
317-
system::error_code ec;
318-
f.open("example.zip", file_mode::scan, ec);
319-
if(ec.failed())
320-
throw system::system_error(ec);
321-
serializer.start<file_source>(response, std::move(file));
316+
file f("example.zip", file_mode::scan);
317+
response.set_payload_size(f.size());
318+
serializer.start<file_source>(response, std::move(f));
322319
@endcode
323320
324321
@par Preconditions

test/unit/Jamfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ local SOURCES =
3939
fields_view.cpp
4040
fields.cpp
4141
file_mode.cpp
42-
file.cpp
4342
header_limits.cpp
4443
http_proto.cpp
4544
message_base.cpp
@@ -81,6 +80,7 @@ for local f in $(SOURCES)
8180
# file tests
8281

8382
local FILE_TESTS =
83+
file.cpp
8484
file_sink.cpp
8585
file_source.cpp
8686
detail/file_posix.cpp

0 commit comments

Comments
 (0)