1#ifndef SDL3PP_ASYNCIO_H_
2#define SDL3PP_ASYNCIO_H_
4#include <SDL3/SDL_asyncio.h>
6#include "SDL3pp_stdinc.h"
118 constexpr explicit operator bool()
const {
return !!
value; }
154 constexpr explicit operator bool()
const {
return !!
value; }
191 : m_resource(resource)
246 : m_resource(SDL_AsyncIOFromFile(file, mode))
255 (
void*)(m_resource));
262 std::swap(m_resource, other.m_resource);
273 m_resource =
nullptr;
281 constexpr bool operator==(std::nullptr_t _)
const {
return !m_resource; }
284 constexpr explicit operator bool()
const {
return !!m_resource; }
425 void Write(
void* ptr,
465 SDL_ASYNCIO_TASK_READ;
468 SDL_ASYNCIO_TASK_WRITE;
471 SDL_ASYNCIO_TASK_CLOSE;
481 SDL_ASYNCIO_COMPLETE;
487 SDL_ASYNCIO_CANCELED;
527 : m_resource(resource)
562 : m_resource(SDL_CreateAsyncIOQueue())
572 std::swap(m_resource, other.m_resource);
583 m_resource =
nullptr;
591 constexpr bool operator==(std::nullptr_t _)
const {
return !m_resource; }
594 constexpr explicit operator bool()
const {
return !!m_resource; }
650 std::optional<AsyncIOOutcome>
GetResult();
821 return AsyncIO(std::move(file), std::move(mode));
840 return CheckError(SDL_GetAsyncIOSize(asyncio));
887 CheckError(SDL_ReadAsyncIO(asyncio, ptr, offset, size, queue, userdata));
940 CheckError(SDL_WriteAsyncIO(asyncio, ptr, offset, size, queue, userdata));
1004 return SDL_CloseAsyncIO(asyncio, flush, queue, userdata);
1061 SDL_DestroyAsyncIOQueue(queue);
1091 if (
AsyncIOOutcome outcome; SDL_GetAsyncIOResult(queue, &outcome)) {
1094 return std::nullopt;
1146 SDL_WaitAsyncIOResult(queue, &outcome, timeout.count())) {
1149 return std::nullopt;
1193 if (
AsyncIOOutcome outcome; SDL_WaitAsyncIOResult(queue, &outcome, -1)) {
1196 return std::nullopt;
1234 SDL_SignalAsyncIOQueue(queue);
1272 CheckError(SDL_LoadFileAsync(file, queue, userdata));
A queue of completed asynchronous I/O tasks.
Definition: SDL3pp_asyncio.h:515
~AsyncIOQueue()
Destructor.
Definition: SDL3pp_asyncio.h:567
AsyncIOQueue & operator=(AsyncIOQueue other)
Assignment operator.
Definition: SDL3pp_asyncio.h:570
constexpr AsyncIOQueue(const AsyncIOQueueRaw resource)
Constructs from AsyncIOQueueParam.
Definition: SDL3pp_asyncio.h:526
constexpr AsyncIOQueueRaw get() const
Retrieves underlying AsyncIOQueueRaw.
Definition: SDL3pp_asyncio.h:577
constexpr AsyncIOQueue(const AsyncIOQueue &other)=delete
Copy constructor.
constexpr AsyncIOQueueRaw release()
Retrieves underlying AsyncIOQueueRaw and clear this.
Definition: SDL3pp_asyncio.h:580
constexpr auto operator<=>(const AsyncIOQueue &other) const =default
Comparison.
constexpr AsyncIOQueue(AsyncIOQueue &&other)
Move constructor.
Definition: SDL3pp_asyncio.h:535
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_asyncio.h:591
AsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition: SDL3pp_asyncio.h:561
The asynchronous I/O operation structure.
Definition: SDL3pp_asyncio.h:176
constexpr AsyncIO(const AsyncIO &other)=delete
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:245
constexpr AsyncIO(const AsyncIORaw resource)
Constructs from AsyncIOParam.
Definition: SDL3pp_asyncio.h:190
~AsyncIO()
Destructor.
Definition: SDL3pp_asyncio.h:251
constexpr AsyncIO(AsyncIO &&other)
Move constructor.
Definition: SDL3pp_asyncio.h:199
AsyncIO & operator=(AsyncIO other)
Assignment operator.
Definition: SDL3pp_asyncio.h:260
constexpr AsyncIORaw release()
Retrieves underlying AsyncIORaw and clear this.
Definition: SDL3pp_asyncio.h:270
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_asyncio.h:281
constexpr AsyncIO()=default
Default ctor.
constexpr AsyncIORaw get() const
Retrieves underlying AsyncIORaw.
Definition: SDL3pp_asyncio.h:267
constexpr auto operator<=>(const AsyncIO &other) const =default
Comparison.
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:843
bool Close(bool flush, AsyncIOQueueParam queue, void *userdata)
Close and free any allocated resources for an async I/O object.
Definition: SDL3pp_asyncio.h:1007
void SignalAsyncIOQueue(AsyncIOQueueParam queue)
Wake up any threads that are blocking in AsyncIOQueue.WaitResult().
Definition: SDL3pp_asyncio.h:1232
void Destroy()
Destroy a previously-created async I/O task queue.
Definition: SDL3pp_asyncio.h:1064
constexpr AsyncIOResult ASYNCIO_CANCELED
request was canceled before completing.
Definition: SDL3pp_asyncio.h:486
AsyncIOQueue CreateAsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition: SDL3pp_asyncio.h:1029
SDL_AsyncIOOutcome AsyncIOOutcome
Information about a completed asynchronous I/O request.
Definition: SDL3pp_asyncio.h:494
constexpr AsyncIOTaskType ASYNCIO_TASK_CLOSE
A close operation.
Definition: SDL3pp_asyncio.h:470
void Read(void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async read.
Definition: SDL3pp_asyncio.h:890
SDL_AsyncIOTaskType AsyncIOTaskType
Types of asynchronous I/O tasks.
Definition: SDL3pp_asyncio.h:462
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:819
std::optional< AsyncIOOutcome > WaitAsyncIOResult(AsyncIOQueueParam queue, Milliseconds timeout)
Block until an async I/O task queue has a completed task.
Definition: SDL3pp_asyncio.h:1142
constexpr AsyncIOTaskType ASYNCIO_TASK_READ
A read operation.
Definition: SDL3pp_asyncio.h:464
void Signal()
Wake up any threads that are blocking in AsyncIOQueue.WaitResult().
Definition: SDL3pp_asyncio.h:1237
std::optional< AsyncIOOutcome > GetResult()
Query an async I/O task queue for completed tasks.
Definition: SDL3pp_asyncio.h:1097
void WriteAsyncIO(AsyncIOParam asyncio, void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async write.
Definition: SDL3pp_asyncio.h:933
SDL_AsyncIO * AsyncIORaw
Alias to raw representation for AsyncIO.
Definition: SDL3pp_asyncio.h:95
constexpr AsyncIOResult ASYNCIO_COMPLETE
request was completed without error
Definition: SDL3pp_asyncio.h:480
void LoadFileAsync(StringParam file, AsyncIOQueueParam queue, void *userdata)
Load all the data from a file path, asynchronously.
Definition: SDL3pp_asyncio.h:1268
std::optional< AsyncIOOutcome > WaitResult()
Block until an async I/O task queue has a completed task.
Definition: SDL3pp_asyncio.h:1205
void Write(void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async write.
Definition: SDL3pp_asyncio.h:943
constexpr AsyncIOResult ASYNCIO_FAILURE
request failed for some reason; check GetError()!
Definition: SDL3pp_asyncio.h:483
void DestroyAsyncIOQueue(AsyncIOQueueRaw queue)
Destroy a previously-created async I/O task queue.
Definition: SDL3pp_asyncio.h:1059
std::optional< AsyncIOOutcome > GetAsyncIOResult(AsyncIOQueueParam queue)
Query an async I/O task queue for completed tasks.
Definition: SDL3pp_asyncio.h:1089
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:999
constexpr AsyncIOTaskType ASYNCIO_TASK_WRITE
A write operation.
Definition: SDL3pp_asyncio.h:467
SDL_AsyncIOResult AsyncIOResult
Possible outcomes of an asynchronous I/O task.
Definition: SDL3pp_asyncio.h:478
void ReadAsyncIO(AsyncIOParam asyncio, void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async read.
Definition: SDL3pp_asyncio.h:880
Sint64 GetAsyncIOSize(AsyncIOParam asyncio)
Use this function to get the size of the data stream in an AsyncIO.
Definition: SDL3pp_asyncio.h:838
SDL_AsyncIOQueue * AsyncIOQueueRaw
Alias to raw representation for AsyncIOQueue.
Definition: SDL3pp_asyncio.h:131
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:198
constexpr LogCategory LOG_CATEGORY_ERROR
ERROR.
Definition: SDL3pp_log.h:432
void LogDebug(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_DEBUG.
Definition: SDL3pp_log.h:807
Sint64 Sint64
A signed 64-bit integer type.
Definition: SDL3pp_stdinc.h:344
std::chrono::milliseconds Milliseconds
Duration in Miliseconds (Uint32).
Definition: SDL3pp_stdinc.h:380
Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:363
Main include header for the SDL3pp library.
Safely wrap AsyncIO for non owning parameters.
Definition: SDL3pp_asyncio.h:102
AsyncIORaw value
parameter's AsyncIORaw
Definition: SDL3pp_asyncio.h:103
constexpr AsyncIOParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_asyncio.h:112
constexpr AsyncIOParam(AsyncIORaw value)
Constructs from AsyncIORaw.
Definition: SDL3pp_asyncio.h:106
constexpr auto operator<=>(const AsyncIOParam &other) const =default
Comparison.
Safely wrap AsyncIOQueue for non owning parameters.
Definition: SDL3pp_asyncio.h:138
constexpr AsyncIOQueueParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_asyncio.h:148
AsyncIOQueueRaw value
parameter's AsyncIOQueueRaw
Definition: SDL3pp_asyncio.h:139
constexpr AsyncIOQueueParam(AsyncIOQueueRaw value)
Constructs from AsyncIOQueueRaw.
Definition: SDL3pp_asyncio.h:142
constexpr auto operator<=>(const AsyncIOQueueParam &other) const =default
Comparison.
Semi-safe reference for AsyncIOQueue.
Definition: SDL3pp_asyncio.h:759
~AsyncIOQueueRef()
Destructor.
Definition: SDL3pp_asyncio.h:779
AsyncIOQueueRef(AsyncIOQueueParam resource)
Constructs from AsyncIOQueueParam.
Definition: SDL3pp_asyncio.h:767
AsyncIOQueueRef(const AsyncIOQueueRef &other)
Copy constructor.
Definition: SDL3pp_asyncio.h:773
Semi-safe reference for AsyncIO.
Definition: SDL3pp_asyncio.h:434
AsyncIORef(const AsyncIORef &other)
Copy constructor.
Definition: SDL3pp_asyncio.h:448
AsyncIORef(AsyncIOParam resource)
Constructs from AsyncIOParam.
Definition: SDL3pp_asyncio.h:442
~AsyncIORef()
Destructor.
Definition: SDL3pp_asyncio.h:454