File indexing completed on 2025-01-18 09:54:50
0001
0002
0003
0004
0005 #ifndef CPPCORO_DETAIL_WIN32_HPP_INCLUDED
0006 #define CPPCORO_DETAIL_WIN32_HPP_INCLUDED
0007
0008 #include <cppcoro/config.hpp>
0009
0010 #if !CPPCORO_OS_WINNT
0011 # error <cppcoro/detail/win32.hpp> is only supported on the Windows platform.
0012 #endif
0013
0014 #include <utility>
0015 #include <cstdint>
0016
0017 struct _OVERLAPPED;
0018
0019 namespace cppcoro
0020 {
0021 namespace detail
0022 {
0023 namespace win32
0024 {
0025 using handle_t = void*;
0026 using ulongptr_t = std::uintptr_t;
0027 using longptr_t = std::intptr_t;
0028 using dword_t = unsigned long;
0029 using socket_t = std::uintptr_t;
0030 using ulong_t = unsigned long;
0031
0032 #if CPPCORO_COMPILER_MSVC
0033 # pragma warning(push)
0034 # pragma warning(disable : 4201)
0035 #endif
0036
0037
0038
0039 struct overlapped
0040 {
0041 ulongptr_t Internal;
0042 ulongptr_t InternalHigh;
0043 union
0044 {
0045 struct
0046 {
0047 dword_t Offset;
0048 dword_t OffsetHigh;
0049 };
0050 void* Pointer;
0051 };
0052 handle_t hEvent;
0053 };
0054
0055 #if CPPCORO_COMPILER_MSVC
0056 # pragma warning(pop)
0057 #endif
0058
0059 struct wsabuf
0060 {
0061 constexpr wsabuf() noexcept
0062 : len(0)
0063 , buf(nullptr)
0064 {}
0065
0066 constexpr wsabuf(void* ptr, std::size_t size)
0067 : len(size <= ulong_t(-1) ? ulong_t(size) : ulong_t(-1))
0068 , buf(static_cast<char*>(ptr))
0069 {}
0070
0071 ulong_t len;
0072 char* buf;
0073 };
0074
0075 struct io_state : win32::overlapped
0076 {
0077 using callback_type = void(
0078 io_state* state,
0079 win32::dword_t errorCode,
0080 win32::dword_t numberOfBytesTransferred,
0081 win32::ulongptr_t completionKey);
0082
0083 io_state(callback_type* callback = nullptr) noexcept
0084 : io_state(std::uint64_t(0), callback)
0085 {}
0086
0087 io_state(void* pointer, callback_type* callback) noexcept
0088 : m_callback(callback)
0089 {
0090 this->Internal = 0;
0091 this->InternalHigh = 0;
0092 this->Pointer = pointer;
0093 this->hEvent = nullptr;
0094 }
0095
0096 io_state(std::uint64_t offset, callback_type* callback) noexcept
0097 : m_callback(callback)
0098 {
0099 this->Internal = 0;
0100 this->InternalHigh = 0;
0101 this->Offset = static_cast<dword_t>(offset);
0102 this->OffsetHigh = static_cast<dword_t>(offset >> 32);
0103 this->hEvent = nullptr;
0104 }
0105
0106 callback_type* m_callback;
0107 };
0108
0109 class safe_handle
0110 {
0111 public:
0112
0113 safe_handle()
0114 : m_handle(nullptr)
0115 {}
0116
0117 explicit safe_handle(handle_t handle)
0118 : m_handle(handle)
0119 {}
0120
0121 safe_handle(const safe_handle& other) = delete;
0122
0123 safe_handle(safe_handle&& other) noexcept
0124 : m_handle(other.m_handle)
0125 {
0126 other.m_handle = nullptr;
0127 }
0128
0129 ~safe_handle()
0130 {
0131 close();
0132 }
0133
0134 safe_handle& operator=(safe_handle handle) noexcept
0135 {
0136 swap(handle);
0137 return *this;
0138 }
0139
0140 constexpr handle_t handle() const { return m_handle; }
0141
0142
0143 void close() noexcept;
0144
0145 void swap(safe_handle& other) noexcept
0146 {
0147 std::swap(m_handle, other.m_handle);
0148 }
0149
0150 bool operator==(const safe_handle& other) const
0151 {
0152 return m_handle == other.m_handle;
0153 }
0154
0155 bool operator!=(const safe_handle& other) const
0156 {
0157 return m_handle != other.m_handle;
0158 }
0159
0160 bool operator==(handle_t handle) const
0161 {
0162 return m_handle == handle;
0163 }
0164
0165 bool operator!=(handle_t handle) const
0166 {
0167 return m_handle != handle;
0168 }
0169
0170 private:
0171
0172 handle_t m_handle;
0173
0174 };
0175 }
0176 }
0177 }
0178
0179 #endif