forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_write_operation.hpp
More file actions
90 lines (70 loc) · 2.19 KB
/
Copy pathfile_write_operation.hpp
File metadata and controls
90 lines (70 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCORO_FILE_WRITE_OPERATION_HPP_INCLUDED
#define CPPCORO_FILE_WRITE_OPERATION_HPP_INCLUDED
#include <cppcoro/config.hpp>
#include <cppcoro/cancellation_registration.hpp>
#include <cppcoro/cancellation_token.hpp>
#include <atomic>
#include <optional>
#include <experimental/coroutine>
#if CPPCORO_OS_WINNT
# include <cppcoro/detail/win32.hpp>
#endif
namespace cppcoro
{
class file_write_operation
#if CPPCORO_OS_WINNT
: private cppcoro::detail::win32::io_state
#endif
{
public:
#if CPPCORO_OS_WINNT
file_write_operation(
detail::win32::handle_t fileHandle,
std::uint64_t fileOffset,
const void* buffer,
std::size_t byteCount,
cancellation_token cancellationToken) noexcept;
#endif
file_write_operation(file_write_operation&& other) noexcept;
file_write_operation(const file_write_operation& other) noexcept;
file_write_operation& operator=(const file_write_operation& other) = delete;
file_write_operation& operator=(file_write_operation&& other) = delete;
bool await_ready() const noexcept;
bool await_suspend(std::experimental::coroutine_handle<> awaiter);
std::size_t await_resume();
private:
void on_cancellation_requested() noexcept;
#if CPPCORO_OS_WINNT
static void on_operation_completed(
detail::win32::io_state* ioState,
detail::win32::dword_t errorCode,
detail::win32::dword_t numberOfBytesTransferred,
detail::win32::ulongptr_t completionKey) noexcept;
#endif
enum class state
{
not_started,
started,
cancellation_requested,
complete
};
std::atomic<state> m_state;
#if CPPCORO_OS_WINNT
detail::win32::handle_t m_fileHandle;
#endif
const void* m_buffer;
std::size_t m_byteCount;
cancellation_token m_cancellationToken;
std::experimental::coroutine_handle<> m_awaiter;
#if CPPCORO_OS_WINNT
detail::win32::dword_t m_errorCode;
detail::win32::dword_t m_numberOfBytesTransferred;
#endif
std::optional<cancellation_registration> m_cancellationRegistration;
};
}
#endif