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(std::nullptr_t = nullptr) noexcept
180 : m_resource(0)
181 {
182 }
183
191 constexpr explicit AsyncIO(const AsyncIORaw resource) noexcept
192 : m_resource(resource)
193 {
194 }
195
196protected:
198 constexpr AsyncIO(const AsyncIO& other) noexcept = default;
199
200public:
202 constexpr AsyncIO(AsyncIO&& other) noexcept
203 : AsyncIO(other.release())
204 {
205 }
206
207 constexpr AsyncIO(const AsyncIORef& other) = delete;
208
209 constexpr AsyncIO(AsyncIORef&& other) = delete;
210
249 : m_resource(SDL_AsyncIOFromFile(file, mode))
250 {
251 }
252
255 {
256 if (m_resource) {
257 LOG_CATEGORY_ERROR.LogDebug("AsyncIO ID was not properly Destroyed: {}",
258 (void*)(m_resource));
259 }
260 }
261
263 constexpr AsyncIO& operator=(AsyncIO&& other) noexcept
264 {
265 std::swap(m_resource, other.m_resource);
266 return *this;
267 }
268
269protected:
271 constexpr AsyncIO& operator=(const AsyncIO& other) noexcept = default;
272
273public:
275 constexpr AsyncIORaw get() const noexcept { return m_resource; }
276
278 constexpr AsyncIORaw release() noexcept
279 {
280 auto r = m_resource;
281 m_resource = nullptr;
282 return r;
283 }
284
286 constexpr auto operator<=>(const AsyncIO& other) const noexcept = default;
287
289 constexpr explicit operator bool() const noexcept { return !!m_resource; }
290
292 constexpr operator AsyncIOParam() const noexcept { return {m_resource}; }
293
340 bool Close(bool flush, AsyncIOQueueParam queue, void* userdata);
341
355 Sint64 GetSize();
356
391 void Read(void* ptr,
392 Uint64 offset,
393 Uint64 size,
394 AsyncIOQueueParam queue,
395 void* userdata);
396
430 void Write(void* ptr,
431 Uint64 offset,
432 Uint64 size,
433 AsyncIOQueueParam queue,
434 void* userdata);
435};
436
439{
440 using AsyncIO::AsyncIO;
441
449 AsyncIORef(AsyncIOParam resource) noexcept
450 : AsyncIO(resource.value)
451 {
452 }
453
461 AsyncIORef(AsyncIORaw resource) noexcept
462 : AsyncIO(resource)
463 {
464 }
465
467 constexpr AsyncIORef(const AsyncIORef& other) noexcept = default;
468
471};
472
478using AsyncIOTaskType = SDL_AsyncIOTaskType;
479
481 SDL_ASYNCIO_TASK_READ;
482
484 SDL_ASYNCIO_TASK_WRITE;
485
487 SDL_ASYNCIO_TASK_CLOSE;
488
494using AsyncIOResult = SDL_AsyncIOResult;
495
497 SDL_ASYNCIO_COMPLETE;
498
500 SDL_ASYNCIO_FAILURE;
501
503 SDL_ASYNCIO_CANCELED;
504
510using AsyncIOOutcome = SDL_AsyncIOOutcome;
511
531{
532 AsyncIOQueueRaw m_resource = nullptr;
533
534public:
536 constexpr AsyncIOQueue(std::nullptr_t) noexcept
537 : m_resource(0)
538 {
539 }
540
548 constexpr explicit AsyncIOQueue(const AsyncIOQueueRaw resource) noexcept
549 : m_resource(resource)
550 {
551 }
552
553protected:
555 constexpr AsyncIOQueue(const AsyncIOQueue& other) noexcept = default;
556
557public:
559 constexpr AsyncIOQueue(AsyncIOQueue&& other) noexcept
560 : AsyncIOQueue(other.release())
561 {
562 }
563
564 constexpr AsyncIOQueue(const AsyncIOQueueRef& other) = delete;
565
566 constexpr AsyncIOQueue(AsyncIOQueueRef&& other) = delete;
567
586 : m_resource(SDL_CreateAsyncIOQueue())
587 {
588 }
589
591 ~AsyncIOQueue() { SDL_DestroyAsyncIOQueue(m_resource); }
592
594 constexpr AsyncIOQueue& operator=(AsyncIOQueue&& other) noexcept
595 {
596 std::swap(m_resource, other.m_resource);
597 return *this;
598 }
599
600protected:
602 constexpr AsyncIOQueue& operator=(const AsyncIOQueue& other) noexcept =
603 default;
604
605public:
607 constexpr AsyncIOQueueRaw get() const noexcept { return m_resource; }
608
610 constexpr AsyncIOQueueRaw release() noexcept
611 {
612 auto r = m_resource;
613 m_resource = nullptr;
614 return r;
615 }
616
618 constexpr auto operator<=>(const AsyncIOQueue& other) const noexcept =
619 default;
620
622 constexpr explicit operator bool() const noexcept { return !!m_resource; }
623
625 constexpr operator AsyncIOQueueParam() const noexcept { return {m_resource}; }
626
652 void Destroy();
653
676 std::optional<AsyncIOOutcome> GetResult();
677
717 std::optional<AsyncIOOutcome> WaitResult(Milliseconds timeout);
718
757 std::optional<AsyncIOOutcome> WaitResult();
758
779 void Signal();
780};
781
784{
786
795 : AsyncIOQueue(resource.value)
796 {
797 }
798
807 : AsyncIOQueue(resource)
808 {
809 }
810
812 constexpr AsyncIOQueueRef(const AsyncIOQueueRef& other) noexcept = default;
813
816};
817
855{
856 return AsyncIO(std::move(file), std::move(mode));
857}
858
874{
875 return CheckError(SDL_GetAsyncIOSize(asyncio));
876}
877
878inline Sint64 AsyncIO::GetSize() { return SDL::GetAsyncIOSize(m_resource); }
879
915inline void ReadAsyncIO(AsyncIOParam asyncio,
916 void* ptr,
917 Uint64 offset,
918 Uint64 size,
919 AsyncIOQueueParam queue,
920 void* userdata)
921{
922 CheckError(SDL_ReadAsyncIO(asyncio, ptr, offset, size, queue, userdata));
923}
924
925inline void AsyncIO::Read(void* ptr,
926 Uint64 offset,
927 Uint64 size,
928 AsyncIOQueueParam queue,
929 void* userdata)
930{
931 SDL::ReadAsyncIO(m_resource, ptr, offset, size, queue, userdata);
932}
933
968inline void WriteAsyncIO(AsyncIOParam asyncio,
969 void* ptr,
970 Uint64 offset,
971 Uint64 size,
972 AsyncIOQueueParam queue,
973 void* userdata)
974{
975 CheckError(SDL_WriteAsyncIO(asyncio, ptr, offset, size, queue, userdata));
976}
977
978inline void AsyncIO::Write(void* ptr,
979 Uint64 offset,
980 Uint64 size,
981 AsyncIOQueueParam queue,
982 void* userdata)
983{
984 SDL::WriteAsyncIO(m_resource, ptr, offset, size, queue, userdata);
985}
986
1033inline bool CloseAsyncIO(AsyncIORaw asyncio,
1034 bool flush,
1035 AsyncIOQueueParam queue,
1036 void* userdata)
1037{
1038 return SDL_CloseAsyncIO(asyncio, flush, queue, userdata);
1039}
1040
1041inline bool AsyncIO::Close(bool flush, AsyncIOQueueParam queue, void* userdata)
1042{
1043 return CloseAsyncIO(release(), flush, queue, userdata);
1044}
1045
1064
1093{
1094 SDL_DestroyAsyncIOQueue(queue);
1095}
1096
1098
1122inline std::optional<AsyncIOOutcome> GetAsyncIOResult(AsyncIOQueueParam queue)
1123{
1124 if (AsyncIOOutcome outcome; SDL_GetAsyncIOResult(queue, &outcome)) {
1125 return outcome;
1126 }
1127 return std::nullopt;
1128}
1129
1130inline std::optional<AsyncIOOutcome> AsyncIOQueue::GetResult()
1131{
1132 return SDL::GetAsyncIOResult(m_resource);
1133}
1134
1174inline std::optional<AsyncIOOutcome> WaitAsyncIOResult(AsyncIOQueueParam queue,
1175 Milliseconds timeout)
1176{
1177 if (AsyncIOOutcome outcome;
1178 SDL_WaitAsyncIOResult(queue, &outcome, timeout.count())) {
1179 return outcome;
1180 }
1181 return std::nullopt;
1182}
1183
1222inline std::optional<AsyncIOOutcome> WaitAsyncIOResult(AsyncIOQueueParam queue)
1223{
1224 if (AsyncIOOutcome outcome; SDL_WaitAsyncIOResult(queue, &outcome, -1)) {
1225 return outcome;
1226 }
1227 return std::nullopt;
1228}
1229
1230inline std::optional<AsyncIOOutcome> AsyncIOQueue::WaitResult(
1231 Milliseconds timeout)
1232{
1233 return SDL::WaitAsyncIOResult(m_resource, timeout);
1234}
1235
1236inline std::optional<AsyncIOOutcome> AsyncIOQueue::WaitResult()
1237{
1238 return SDL::WaitAsyncIOResult(m_resource);
1239}
1240
1264{
1265 SDL_SignalAsyncIOQueue(queue);
1266}
1267
1268inline void AsyncIOQueue::Signal() { SDL::SignalAsyncIOQueue(m_resource); }
1269
1300 AsyncIOQueueParam queue,
1301 void* userdata)
1302{
1303 CheckError(SDL_LoadFileAsync(file, queue, userdata));
1304}
1305
1307
1308} // namespace SDL
1309
1310#endif /* SDL3PP_ASYNCIO_H_ */
A queue of completed asynchronous I/O tasks.
Definition: SDL3pp_asyncio.h:531
~AsyncIOQueue()
Destructor.
Definition: SDL3pp_asyncio.h:591
constexpr AsyncIOQueue(AsyncIOQueue &&other) noexcept
Move constructor.
Definition: SDL3pp_asyncio.h:559
constexpr AsyncIOQueue(std::nullptr_t) noexcept
Default ctor.
Definition: SDL3pp_asyncio.h:536
constexpr AsyncIOQueue(const AsyncIOQueueRaw resource) noexcept
Constructs from AsyncIOQueueParam.
Definition: SDL3pp_asyncio.h:548
constexpr AsyncIOQueueRaw release() noexcept
Retrieves underlying AsyncIOQueueRaw and clear this.
Definition: SDL3pp_asyncio.h:610
constexpr AsyncIOQueue(const AsyncIOQueue &other) noexcept=default
Copy constructor.
constexpr AsyncIOQueue & operator=(AsyncIOQueue &&other) noexcept
Assignment operator.
Definition: SDL3pp_asyncio.h:594
constexpr AsyncIOQueueRaw get() const noexcept
Retrieves underlying AsyncIOQueueRaw.
Definition: SDL3pp_asyncio.h:607
constexpr auto operator<=>(const AsyncIOQueue &other) const noexcept=default
Comparison.
constexpr AsyncIOQueue & operator=(const AsyncIOQueue &other) noexcept=default
Assignment operator.
AsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition: SDL3pp_asyncio.h:585
The asynchronous I/O operation structure.
Definition: SDL3pp_asyncio.h:174
constexpr AsyncIO(const AsyncIO &other) noexcept=default
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:248
constexpr AsyncIORaw get() const noexcept
Retrieves underlying AsyncIORaw.
Definition: SDL3pp_asyncio.h:275
constexpr AsyncIO & operator=(AsyncIO &&other) noexcept
Assignment operator.
Definition: SDL3pp_asyncio.h:263
~AsyncIO()
Destructor.
Definition: SDL3pp_asyncio.h:254
constexpr AsyncIO(const AsyncIORaw resource) noexcept
Constructs from AsyncIOParam.
Definition: SDL3pp_asyncio.h:191
constexpr AsyncIORaw release() noexcept
Retrieves underlying AsyncIORaw and clear this.
Definition: SDL3pp_asyncio.h:278
constexpr auto operator<=>(const AsyncIO &other) const noexcept=default
Comparison.
constexpr AsyncIO & operator=(const AsyncIO &other) noexcept=default
Assignment operator.
constexpr AsyncIO(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_asyncio.h:179
constexpr AsyncIO(AsyncIO &&other) noexcept
Move constructor.
Definition: SDL3pp_asyncio.h:202
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:878
bool Close(bool flush, AsyncIOQueueParam queue, void *userdata)
Close and free any allocated resources for an async I/O object.
Definition: SDL3pp_asyncio.h:1041
void SignalAsyncIOQueue(AsyncIOQueueParam queue)
Wake up any threads that are blocking in AsyncIOQueue.WaitResult().
Definition: SDL3pp_asyncio.h:1263
void Destroy()
Destroy a previously-created async I/O task queue.
Definition: SDL3pp_asyncio.h:1097
constexpr AsyncIOResult ASYNCIO_CANCELED
request was canceled before completing.
Definition: SDL3pp_asyncio.h:502
AsyncIOQueue CreateAsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition: SDL3pp_asyncio.h:1063
SDL_AsyncIOOutcome AsyncIOOutcome
Information about a completed asynchronous I/O request.
Definition: SDL3pp_asyncio.h:510
constexpr AsyncIOTaskType ASYNCIO_TASK_CLOSE
A close operation.
Definition: SDL3pp_asyncio.h:486
void Read(void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async read.
Definition: SDL3pp_asyncio.h:925
SDL_AsyncIOTaskType AsyncIOTaskType
Types of asynchronous I/O tasks.
Definition: SDL3pp_asyncio.h:478
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:854
std::optional< AsyncIOOutcome > WaitAsyncIOResult(AsyncIOQueueParam queue, Milliseconds timeout)
Block until an async I/O task queue has a completed task.
Definition: SDL3pp_asyncio.h:1174
constexpr AsyncIOTaskType ASYNCIO_TASK_READ
A read operation.
Definition: SDL3pp_asyncio.h:480
void Signal()
Wake up any threads that are blocking in AsyncIOQueue.WaitResult().
Definition: SDL3pp_asyncio.h:1268
std::optional< AsyncIOOutcome > GetResult()
Query an async I/O task queue for completed tasks.
Definition: SDL3pp_asyncio.h:1130
void WriteAsyncIO(AsyncIOParam asyncio, void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async write.
Definition: SDL3pp_asyncio.h:968
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:496
void LoadFileAsync(StringParam file, AsyncIOQueueParam queue, void *userdata)
Load all the data from a file path, asynchronously.
Definition: SDL3pp_asyncio.h:1299
std::optional< AsyncIOOutcome > WaitResult()
Block until an async I/O task queue has a completed task.
Definition: SDL3pp_asyncio.h:1236
void Write(void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async write.
Definition: SDL3pp_asyncio.h:978
constexpr AsyncIOResult ASYNCIO_FAILURE
request failed for some reason; check GetError()!
Definition: SDL3pp_asyncio.h:499
void DestroyAsyncIOQueue(AsyncIOQueueRaw queue)
Destroy a previously-created async I/O task queue.
Definition: SDL3pp_asyncio.h:1092
std::optional< AsyncIOOutcome > GetAsyncIOResult(AsyncIOQueueParam queue)
Query an async I/O task queue for completed tasks.
Definition: SDL3pp_asyncio.h:1122
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:1033
constexpr AsyncIOTaskType ASYNCIO_TASK_WRITE
A write operation.
Definition: SDL3pp_asyncio.h:483
SDL_AsyncIOResult AsyncIOResult
Possible outcomes of an asynchronous I/O task.
Definition: SDL3pp_asyncio.h:494
void ReadAsyncIO(AsyncIOParam asyncio, void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueParam queue, void *userdata)
Start an async read.
Definition: SDL3pp_asyncio.h:915
Sint64 GetAsyncIOSize(AsyncIOParam asyncio)
Use this function to get the size of the data stream in an AsyncIO.
Definition: SDL3pp_asyncio.h:873
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:811
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(AsyncIORaw value)
Constructs from AsyncIORaw.
Definition: SDL3pp_asyncio.h:104
constexpr AsyncIOParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_asyncio.h:110
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:784
~AsyncIOQueueRef()
Destructor.
Definition: SDL3pp_asyncio.h:815
AsyncIOQueueRef(AsyncIOQueueParam resource) noexcept
Constructs from AsyncIOQueueParam.
Definition: SDL3pp_asyncio.h:794
AsyncIOQueueRef(AsyncIOQueueRaw resource) noexcept
Constructs from AsyncIOQueueParam.
Definition: SDL3pp_asyncio.h:806
constexpr AsyncIOQueueRef(const AsyncIOQueueRef &other) noexcept=default
Copy constructor.
AsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition: SDL3pp_asyncio.h:585
Semi-safe reference for AsyncIO.
Definition: SDL3pp_asyncio.h:439
constexpr AsyncIORef(const AsyncIORef &other) noexcept=default
Copy constructor.
AsyncIORef(AsyncIORaw resource) noexcept
Constructs from AsyncIOParam.
Definition: SDL3pp_asyncio.h:461
AsyncIORef(AsyncIOParam resource) noexcept
Constructs from AsyncIOParam.
Definition: SDL3pp_asyncio.h:449
~AsyncIORef()
Destructor.
Definition: SDL3pp_asyncio.h:470