1#ifndef SDL3PP_ASYNCIO_H_
2#define SDL3PP_ASYNCIO_H_
4#include <SDL3/SDL_asyncio.h>
6#include "SDL3pp_stdinc.h"
116 constexpr explicit operator bool()
const {
return !!
value; }
152 constexpr explicit operator bool()
const {
return !!
value; }
179 constexpr AsyncIO(std::nullptr_t =
nullptr) noexcept
192 : m_resource(resource)
249 : m_resource(SDL_AsyncIOFromFile(file, mode))
258 (
void*)(m_resource));
265 std::swap(m_resource, other.m_resource);
281 m_resource =
nullptr;
289 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
292 constexpr operator AsyncIOParam() const noexcept {
return {m_resource}; }
430 void Write(
void* ptr,
481 SDL_ASYNCIO_TASK_READ;
484 SDL_ASYNCIO_TASK_WRITE;
487 SDL_ASYNCIO_TASK_CLOSE;
497 SDL_ASYNCIO_COMPLETE;
503 SDL_ASYNCIO_CANCELED;
549 : m_resource(resource)
586 : m_resource(SDL_CreateAsyncIOQueue())
596 std::swap(m_resource, other.m_resource);
613 m_resource =
nullptr;
622 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
676 std::optional<AsyncIOOutcome>
GetResult();
856 return AsyncIO(std::move(file), std::move(mode));
875 return CheckError(SDL_GetAsyncIOSize(asyncio));
922 CheckError(SDL_ReadAsyncIO(asyncio, ptr, offset, size, queue, userdata));
975 CheckError(SDL_WriteAsyncIO(asyncio, ptr, offset, size, queue, userdata));
1038 return SDL_CloseAsyncIO(asyncio, flush, queue, userdata);
1094 SDL_DestroyAsyncIOQueue(queue);
1124 if (
AsyncIOOutcome outcome; SDL_GetAsyncIOResult(queue, &outcome)) {
1127 return std::nullopt;
1178 SDL_WaitAsyncIOResult(queue, &outcome, timeout.count())) {
1181 return std::nullopt;
1224 if (
AsyncIOOutcome outcome; SDL_WaitAsyncIOResult(queue, &outcome, -1)) {
1227 return std::nullopt;
1265 SDL_SignalAsyncIOQueue(queue);
1303 CheckError(SDL_LoadFileAsync(file, queue, userdata));
A queue of completed asynchronous I/O tasks.
Definition: SDL3pp_asyncio.h:531
~AsyncIOQueue()
Destructor.
Definition: SDL3pp_asyncio.h:591
constexpr AsyncIOQueue(AsyncIOQueue &&other) noexcept
Move constructor.
Definition: SDL3pp_asyncio.h:559
constexpr AsyncIOQueue(std::nullptr_t) noexcept
Default ctor.
Definition: SDL3pp_asyncio.h:536
constexpr AsyncIOQueue(const AsyncIOQueueRaw resource) noexcept
Constructs from AsyncIOQueueParam.
Definition: SDL3pp_asyncio.h:548
constexpr AsyncIOQueueRaw release() noexcept
Retrieves underlying AsyncIOQueueRaw and clear this.
Definition: SDL3pp_asyncio.h:610
constexpr AsyncIOQueue(const AsyncIOQueue &other) noexcept=default
Copy constructor.
constexpr AsyncIOQueue & operator=(AsyncIOQueue &&other) noexcept
Assignment operator.
Definition: SDL3pp_asyncio.h:594
constexpr AsyncIOQueueRaw get() const noexcept
Retrieves underlying AsyncIOQueueRaw.
Definition: SDL3pp_asyncio.h:607
constexpr auto operator<=>(const AsyncIOQueue &other) const noexcept=default
Comparison.
constexpr AsyncIOQueue & operator=(const AsyncIOQueue &other) noexcept=default
Assignment operator.
AsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition: SDL3pp_asyncio.h:585
The asynchronous I/O operation structure.
Definition: SDL3pp_asyncio.h:174
constexpr AsyncIO(const AsyncIO &other) noexcept=default
Copy constructor.
AsyncIO(StringParam file, StringParam mode)
Use this function to create a new AsyncIO object for reading from and/or writing to a named file.
Definition: SDL3pp_asyncio.h:248
constexpr AsyncIORaw get() const noexcept
Retrieves underlying AsyncIORaw.
Definition: SDL3pp_asyncio.h:275
constexpr AsyncIO & operator=(AsyncIO &&other) noexcept
Assignment operator.
Definition: SDL3pp_asyncio.h:263
~AsyncIO()
Destructor.
Definition: SDL3pp_asyncio.h:254
constexpr AsyncIO(const AsyncIORaw resource) noexcept
Constructs from AsyncIOParam.
Definition: SDL3pp_asyncio.h:191
constexpr AsyncIORaw release() noexcept
Retrieves underlying AsyncIORaw and clear this.
Definition: SDL3pp_asyncio.h:278
constexpr auto operator<=>(const AsyncIO &other) const noexcept=default
Comparison.
constexpr AsyncIO & operator=(const AsyncIO &other) noexcept=default
Assignment operator.
constexpr AsyncIO(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_asyncio.h:179
constexpr AsyncIO(AsyncIO &&other) noexcept
Move constructor.
Definition: SDL3pp_asyncio.h:202
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
Sint64 GetSize()
Use this function to get the size of the data stream in an AsyncIO.
Definition: SDL3pp_asyncio.h:878
bool Close(bool flush, AsyncIOQueueParam queue, void *userdata)
Close and free any allocated resources for an async I/O object.
Definition: SDL3pp_asyncio.h:1041
void SignalAsyncIOQueue(AsyncIOQueueParam queue)
Wake up any threads that are blocking in AsyncIOQueue.WaitResult().
Definition: SDL3pp_asyncio.h:1263
void Destroy()
Destroy a previously-created async I/O task queue.
Definition: SDL3pp_asyncio.h:1097
constexpr AsyncIOResult ASYNCIO_CANCELED
request was canceled before completing.
Definition: SDL3pp_asyncio.h:502
AsyncIOQueue CreateAsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition: SDL3pp_asyncio.h:1063
SDL_AsyncIOOutcome AsyncIOOutcome
Information about a completed asynchronous I/O request.
Definition: SDL3pp_asyncio.h:510
constexpr AsyncIOTaskType ASYNCIO_TASK_CLOSE
A close operation.
Definition: SDL3pp_asyncio.h:486
void Read(void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async read.
Definition: SDL3pp_asyncio.h:925
SDL_AsyncIOTaskType AsyncIOTaskType
Types of asynchronous I/O tasks.
Definition: SDL3pp_asyncio.h:478
AsyncIO AsyncIOFromFile(StringParam file, StringParam mode)
Use this function to create a new AsyncIO object for reading from and/or writing to a named file.
Definition: SDL3pp_asyncio.h:854
std::optional< AsyncIOOutcome > WaitAsyncIOResult(AsyncIOQueueParam queue, Milliseconds timeout)
Block until an async I/O task queue has a completed task.
Definition: SDL3pp_asyncio.h:1174
constexpr AsyncIOTaskType ASYNCIO_TASK_READ
A read operation.
Definition: SDL3pp_asyncio.h:480
void Signal()
Wake up any threads that are blocking in AsyncIOQueue.WaitResult().
Definition: SDL3pp_asyncio.h:1268
std::optional< AsyncIOOutcome > GetResult()
Query an async I/O task queue for completed tasks.
Definition: SDL3pp_asyncio.h:1130
void WriteAsyncIO(AsyncIOParam asyncio, void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async write.
Definition: SDL3pp_asyncio.h:968
SDL_AsyncIO * AsyncIORaw
Alias to raw representation for AsyncIO.
Definition: SDL3pp_asyncio.h:93
constexpr AsyncIOResult ASYNCIO_COMPLETE
request was completed without error
Definition: SDL3pp_asyncio.h:496
void LoadFileAsync(StringParam file, AsyncIOQueueParam queue, void *userdata)
Load all the data from a file path, asynchronously.
Definition: SDL3pp_asyncio.h:1299
std::optional< AsyncIOOutcome > WaitResult()
Block until an async I/O task queue has a completed task.
Definition: SDL3pp_asyncio.h:1236
void Write(void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async write.
Definition: SDL3pp_asyncio.h:978
constexpr AsyncIOResult ASYNCIO_FAILURE
request failed for some reason; check GetError()!
Definition: SDL3pp_asyncio.h:499
void DestroyAsyncIOQueue(AsyncIOQueueRaw queue)
Destroy a previously-created async I/O task queue.
Definition: SDL3pp_asyncio.h:1092
std::optional< AsyncIOOutcome > GetAsyncIOResult(AsyncIOQueueParam queue)
Query an async I/O task queue for completed tasks.
Definition: SDL3pp_asyncio.h:1122
bool CloseAsyncIO(AsyncIORaw asyncio, bool flush, AsyncIOQueueParam queue, void *userdata)
Close and free any allocated resources for an async I/O object.
Definition: SDL3pp_asyncio.h:1033
constexpr AsyncIOTaskType ASYNCIO_TASK_WRITE
A write operation.
Definition: SDL3pp_asyncio.h:483
SDL_AsyncIOResult AsyncIOResult
Possible outcomes of an asynchronous I/O task.
Definition: SDL3pp_asyncio.h:494
void ReadAsyncIO(AsyncIOParam asyncio, void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async read.
Definition: SDL3pp_asyncio.h:915
Sint64 GetAsyncIOSize(AsyncIOParam asyncio)
Use this function to get the size of the data stream in an AsyncIO.
Definition: SDL3pp_asyncio.h:873
SDL_AsyncIOQueue * AsyncIOQueueRaw
Alias to raw representation for AsyncIOQueue.
Definition: SDL3pp_asyncio.h:129
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
constexpr LogCategory LOG_CATEGORY_ERROR
ERROR.
Definition: SDL3pp_log.h:435
void LogDebug(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_DEBUG.
Definition: SDL3pp_log.h:811
std::chrono::milliseconds Milliseconds
Duration in Miliseconds (Uint32).
Definition: SDL3pp_stdinc.h:386
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:371
::Sint64 Sint64
A signed 64-bit integer type.
Definition: SDL3pp_stdinc.h:356
Main include header for the SDL3pp library.
Safely wrap AsyncIO for non owning parameters.
Definition: SDL3pp_asyncio.h:100
AsyncIORaw value
parameter's AsyncIORaw
Definition: SDL3pp_asyncio.h:101
constexpr AsyncIOParam(AsyncIORaw value)
Constructs from AsyncIORaw.
Definition: SDL3pp_asyncio.h:104
constexpr AsyncIOParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_asyncio.h:110
constexpr auto operator<=>(const AsyncIOParam &other) const =default
Comparison.
Safely wrap AsyncIOQueue for non owning parameters.
Definition: SDL3pp_asyncio.h:136
constexpr AsyncIOQueueParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_asyncio.h:146
AsyncIOQueueRaw value
parameter's AsyncIOQueueRaw
Definition: SDL3pp_asyncio.h:137
constexpr AsyncIOQueueParam(AsyncIOQueueRaw value)
Constructs from AsyncIOQueueRaw.
Definition: SDL3pp_asyncio.h:140
constexpr auto operator<=>(const AsyncIOQueueParam &other) const =default
Comparison.
Semi-safe reference for AsyncIOQueue.
Definition: SDL3pp_asyncio.h:784
~AsyncIOQueueRef()
Destructor.
Definition: SDL3pp_asyncio.h:815
AsyncIOQueueRef(AsyncIOQueueParam resource) noexcept
Constructs from AsyncIOQueueParam.
Definition: SDL3pp_asyncio.h:794
AsyncIOQueueRef(AsyncIOQueueRaw resource) noexcept
Constructs from AsyncIOQueueParam.
Definition: SDL3pp_asyncio.h:806
constexpr AsyncIOQueueRef(const AsyncIOQueueRef &other) noexcept=default
Copy constructor.
AsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition: SDL3pp_asyncio.h:585
Semi-safe reference for AsyncIO.
Definition: SDL3pp_asyncio.h:439
constexpr AsyncIORef(const AsyncIORef &other) noexcept=default
Copy constructor.
AsyncIORef(AsyncIORaw resource) noexcept
Constructs from AsyncIOParam.
Definition: SDL3pp_asyncio.h:461
AsyncIORef(AsyncIOParam resource) noexcept
Constructs from AsyncIOParam.
Definition: SDL3pp_asyncio.h:449
~AsyncIORef()
Destructor.
Definition: SDL3pp_asyncio.h:470