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
91// Forward decl
92struct AsyncIO;
93
95using AsyncIORaw = SDL_AsyncIO*;
96
97// Forward decl
98struct AsyncIORef;
99
102{
104
107 : value(value)
108 {
109 }
110
112 constexpr AsyncIOParam(std::nullptr_t _ = nullptr)
113 : value(nullptr)
114 {
115 }
116
118 constexpr explicit operator bool() const { return !!value; }
119
121 constexpr auto operator<=>(const AsyncIOParam& other) const = default;
122
124 constexpr operator AsyncIORaw() const { return value; }
125};
126
127// Forward decl
128struct AsyncIOQueue;
129
131using AsyncIOQueueRaw = SDL_AsyncIOQueue*;
132
133// Forward decl
134struct AsyncIOQueueRef;
135
138{
140
143 : value(value)
144 {
145 }
146
148 constexpr AsyncIOQueueParam(std::nullptr_t _ = nullptr)
149 : value(nullptr)
150 {
151 }
152
154 constexpr explicit operator bool() const { return !!value; }
155
157 constexpr auto operator<=>(const AsyncIOQueueParam& other) const = default;
158
160 constexpr operator AsyncIOQueueRaw() const { return value; }
161};
162
176{
177 AsyncIORaw m_resource = nullptr;
178
179public:
181 constexpr AsyncIO() = default;
182
190 constexpr explicit AsyncIO(const AsyncIORaw resource)
191 : m_resource(resource)
192 {
193 }
194
196 constexpr AsyncIO(const AsyncIO& other) = delete;
197
199 constexpr AsyncIO(AsyncIO&& other)
200 : AsyncIO(other.release())
201 {
202 }
203
204 constexpr AsyncIO(const AsyncIORef& other) = delete;
205
206 constexpr AsyncIO(AsyncIORef&& other) = delete;
207
246 : m_resource(SDL_AsyncIOFromFile(file, mode))
247 {
248 }
249
252 {
253 if (m_resource) {
254 LOG_CATEGORY_ERROR.LogDebug("AsyncIO ID was not properly Destroyed: {}",
255 (void*)(m_resource));
256 }
257 }
258
261 {
262 std::swap(m_resource, other.m_resource);
263 return *this;
264 }
265
267 constexpr AsyncIORaw get() const { return m_resource; }
268
270 constexpr AsyncIORaw release()
271 {
272 auto r = m_resource;
273 m_resource = nullptr;
274 return r;
275 }
276
278 constexpr auto operator<=>(const AsyncIO& other) const = default;
279
281 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
282
284 constexpr explicit operator bool() const { return !!m_resource; }
285
287 constexpr operator AsyncIOParam() const { return {m_resource}; }
288
335 bool Close(bool flush, AsyncIOQueueParam queue, void* userdata);
336
350 Sint64 GetSize();
351
386 void Read(void* ptr,
387 Uint64 offset,
388 Uint64 size,
389 AsyncIOQueueParam queue,
390 void* userdata);
391
425 void Write(void* ptr,
426 Uint64 offset,
427 Uint64 size,
428 AsyncIOQueueParam queue,
429 void* userdata);
430};
431
434{
443 : AsyncIO(resource.value)
444 {
445 }
446
448 AsyncIORef(const AsyncIORef& other)
449 : AsyncIO(other.get())
450 {
451 }
452
455};
456
462using AsyncIOTaskType = SDL_AsyncIOTaskType;
463
465 SDL_ASYNCIO_TASK_READ;
466
468 SDL_ASYNCIO_TASK_WRITE;
469
471 SDL_ASYNCIO_TASK_CLOSE;
472
478using AsyncIOResult = SDL_AsyncIOResult;
479
481 SDL_ASYNCIO_COMPLETE;
482
484 SDL_ASYNCIO_FAILURE;
485
487 SDL_ASYNCIO_CANCELED;
488
494using AsyncIOOutcome = SDL_AsyncIOOutcome;
495
515{
516 AsyncIOQueueRaw m_resource = nullptr;
517
518public:
526 constexpr explicit AsyncIOQueue(const AsyncIOQueueRaw resource)
527 : m_resource(resource)
528 {
529 }
530
532 constexpr AsyncIOQueue(const AsyncIOQueue& other) = delete;
533
535 constexpr AsyncIOQueue(AsyncIOQueue&& other)
536 : AsyncIOQueue(other.release())
537 {
538 }
539
540 constexpr AsyncIOQueue(const AsyncIOQueueRef& other) = delete;
541
542 constexpr AsyncIOQueue(AsyncIOQueueRef&& other) = delete;
543
562 : m_resource(SDL_CreateAsyncIOQueue())
563 {
564 }
565
567 ~AsyncIOQueue() { SDL_DestroyAsyncIOQueue(m_resource); }
568
571 {
572 std::swap(m_resource, other.m_resource);
573 return *this;
574 }
575
577 constexpr AsyncIOQueueRaw get() const { return m_resource; }
578
581 {
582 auto r = m_resource;
583 m_resource = nullptr;
584 return r;
585 }
586
588 constexpr auto operator<=>(const AsyncIOQueue& other) const = default;
589
591 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
592
594 constexpr explicit operator bool() const { return !!m_resource; }
595
597 constexpr operator AsyncIOQueueParam() const { return {m_resource}; }
598
626 void Destroy();
627
650 std::optional<AsyncIOOutcome> GetResult();
651
691 std::optional<AsyncIOOutcome> WaitResult(Milliseconds timeout);
692
731 std::optional<AsyncIOOutcome> WaitResult();
732
754 void Signal();
755};
756
759{
768 : AsyncIOQueue(resource.value)
769 {
770 }
771
774 : AsyncIOQueue(other.get())
775 {
776 }
777
780};
781
820{
821 return AsyncIO(std::move(file), std::move(mode));
822}
823
839{
840 return CheckError(SDL_GetAsyncIOSize(asyncio));
841}
842
843inline Sint64 AsyncIO::GetSize() { return SDL::GetAsyncIOSize(m_resource); }
844
880inline void ReadAsyncIO(AsyncIOParam asyncio,
881 void* ptr,
882 Uint64 offset,
883 Uint64 size,
884 AsyncIOQueueParam queue,
885 void* userdata)
886{
887 CheckError(SDL_ReadAsyncIO(asyncio, ptr, offset, size, queue, userdata));
888}
889
890inline void AsyncIO::Read(void* ptr,
891 Uint64 offset,
892 Uint64 size,
893 AsyncIOQueueParam queue,
894 void* userdata)
895{
896 SDL::ReadAsyncIO(m_resource, ptr, offset, size, queue, userdata);
897}
898
933inline void WriteAsyncIO(AsyncIOParam asyncio,
934 void* ptr,
935 Uint64 offset,
936 Uint64 size,
937 AsyncIOQueueParam queue,
938 void* userdata)
939{
940 CheckError(SDL_WriteAsyncIO(asyncio, ptr, offset, size, queue, userdata));
941}
942
943inline void AsyncIO::Write(void* ptr,
944 Uint64 offset,
945 Uint64 size,
946 AsyncIOQueueParam queue,
947 void* userdata)
948{
949 SDL::WriteAsyncIO(m_resource, ptr, offset, size, queue, userdata);
950}
951
999inline bool CloseAsyncIO(AsyncIORaw asyncio,
1000 bool flush,
1001 AsyncIOQueueParam queue,
1002 void* userdata)
1003{
1004 return SDL_CloseAsyncIO(asyncio, flush, queue, userdata);
1005}
1006
1007inline bool AsyncIO::Close(bool flush, AsyncIOQueueParam queue, void* userdata)
1008{
1009 return CloseAsyncIO(release(), flush, queue, userdata);
1010}
1011
1030
1060{
1061 SDL_DestroyAsyncIOQueue(queue);
1062}
1063
1065
1089inline std::optional<AsyncIOOutcome> GetAsyncIOResult(AsyncIOQueueParam queue)
1090{
1091 if (AsyncIOOutcome outcome; SDL_GetAsyncIOResult(queue, &outcome)) {
1092 return outcome;
1093 }
1094 return std::nullopt;
1095}
1096
1097inline std::optional<AsyncIOOutcome> AsyncIOQueue::GetResult()
1098{
1099 return SDL::GetAsyncIOResult(m_resource);
1100}
1101
1142inline std::optional<AsyncIOOutcome> WaitAsyncIOResult(AsyncIOQueueParam queue,
1143 Milliseconds timeout)
1144{
1145 if (AsyncIOOutcome outcome;
1146 SDL_WaitAsyncIOResult(queue, &outcome, timeout.count())) {
1147 return outcome;
1148 }
1149 return std::nullopt;
1150}
1151
1191inline std::optional<AsyncIOOutcome> WaitAsyncIOResult(AsyncIOQueueParam queue)
1192{
1193 if (AsyncIOOutcome outcome; SDL_WaitAsyncIOResult(queue, &outcome, -1)) {
1194 return outcome;
1195 }
1196 return std::nullopt;
1197}
1198
1199inline std::optional<AsyncIOOutcome> AsyncIOQueue::WaitResult(
1200 Milliseconds timeout)
1201{
1202 return SDL::WaitAsyncIOResult(m_resource, timeout);
1203}
1204
1205inline std::optional<AsyncIOOutcome> AsyncIOQueue::WaitResult()
1206{
1207 return SDL::WaitAsyncIOResult(m_resource);
1208}
1209
1233{
1234 SDL_SignalAsyncIOQueue(queue);
1235}
1236
1237inline void AsyncIOQueue::Signal() { SDL::SignalAsyncIOQueue(m_resource); }
1238
1269 AsyncIOQueueParam queue,
1270 void* userdata)
1271{
1272 CheckError(SDL_LoadFileAsync(file, queue, userdata));
1273}
1274
1276
1277} // namespace SDL
1278
1279#endif /* SDL3PP_ASYNCIO_H_ */
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