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; }
189 : m_resource(resource)
244 : m_resource(SDL_AsyncIOFromFile(file, mode))
253 (
void*)(m_resource));
260 std::swap(m_resource, other.m_resource);
271 m_resource =
nullptr;
279 constexpr bool operator==(std::nullptr_t _)
const {
return !m_resource; }
282 constexpr explicit operator bool()
const {
return !!m_resource; }
423 void Write(
void* ptr,
463 SDL_ASYNCIO_TASK_READ;
466 SDL_ASYNCIO_TASK_WRITE;
469 SDL_ASYNCIO_TASK_CLOSE;
479 SDL_ASYNCIO_COMPLETE;
485 SDL_ASYNCIO_CANCELED;
525 : m_resource(resource)
560 : m_resource(SDL_CreateAsyncIOQueue())
570 std::swap(m_resource, other.m_resource);
581 m_resource =
nullptr;
589 constexpr bool operator==(std::nullptr_t _)
const {
return !m_resource; }
592 constexpr explicit operator bool()
const {
return !!m_resource; }
646 std::optional<AsyncIOOutcome>
GetResult();
815 return AsyncIO(std::move(file), std::move(mode));
834 return CheckError(SDL_GetAsyncIOSize(asyncio));
881 CheckError(SDL_ReadAsyncIO(asyncio, ptr, offset, size, queue, userdata));
934 CheckError(SDL_WriteAsyncIO(asyncio, ptr, offset, size, queue, userdata));
997 return SDL_CloseAsyncIO(asyncio, flush, queue, userdata);
1053 SDL_DestroyAsyncIOQueue(queue);
1083 if (
AsyncIOOutcome outcome; SDL_GetAsyncIOResult(queue, &outcome)) {
1086 return std::nullopt;
1137 SDL_WaitAsyncIOResult(queue, &outcome, timeout.count())) {
1140 return std::nullopt;
1183 if (
AsyncIOOutcome outcome; SDL_WaitAsyncIOResult(queue, &outcome, -1)) {
1186 return std::nullopt;
1224 SDL_SignalAsyncIOQueue(queue);
1262 CheckError(SDL_LoadFileAsync(file, queue, userdata));
A queue of completed asynchronous I/O tasks.
Definition: SDL3pp_asyncio.h:513
~AsyncIOQueue()
Destructor.
Definition: SDL3pp_asyncio.h:565
AsyncIOQueue & operator=(AsyncIOQueue other)
Assignment operator.
Definition: SDL3pp_asyncio.h:568
constexpr AsyncIOQueue(const AsyncIOQueueRaw resource)
Constructs from AsyncIOQueueParam.
Definition: SDL3pp_asyncio.h:524
constexpr AsyncIOQueueRaw get() const
Retrieves underlying AsyncIOQueueRaw.
Definition: SDL3pp_asyncio.h:575
constexpr AsyncIOQueue(const AsyncIOQueue &other)=delete
Copy constructor.
constexpr AsyncIOQueueRaw release()
Retrieves underlying AsyncIOQueueRaw and clear this.
Definition: SDL3pp_asyncio.h:578
constexpr auto operator<=>(const AsyncIOQueue &other) const =default
Comparison.
constexpr AsyncIOQueue(AsyncIOQueue &&other)
Move constructor.
Definition: SDL3pp_asyncio.h:533
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_asyncio.h:589
AsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition: SDL3pp_asyncio.h:559
The asynchronous I/O operation structure.
Definition: SDL3pp_asyncio.h:174
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:243
constexpr AsyncIO(const AsyncIORaw resource)
Constructs from AsyncIOParam.
Definition: SDL3pp_asyncio.h:188
~AsyncIO()
Destructor.
Definition: SDL3pp_asyncio.h:249
constexpr AsyncIO(AsyncIO &&other)
Move constructor.
Definition: SDL3pp_asyncio.h:197
AsyncIO & operator=(AsyncIO other)
Assignment operator.
Definition: SDL3pp_asyncio.h:258
constexpr AsyncIORaw release()
Retrieves underlying AsyncIORaw and clear this.
Definition: SDL3pp_asyncio.h:268
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_asyncio.h:279
constexpr AsyncIO()=default
Default ctor.
constexpr AsyncIORaw get() const
Retrieves underlying AsyncIORaw.
Definition: SDL3pp_asyncio.h:265
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:837
bool Close(bool flush, AsyncIOQueueParam queue, void *userdata)
Close and free any allocated resources for an async I/O object.
Definition: SDL3pp_asyncio.h:1000
void SignalAsyncIOQueue(AsyncIOQueueParam queue)
Wake up any threads that are blocking in AsyncIOQueue.WaitResult().
Definition: SDL3pp_asyncio.h:1222
void Destroy()
Destroy a previously-created async I/O task queue.
Definition: SDL3pp_asyncio.h:1056
constexpr AsyncIOResult ASYNCIO_CANCELED
request was canceled before completing.
Definition: SDL3pp_asyncio.h:484
AsyncIOQueue CreateAsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition: SDL3pp_asyncio.h:1022
SDL_AsyncIOOutcome AsyncIOOutcome
Information about a completed asynchronous I/O request.
Definition: SDL3pp_asyncio.h:492
constexpr AsyncIOTaskType ASYNCIO_TASK_CLOSE
A close operation.
Definition: SDL3pp_asyncio.h:468
void Read(void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async read.
Definition: SDL3pp_asyncio.h:884
SDL_AsyncIOTaskType AsyncIOTaskType
Types of asynchronous I/O tasks.
Definition: SDL3pp_asyncio.h:460
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:813
std::optional< AsyncIOOutcome > WaitAsyncIOResult(AsyncIOQueueParam queue, Milliseconds timeout)
Block until an async I/O task queue has a completed task.
Definition: SDL3pp_asyncio.h:1133
constexpr AsyncIOTaskType ASYNCIO_TASK_READ
A read operation.
Definition: SDL3pp_asyncio.h:462
void Signal()
Wake up any threads that are blocking in AsyncIOQueue.WaitResult().
Definition: SDL3pp_asyncio.h:1227
std::optional< AsyncIOOutcome > GetResult()
Query an async I/O task queue for completed tasks.
Definition: SDL3pp_asyncio.h:1089
void WriteAsyncIO(AsyncIOParam asyncio, void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async write.
Definition: SDL3pp_asyncio.h:927
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:478
void LoadFileAsync(StringParam file, AsyncIOQueueParam queue, void *userdata)
Load all the data from a file path, asynchronously.
Definition: SDL3pp_asyncio.h:1258
std::optional< AsyncIOOutcome > WaitResult()
Block until an async I/O task queue has a completed task.
Definition: SDL3pp_asyncio.h:1195
void Write(void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async write.
Definition: SDL3pp_asyncio.h:937
constexpr AsyncIOResult ASYNCIO_FAILURE
request failed for some reason; check GetError()!
Definition: SDL3pp_asyncio.h:481
void DestroyAsyncIOQueue(AsyncIOQueueRaw queue)
Destroy a previously-created async I/O task queue.
Definition: SDL3pp_asyncio.h:1051
std::optional< AsyncIOOutcome > GetAsyncIOResult(AsyncIOQueueParam queue)
Query an async I/O task queue for completed tasks.
Definition: SDL3pp_asyncio.h:1081
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:992
constexpr AsyncIOTaskType ASYNCIO_TASK_WRITE
A write operation.
Definition: SDL3pp_asyncio.h:465
SDL_AsyncIOResult AsyncIOResult
Possible outcomes of an asynchronous I/O task.
Definition: SDL3pp_asyncio.h:476
void ReadAsyncIO(AsyncIOParam asyncio, void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async read.
Definition: SDL3pp_asyncio.h:874
Sint64 GetAsyncIOSize(AsyncIOParam asyncio)
Use this function to get the size of the data stream in an AsyncIO.
Definition: SDL3pp_asyncio.h:832
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:809
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(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_asyncio.h:110
constexpr AsyncIOParam(AsyncIORaw value)
Constructs from AsyncIORaw.
Definition: SDL3pp_asyncio.h:104
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:754
~AsyncIOQueueRef()
Destructor.
Definition: SDL3pp_asyncio.h:774
AsyncIOQueueRef(AsyncIOQueueParam resource)
Constructs from AsyncIOQueueParam.
Definition: SDL3pp_asyncio.h:762
AsyncIOQueueRef(const AsyncIOQueueRef &other)
Copy constructor.
Definition: SDL3pp_asyncio.h:768
Semi-safe reference for AsyncIO.
Definition: SDL3pp_asyncio.h:432
AsyncIORef(const AsyncIORef &other)
Copy constructor.
Definition: SDL3pp_asyncio.h:446
AsyncIORef(AsyncIOParam resource)
Constructs from AsyncIOParam.
Definition: SDL3pp_asyncio.h:440
~AsyncIORef()
Destructor.
Definition: SDL3pp_asyncio.h:452