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
89// Forward decl
90struct AsyncIO;
91
93using AsyncIORaw = SDL_AsyncIO*;
94
95// Forward decl
96struct AsyncIORef;
97
100{
102
105 : value(value)
106 {
107 }
108
110 constexpr AsyncIOParam(std::nullptr_t _ = nullptr)
111 : value(nullptr)
112 {
113 }
114
116 constexpr explicit operator bool() const { return !!value; }
117
119 constexpr auto operator<=>(const AsyncIOParam& other) const = default;
120
122 constexpr operator AsyncIORaw() const { return value; }
123};
124
125// Forward decl
126struct AsyncIOQueue;
127
129using AsyncIOQueueRaw = SDL_AsyncIOQueue*;
130
131// Forward decl
132struct AsyncIOQueueRef;
133
136{
138
141 : value(value)
142 {
143 }
144
146 constexpr AsyncIOQueueParam(std::nullptr_t _ = nullptr)
147 : value(nullptr)
148 {
149 }
150
152 constexpr explicit operator bool() const { return !!value; }
153
155 constexpr auto operator<=>(const AsyncIOQueueParam& other) const = default;
156
158 constexpr operator AsyncIOQueueRaw() const { return value; }
159};
160
174{
175 AsyncIORaw m_resource = nullptr;
176
177public:
179 constexpr AsyncIO() = default;
180
188 constexpr explicit AsyncIO(const AsyncIORaw resource)
189 : m_resource(resource)
190 {
191 }
192
194 constexpr AsyncIO(const AsyncIO& other) = delete;
195
197 constexpr AsyncIO(AsyncIO&& other)
198 : AsyncIO(other.release())
199 {
200 }
201
202 constexpr AsyncIO(const AsyncIORef& other) = delete;
203
204 constexpr AsyncIO(AsyncIORef&& other) = delete;
205
244 : m_resource(SDL_AsyncIOFromFile(file, mode))
245 {
246 }
247
250 {
251 if (m_resource) {
252 LOG_CATEGORY_ERROR.LogDebug("AsyncIO ID was not properly Destroyed: {}",
253 (void*)(m_resource));
254 }
255 }
256
259 {
260 std::swap(m_resource, other.m_resource);
261 return *this;
262 }
263
265 constexpr AsyncIORaw get() const { return m_resource; }
266
268 constexpr AsyncIORaw release()
269 {
270 auto r = m_resource;
271 m_resource = nullptr;
272 return r;
273 }
274
276 constexpr auto operator<=>(const AsyncIO& other) const = default;
277
279 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
280
282 constexpr explicit operator bool() const { return !!m_resource; }
283
285 constexpr operator AsyncIOParam() const { return {m_resource}; }
286
333 bool Close(bool flush, AsyncIOQueueParam queue, void* userdata);
334
348 Sint64 GetSize();
349
384 void Read(void* ptr,
385 Uint64 offset,
386 Uint64 size,
387 AsyncIOQueueParam queue,
388 void* userdata);
389
423 void Write(void* ptr,
424 Uint64 offset,
425 Uint64 size,
426 AsyncIOQueueParam queue,
427 void* userdata);
428};
429
432{
441 : AsyncIO(resource.value)
442 {
443 }
444
446 AsyncIORef(const AsyncIORef& other)
447 : AsyncIO(other.get())
448 {
449 }
450
453};
454
460using AsyncIOTaskType = SDL_AsyncIOTaskType;
461
463 SDL_ASYNCIO_TASK_READ;
464
466 SDL_ASYNCIO_TASK_WRITE;
467
469 SDL_ASYNCIO_TASK_CLOSE;
470
476using AsyncIOResult = SDL_AsyncIOResult;
477
479 SDL_ASYNCIO_COMPLETE;
480
482 SDL_ASYNCIO_FAILURE;
483
485 SDL_ASYNCIO_CANCELED;
486
492using AsyncIOOutcome = SDL_AsyncIOOutcome;
493
513{
514 AsyncIOQueueRaw m_resource = nullptr;
515
516public:
524 constexpr explicit AsyncIOQueue(const AsyncIOQueueRaw resource)
525 : m_resource(resource)
526 {
527 }
528
530 constexpr AsyncIOQueue(const AsyncIOQueue& other) = delete;
531
533 constexpr AsyncIOQueue(AsyncIOQueue&& other)
534 : AsyncIOQueue(other.release())
535 {
536 }
537
538 constexpr AsyncIOQueue(const AsyncIOQueueRef& other) = delete;
539
540 constexpr AsyncIOQueue(AsyncIOQueueRef&& other) = delete;
541
560 : m_resource(SDL_CreateAsyncIOQueue())
561 {
562 }
563
565 ~AsyncIOQueue() { SDL_DestroyAsyncIOQueue(m_resource); }
566
569 {
570 std::swap(m_resource, other.m_resource);
571 return *this;
572 }
573
575 constexpr AsyncIOQueueRaw get() const { return m_resource; }
576
579 {
580 auto r = m_resource;
581 m_resource = nullptr;
582 return r;
583 }
584
586 constexpr auto operator<=>(const AsyncIOQueue& other) const = default;
587
589 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
590
592 constexpr explicit operator bool() const { return !!m_resource; }
593
595 constexpr operator AsyncIOQueueParam() const { return {m_resource}; }
596
622 void Destroy();
623
646 std::optional<AsyncIOOutcome> GetResult();
647
687 std::optional<AsyncIOOutcome> WaitResult(Milliseconds timeout);
688
727 std::optional<AsyncIOOutcome> WaitResult();
728
749 void Signal();
750};
751
754{
763 : AsyncIOQueue(resource.value)
764 {
765 }
766
769 : AsyncIOQueue(other.get())
770 {
771 }
772
775};
776
814{
815 return AsyncIO(std::move(file), std::move(mode));
816}
817
833{
834 return CheckError(SDL_GetAsyncIOSize(asyncio));
835}
836
837inline Sint64 AsyncIO::GetSize() { return SDL::GetAsyncIOSize(m_resource); }
838
874inline void ReadAsyncIO(AsyncIOParam asyncio,
875 void* ptr,
876 Uint64 offset,
877 Uint64 size,
878 AsyncIOQueueParam queue,
879 void* userdata)
880{
881 CheckError(SDL_ReadAsyncIO(asyncio, ptr, offset, size, queue, userdata));
882}
883
884inline void AsyncIO::Read(void* ptr,
885 Uint64 offset,
886 Uint64 size,
887 AsyncIOQueueParam queue,
888 void* userdata)
889{
890 SDL::ReadAsyncIO(m_resource, ptr, offset, size, queue, userdata);
891}
892
927inline void WriteAsyncIO(AsyncIOParam asyncio,
928 void* ptr,
929 Uint64 offset,
930 Uint64 size,
931 AsyncIOQueueParam queue,
932 void* userdata)
933{
934 CheckError(SDL_WriteAsyncIO(asyncio, ptr, offset, size, queue, userdata));
935}
936
937inline void AsyncIO::Write(void* ptr,
938 Uint64 offset,
939 Uint64 size,
940 AsyncIOQueueParam queue,
941 void* userdata)
942{
943 SDL::WriteAsyncIO(m_resource, ptr, offset, size, queue, userdata);
944}
945
992inline bool CloseAsyncIO(AsyncIORaw asyncio,
993 bool flush,
994 AsyncIOQueueParam queue,
995 void* userdata)
996{
997 return SDL_CloseAsyncIO(asyncio, flush, queue, userdata);
998}
999
1000inline bool AsyncIO::Close(bool flush, AsyncIOQueueParam queue, void* userdata)
1001{
1002 return CloseAsyncIO(release(), flush, queue, userdata);
1003}
1004
1023
1052{
1053 SDL_DestroyAsyncIOQueue(queue);
1054}
1055
1057
1081inline std::optional<AsyncIOOutcome> GetAsyncIOResult(AsyncIOQueueParam queue)
1082{
1083 if (AsyncIOOutcome outcome; SDL_GetAsyncIOResult(queue, &outcome)) {
1084 return outcome;
1085 }
1086 return std::nullopt;
1087}
1088
1089inline std::optional<AsyncIOOutcome> AsyncIOQueue::GetResult()
1090{
1091 return SDL::GetAsyncIOResult(m_resource);
1092}
1093
1133inline std::optional<AsyncIOOutcome> WaitAsyncIOResult(AsyncIOQueueParam queue,
1134 Milliseconds timeout)
1135{
1136 if (AsyncIOOutcome outcome;
1137 SDL_WaitAsyncIOResult(queue, &outcome, timeout.count())) {
1138 return outcome;
1139 }
1140 return std::nullopt;
1141}
1142
1181inline std::optional<AsyncIOOutcome> WaitAsyncIOResult(AsyncIOQueueParam queue)
1182{
1183 if (AsyncIOOutcome outcome; SDL_WaitAsyncIOResult(queue, &outcome, -1)) {
1184 return outcome;
1185 }
1186 return std::nullopt;
1187}
1188
1189inline std::optional<AsyncIOOutcome> AsyncIOQueue::WaitResult(
1190 Milliseconds timeout)
1191{
1192 return SDL::WaitAsyncIOResult(m_resource, timeout);
1193}
1194
1195inline std::optional<AsyncIOOutcome> AsyncIOQueue::WaitResult()
1196{
1197 return SDL::WaitAsyncIOResult(m_resource);
1198}
1199
1223{
1224 SDL_SignalAsyncIOQueue(queue);
1225}
1226
1227inline void AsyncIOQueue::Signal() { SDL::SignalAsyncIOQueue(m_resource); }
1228
1259 AsyncIOQueueParam queue,
1260 void* userdata)
1261{
1262 CheckError(SDL_LoadFileAsync(file, queue, userdata));
1263}
1264
1266
1267} // namespace SDL
1268
1269#endif /* SDL3PP_ASYNCIO_H_ */
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