SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_asyncio.h
1#ifndef SDL3PP_ASYNCIO_H_
2#define SDL3PP_ASYNCIO_H_
3
4#include <SDL3/SDL_asyncio.h>
5#include "SDL3pp_log.h"
6#include "SDL3pp_stdinc.h"
7
8namespace SDL {
9
88
89// Forward decl
90struct AsyncIO;
91
93using AsyncIORaw = SDL_AsyncIO*;
94
101
102// Forward decl
103struct AsyncIOQueue;
104
106using AsyncIOQueueRaw = SDL_AsyncIOQueue*;
107
114
127struct AsyncIO : ResourceBase<AsyncIORaw>
128{
130
138 constexpr explicit AsyncIO(AsyncIORaw resource) noexcept
139 : ResourceBase(resource)
140 {
141 }
142
144 constexpr AsyncIO(const AsyncIO& other) = delete;
145
147 constexpr AsyncIO(AsyncIO&& other) noexcept
148 : AsyncIO(other.release())
149 {
150 }
151
152 constexpr AsyncIO(const AsyncIORef& other) = delete;
153
154 constexpr AsyncIO(AsyncIORef&& other) = delete;
155
195 AsyncIO(StringParam file, StringParam mode);
196
199 {
200 if (get()) {
201 LOG_CATEGORY_ERROR.LogDebug("AsyncIO ID was not properly Destroyed: {}",
202 (void*)(get()));
203 }
204 }
205
207 constexpr AsyncIO& operator=(AsyncIO&& other) noexcept
208 {
209 swap(*this, other);
210 return *this;
211 }
212
214 AsyncIO& operator=(const AsyncIO& other) = delete;
215
262 bool Close(bool flush, AsyncIOQueueRef queue, void* userdata);
263
277 Sint64 GetSize();
278
313 void Read(void* ptr,
314 Uint64 offset,
315 Uint64 size,
316 AsyncIOQueueRef queue,
317 void* userdata);
318
352 void Write(void* ptr,
353 Uint64 offset,
354 Uint64 size,
355 AsyncIOQueueRef queue,
356 void* userdata);
357};
358
364using AsyncIOTaskType = SDL_AsyncIOTaskType;
365
367 SDL_ASYNCIO_TASK_READ;
368
370 SDL_ASYNCIO_TASK_WRITE;
371
373 SDL_ASYNCIO_TASK_CLOSE;
374
380using AsyncIOResult = SDL_AsyncIOResult;
381
383 SDL_ASYNCIO_COMPLETE;
384
386 SDL_ASYNCIO_FAILURE;
387
389 SDL_ASYNCIO_CANCELED;
390
396using AsyncIOOutcome = SDL_AsyncIOOutcome;
397
416struct AsyncIOQueue : ResourceBase<AsyncIOQueueRaw>
417{
419
427 constexpr explicit AsyncIOQueue(AsyncIOQueueRaw resource) noexcept
428 : ResourceBase(resource)
429 {
430 }
431
433 constexpr AsyncIOQueue(const AsyncIOQueue& other) = delete;
434
436 constexpr AsyncIOQueue(AsyncIOQueue&& other) noexcept
437 : AsyncIOQueue(other.release())
438 {
439 }
440
441 constexpr AsyncIOQueue(const AsyncIOQueueRef& other) = delete;
442
443 constexpr AsyncIOQueue(AsyncIOQueueRef&& other) = delete;
444
462 AsyncIOQueue();
463
465 ~AsyncIOQueue() { SDL_DestroyAsyncIOQueue(get()); }
466
468 constexpr AsyncIOQueue& operator=(AsyncIOQueue&& other) noexcept
469 {
470 swap(*this, other);
471 return *this;
472 }
473
475 AsyncIOQueue& operator=(const AsyncIOQueue& other) = delete;
476
502 void Destroy();
503
526 std::optional<AsyncIOOutcome> GetResult();
527
567 std::optional<AsyncIOOutcome> WaitResult(Milliseconds timeout);
568
607 std::optional<AsyncIOOutcome> WaitResult();
608
629 void Signal();
630};
631
671{
672 return AsyncIO(std::move(file), std::move(mode));
673}
674
676 : AsyncIO(SDL_AsyncIOFromFile(file, mode))
677{
678}
679
695{
696 return CheckError(SDL_GetAsyncIOSize(asyncio));
697}
698
700
736inline void ReadAsyncIO(AsyncIORef asyncio,
737 void* ptr,
738 Uint64 offset,
739 Uint64 size,
740 AsyncIOQueueRef queue,
741 void* userdata)
742{
743 CheckError(SDL_ReadAsyncIO(asyncio, ptr, offset, size, queue, userdata));
744}
745
746inline void AsyncIO::Read(void* ptr,
747 Uint64 offset,
748 Uint64 size,
749 AsyncIOQueueRef queue,
750 void* userdata)
751{
752 SDL::ReadAsyncIO(get(), ptr, offset, size, queue, userdata);
753}
754
789inline void WriteAsyncIO(AsyncIORef asyncio,
790 void* ptr,
791 Uint64 offset,
792 Uint64 size,
793 AsyncIOQueueRef queue,
794 void* userdata)
795{
796 CheckError(SDL_WriteAsyncIO(asyncio, ptr, offset, size, queue, userdata));
797}
798
799inline void AsyncIO::Write(void* ptr,
800 Uint64 offset,
801 Uint64 size,
802 AsyncIOQueueRef queue,
803 void* userdata)
804{
805 SDL::WriteAsyncIO(get(), ptr, offset, size, queue, userdata);
806}
807
854inline bool CloseAsyncIO(AsyncIORaw asyncio,
855 bool flush,
856 AsyncIOQueueRef queue,
857 void* userdata)
858{
859 return SDL_CloseAsyncIO(asyncio, flush, queue, userdata);
860}
861
862inline bool AsyncIO::Close(bool flush, AsyncIOQueueRef queue, void* userdata)
863{
864 return CloseAsyncIO(release(), flush, queue, userdata);
865}
866
885
887 : AsyncIOQueue(SDL_CreateAsyncIOQueue())
888{
889}
890
919{
920 SDL_DestroyAsyncIOQueue(queue);
921}
922
924
948inline std::optional<AsyncIOOutcome> GetAsyncIOResult(AsyncIOQueueRef queue)
949{
950 if (AsyncIOOutcome outcome; SDL_GetAsyncIOResult(queue, &outcome)) {
951 return outcome;
952 }
953 return std::nullopt;
954}
955
956inline std::optional<AsyncIOOutcome> AsyncIOQueue::GetResult()
957{
958 return SDL::GetAsyncIOResult(get());
959}
960
1000inline std::optional<AsyncIOOutcome> WaitAsyncIOResult(AsyncIOQueueRef queue,
1001 Milliseconds timeout)
1002{
1003 if (AsyncIOOutcome outcome;
1004 SDL_WaitAsyncIOResult(queue, &outcome, narrowS32(timeout.count()))) {
1005 return outcome;
1006 }
1007 return std::nullopt;
1008}
1009
1048inline std::optional<AsyncIOOutcome> WaitAsyncIOResult(AsyncIOQueueRef queue)
1049{
1050 if (AsyncIOOutcome outcome; SDL_WaitAsyncIOResult(queue, &outcome, -1)) {
1051 return outcome;
1052 }
1053 return std::nullopt;
1054}
1055
1056inline std::optional<AsyncIOOutcome> AsyncIOQueue::WaitResult(
1057 Milliseconds timeout)
1058{
1059 return SDL::WaitAsyncIOResult(get(), timeout);
1060}
1061
1062inline std::optional<AsyncIOOutcome> AsyncIOQueue::WaitResult()
1063{
1064 return SDL::WaitAsyncIOResult(get());
1065}
1066
1090{
1091 SDL_SignalAsyncIOQueue(queue);
1092}
1093
1095
1128 AsyncIOQueueRef queue,
1129 void* userdata)
1130{
1131 CheckError(SDL_LoadFileAsync(file, queue, userdata));
1132}
1133
1135
1136} // namespace SDL
1137
1138#endif /* SDL3PP_ASYNCIO_H_ */
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:53
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:56
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
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:699
ResourceRef< AsyncIO > AsyncIORef
Reference for AsyncIO.
Definition SDL3pp_asyncio.h:100
void Write(void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueRef queue, void *userdata)
Start an async write.
Definition SDL3pp_asyncio.h:799
void Destroy()
Destroy a previously-created async I/O task queue.
Definition SDL3pp_asyncio.h:923
void SignalAsyncIOQueue(AsyncIOQueueRef queue)
Wake up any threads that are blocking in AsyncIOQueue.WaitResult().
Definition SDL3pp_asyncio.h:1089
void ReadAsyncIO(AsyncIORef asyncio, void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueRef queue, void *userdata)
Start an async read.
Definition SDL3pp_asyncio.h:736
constexpr AsyncIOResult ASYNCIO_CANCELED
request was canceled before completing.
Definition SDL3pp_asyncio.h:388
AsyncIOQueue CreateAsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition SDL3pp_asyncio.h:884
SDL_AsyncIOOutcome AsyncIOOutcome
Information about a completed asynchronous I/O request.
Definition SDL3pp_asyncio.h:396
constexpr AsyncIOTaskType ASYNCIO_TASK_CLOSE
A close operation.
Definition SDL3pp_asyncio.h:372
std::optional< AsyncIOOutcome > WaitAsyncIOResult(AsyncIOQueueRef queue, Milliseconds timeout)
Block until an async I/O task queue has a completed task.
Definition SDL3pp_asyncio.h:1000
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:670
constexpr AsyncIOTaskType ASYNCIO_TASK_READ
A read operation.
Definition SDL3pp_asyncio.h:366
SDL_AsyncIOResult AsyncIOResult
Possible outcomes of an asynchronous I/O task.
Definition SDL3pp_asyncio.h:380
void Signal()
Wake up any threads that are blocking in AsyncIOQueue.WaitResult().
Definition SDL3pp_asyncio.h:1094
std::optional< AsyncIOOutcome > GetResult()
Query an async I/O task queue for completed tasks.
Definition SDL3pp_asyncio.h:956
constexpr AsyncIOResult ASYNCIO_COMPLETE
request was completed without error
Definition SDL3pp_asyncio.h:382
bool Close(bool flush, AsyncIOQueueRef queue, void *userdata)
Close and free any allocated resources for an async I/O object.
Definition SDL3pp_asyncio.h:862
std::optional< AsyncIOOutcome > WaitResult()
Block until an async I/O task queue has a completed task.
Definition SDL3pp_asyncio.h:1062
constexpr AsyncIOResult ASYNCIO_FAILURE
request failed for some reason; check GetError()!
Definition SDL3pp_asyncio.h:385
void Read(void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueRef queue, void *userdata)
Start an async read.
Definition SDL3pp_asyncio.h:746
SDL_AsyncIOQueue * AsyncIOQueueRaw
Alias to raw representation for AsyncIOQueue.
Definition SDL3pp_asyncio.h:106
SDL_AsyncIOTaskType AsyncIOTaskType
Types of asynchronous I/O tasks.
Definition SDL3pp_asyncio.h:364
void DestroyAsyncIOQueue(AsyncIOQueueRaw queue)
Destroy a previously-created async I/O task queue.
Definition SDL3pp_asyncio.h:918
Sint64 GetAsyncIOSize(AsyncIORef asyncio)
Use this function to get the size of the data stream in an AsyncIO.
Definition SDL3pp_asyncio.h:694
void WriteAsyncIO(AsyncIORef asyncio, void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueRef queue, void *userdata)
Start an async write.
Definition SDL3pp_asyncio.h:789
SDL_AsyncIO * AsyncIORaw
Alias to raw representation for AsyncIO.
Definition SDL3pp_asyncio.h:93
void LoadFileAsync(StringParam file, AsyncIOQueueRef queue, void *userdata)
Load all the data from a file path, asynchronously.
Definition SDL3pp_asyncio.h:1127
bool CloseAsyncIO(AsyncIORaw asyncio, bool flush, AsyncIOQueueRef queue, void *userdata)
Close and free any allocated resources for an async I/O object.
Definition SDL3pp_asyncio.h:854
constexpr AsyncIOTaskType ASYNCIO_TASK_WRITE
A write operation.
Definition SDL3pp_asyncio.h:369
ResourceRef< AsyncIOQueue > AsyncIOQueueRef
Reference for AsyncIOQueue.
Definition SDL3pp_asyncio.h:113
std::optional< AsyncIOOutcome > GetAsyncIOResult(AsyncIOQueueRef queue)
Query an async I/O task queue for completed tasks.
Definition SDL3pp_asyncio.h:948
AsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition SDL3pp_asyncio.h:886
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
constexpr LogCategory LOG_CATEGORY_ERROR
ERROR.
Definition SDL3pp_log.h:435
::Sint64 Sint64
A signed 64-bit integer type.
Definition SDL3pp_stdinc.h:305
std::chrono::milliseconds Milliseconds
Duration in Miliseconds (Uint32).
Definition SDL3pp_stdinc.h:335
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:320
Main include header for the SDL3pp library.
Sint32 narrowS32(T value)
Narrows to Sint32.
Definition SDL3pp_stdinc.h:6257
A queue of completed asynchronous I/O tasks.
Definition SDL3pp_asyncio.h:417
~AsyncIOQueue()
Destructor.
Definition SDL3pp_asyncio.h:465
constexpr AsyncIOQueue(AsyncIOQueue &&other) noexcept
Move constructor.
Definition SDL3pp_asyncio.h:436
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
constexpr AsyncIOQueue & operator=(AsyncIOQueue &&other) noexcept
Assignment operator.
Definition SDL3pp_asyncio.h:468
AsyncIOQueue & operator=(const AsyncIOQueue &other)=delete
Assignment operator.
constexpr AsyncIOQueue(const AsyncIOQueue &other)=delete
Copy constructor.
constexpr AsyncIOQueue(AsyncIOQueueRaw resource) noexcept
Constructs from raw AsyncIOQueue.
Definition SDL3pp_asyncio.h:427
The asynchronous I/O operation structure.
Definition SDL3pp_asyncio.h:128
constexpr AsyncIO(const AsyncIO &other)=delete
Copy constructor.
constexpr AsyncIO(AsyncIORaw resource) noexcept
Constructs from raw AsyncIO.
Definition SDL3pp_asyncio.h:138
constexpr AsyncIO & operator=(AsyncIO &&other) noexcept
Assignment operator.
Definition SDL3pp_asyncio.h:207
~AsyncIO()
Destructor.
Definition SDL3pp_asyncio.h:198
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
AsyncIO & operator=(const AsyncIO &other)=delete
Assignment operator.
constexpr AsyncIO(AsyncIO &&other) noexcept
Move constructor.
Definition SDL3pp_asyncio.h:147
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:156