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
87
88// Forward decl
89struct AsyncIOBase;
90
91// Forward decl
92struct AsyncIO;
93
95using AsyncIORaw = SDL_AsyncIO*;
96
103
104// Forward decl
105struct AsyncIOQueueBase;
106
107// Forward decl
108struct AsyncIOQueue;
109
111using AsyncIOQueueRaw = SDL_AsyncIOQueue*;
112
119
125struct AsyncIOBase : ResourceBaseT<AsyncIORaw>
126{
128
175 bool Close(bool flush, AsyncIOQueueRef queue, void* userdata);
176
190 Sint64 GetSize();
191
226 void Read(void* ptr,
227 Uint64 offset,
228 Uint64 size,
229 AsyncIOQueueRef queue,
230 void* userdata);
231
265 void Write(void* ptr,
266 Uint64 offset,
267 Uint64 size,
268 AsyncIOQueueRef queue,
269 void* userdata);
270};
271
285{
286 using AsyncIOBase::AsyncIOBase;
287
295 constexpr explicit AsyncIO(AsyncIORaw resource) noexcept
296 : AsyncIOBase(resource)
297 {
298 }
299
301 constexpr AsyncIO(AsyncIO&& other) noexcept
302 : AsyncIO(other.release())
303 {
304 }
305
345 AsyncIO(StringParam file, StringParam mode);
346
349 {
350 if (get()) {
351 LOG_CATEGORY_ERROR.LogDebug("AsyncIO ID was not properly Destroyed: {}",
352 (void*)(get()));
353 }
354 }
355
357 constexpr AsyncIO& operator=(AsyncIO&& other) noexcept
358 {
359 swap(*this, other);
360 return *this;
361 }
362};
363
369using AsyncIOTaskType = SDL_AsyncIOTaskType;
370
372 SDL_ASYNCIO_TASK_READ;
373
375 SDL_ASYNCIO_TASK_WRITE;
376
378 SDL_ASYNCIO_TASK_CLOSE;
379
385using AsyncIOResult = SDL_AsyncIOResult;
386
388 SDL_ASYNCIO_COMPLETE;
389
391 SDL_ASYNCIO_FAILURE;
392
394 SDL_ASYNCIO_CANCELED;
395
401using AsyncIOOutcome = SDL_AsyncIOOutcome;
402
408struct AsyncIOQueueBase : ResourceBaseT<AsyncIOQueueRaw>
409{
411
437 void Destroy();
438
461 std::optional<AsyncIOOutcome> GetResult();
462
502 std::optional<AsyncIOOutcome> WaitResult(Milliseconds timeout);
503
542 std::optional<AsyncIOOutcome> WaitResult();
543
564 void Signal();
565};
566
586{
587 using AsyncIOQueueBase::AsyncIOQueueBase;
588
596 constexpr explicit AsyncIOQueue(AsyncIOQueueRaw resource) noexcept
597 : AsyncIOQueueBase(resource)
598 {
599 }
600
602 constexpr AsyncIOQueue(AsyncIOQueue&& other) noexcept
603 : AsyncIOQueue(other.release())
604 {
605 }
606
624 AsyncIOQueue();
625
627 ~AsyncIOQueue() { SDL_DestroyAsyncIOQueue(get()); }
628
630 constexpr AsyncIOQueue& operator=(AsyncIOQueue&& other) noexcept
631 {
632 swap(*this, other);
633 return *this;
634 }
635};
636
676{
677 return AsyncIO(std::move(file), std::move(mode));
678}
679
681 : AsyncIO(SDL_AsyncIOFromFile(file, mode))
682{
683}
684
700{
701 return CheckError(SDL_GetAsyncIOSize(asyncio));
702}
703
705
741inline void ReadAsyncIO(AsyncIORef asyncio,
742 void* ptr,
743 Uint64 offset,
744 Uint64 size,
745 AsyncIOQueueRef queue,
746 void* userdata)
747{
748 CheckError(SDL_ReadAsyncIO(asyncio, ptr, offset, size, queue, userdata));
749}
750
751inline void AsyncIOBase::Read(void* ptr,
752 Uint64 offset,
753 Uint64 size,
754 AsyncIOQueueRef queue,
755 void* userdata)
756{
757 SDL::ReadAsyncIO(get(), ptr, offset, size, queue, userdata);
758}
759
794inline void WriteAsyncIO(AsyncIORef asyncio,
795 void* ptr,
796 Uint64 offset,
797 Uint64 size,
798 AsyncIOQueueRef queue,
799 void* userdata)
800{
801 CheckError(SDL_WriteAsyncIO(asyncio, ptr, offset, size, queue, userdata));
802}
803
804inline void AsyncIOBase::Write(void* ptr,
805 Uint64 offset,
806 Uint64 size,
807 AsyncIOQueueRef queue,
808 void* userdata)
809{
810 SDL::WriteAsyncIO(get(), ptr, offset, size, queue, userdata);
811}
812
859inline bool CloseAsyncIO(AsyncIORaw asyncio,
860 bool flush,
861 AsyncIOQueueRef queue,
862 void* userdata)
863{
864 return SDL_CloseAsyncIO(asyncio, flush, queue, userdata);
865}
866
867inline bool AsyncIOBase::Close(bool flush,
868 AsyncIOQueueRef queue,
869 void* userdata)
870{
871 return CloseAsyncIO(release(), flush, queue, userdata);
872}
873
892
894 : AsyncIOQueue(SDL_CreateAsyncIOQueue())
895{
896}
897
925{
926 SDL_DestroyAsyncIOQueue(queue);
927}
928
930
954inline std::optional<AsyncIOOutcome> GetAsyncIOResult(AsyncIOQueueRef queue)
955{
956 if (AsyncIOOutcome outcome; SDL_GetAsyncIOResult(queue, &outcome)) {
957 return outcome;
958 }
959 return std::nullopt;
960}
961
962inline std::optional<AsyncIOOutcome> AsyncIOQueueBase::GetResult()
963{
964 return SDL::GetAsyncIOResult(get());
965}
966
1006inline std::optional<AsyncIOOutcome> WaitAsyncIOResult(AsyncIOQueueRef queue,
1007 Milliseconds timeout)
1008{
1009 if (AsyncIOOutcome outcome;
1010 SDL_WaitAsyncIOResult(queue, &outcome, narrowS32(timeout.count()))) {
1011 return outcome;
1012 }
1013 return std::nullopt;
1014}
1015
1054inline std::optional<AsyncIOOutcome> WaitAsyncIOResult(AsyncIOQueueRef queue)
1055{
1056 if (AsyncIOOutcome outcome; SDL_WaitAsyncIOResult(queue, &outcome, -1)) {
1057 return outcome;
1058 }
1059 return std::nullopt;
1060}
1061
1062inline std::optional<AsyncIOOutcome> AsyncIOQueueBase::WaitResult(
1063 Milliseconds timeout)
1064{
1065 return SDL::WaitAsyncIOResult(get(), timeout);
1066}
1067
1068inline std::optional<AsyncIOOutcome> AsyncIOQueueBase::WaitResult()
1069{
1070 return SDL::WaitAsyncIOResult(get());
1071}
1072
1096{
1097 SDL_SignalAsyncIOQueue(queue);
1098}
1099
1101
1134 AsyncIOQueueRef queue,
1135 void* userdata)
1136{
1137 CheckError(SDL_LoadFileAsync(file, queue, userdata));
1138}
1139
1141
1142} // namespace SDL
1143
1144#endif /* SDL3PP_ASYNCIO_H_ */
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:57
friend constexpr void swap(ResourceBaseT &lhs, ResourceBaseT &rhs) noexcept
Definition SDL3pp_resource.h:65
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:54
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
ResourceRefT< AsyncIOQueueBase > AsyncIOQueueRef
Reference for AsyncIOQueue.
Definition SDL3pp_asyncio.h:118
Sint64 GetSize()
Use this function to get the size of the data stream in an AsyncIO.
Definition SDL3pp_asyncio.h:704
void SignalAsyncIOQueue(AsyncIOQueueRef queue)
Wake up any threads that are blocking in WaitAsyncIOResult().
Definition SDL3pp_asyncio.h:1095
void ReadAsyncIO(AsyncIORef asyncio, void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueRef queue, void *userdata)
Start an async read.
Definition SDL3pp_asyncio.h:741
constexpr AsyncIOResult ASYNCIO_CANCELED
request was canceled before completing.
Definition SDL3pp_asyncio.h:393
AsyncIOQueue CreateAsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition SDL3pp_asyncio.h:891
bool Close(bool flush, AsyncIOQueueRef queue, void *userdata)
Close and free any allocated resources for an async I/O object.
Definition SDL3pp_asyncio.h:867
ResourceRefT< AsyncIOBase > AsyncIORef
Reference for AsyncIO.
Definition SDL3pp_asyncio.h:102
SDL_AsyncIOOutcome AsyncIOOutcome
Information about a completed asynchronous I/O request.
Definition SDL3pp_asyncio.h:401
constexpr AsyncIOTaskType ASYNCIO_TASK_CLOSE
A close operation.
Definition SDL3pp_asyncio.h:377
std::optional< AsyncIOOutcome > WaitAsyncIOResult(AsyncIOQueueRef queue, Milliseconds timeout)
Block until an async I/O task queue has a completed task.
Definition SDL3pp_asyncio.h:1006
void Write(void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueRef queue, void *userdata)
Start an async write.
Definition SDL3pp_asyncio.h:804
std::optional< AsyncIOOutcome > GetResult()
Query an async I/O task queue for completed tasks.
Definition SDL3pp_asyncio.h:962
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:675
constexpr AsyncIOTaskType ASYNCIO_TASK_READ
A read operation.
Definition SDL3pp_asyncio.h:371
void Read(void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueRef queue, void *userdata)
Start an async read.
Definition SDL3pp_asyncio.h:751
SDL_AsyncIOResult AsyncIOResult
Possible outcomes of an asynchronous I/O task.
Definition SDL3pp_asyncio.h:385
constexpr AsyncIOResult ASYNCIO_COMPLETE
request was completed without error
Definition SDL3pp_asyncio.h:387
std::optional< AsyncIOOutcome > WaitResult()
Block until an async I/O task queue has a completed task.
Definition SDL3pp_asyncio.h:1068
constexpr AsyncIOResult ASYNCIO_FAILURE
request failed for some reason; check GetError()!
Definition SDL3pp_asyncio.h:390
SDL_AsyncIOQueue * AsyncIOQueueRaw
Alias to raw representation for AsyncIOQueue.
Definition SDL3pp_asyncio.h:111
SDL_AsyncIOTaskType AsyncIOTaskType
Types of asynchronous I/O tasks.
Definition SDL3pp_asyncio.h:369
void DestroyAsyncIOQueue(AsyncIOQueueRaw queue)
Destroy a previously-created async I/O task queue.
Definition SDL3pp_asyncio.h:924
Sint64 GetAsyncIOSize(AsyncIORef asyncio)
Use this function to get the size of the data stream in an AsyncIO.
Definition SDL3pp_asyncio.h:699
void WriteAsyncIO(AsyncIORef asyncio, void *ptr, Uint64 offset, Uint64 size, AsyncIOQueueRef queue, void *userdata)
Start an async write.
Definition SDL3pp_asyncio.h:794
SDL_AsyncIO * AsyncIORaw
Alias to raw representation for AsyncIO.
Definition SDL3pp_asyncio.h:95
void LoadFileAsync(StringParam file, AsyncIOQueueRef queue, void *userdata)
Load all the data from a file path, asynchronously.
Definition SDL3pp_asyncio.h:1133
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:859
void Signal()
Wake up any threads that are blocking in WaitAsyncIOResult().
Definition SDL3pp_asyncio.h:1100
constexpr AsyncIOTaskType ASYNCIO_TASK_WRITE
A write operation.
Definition SDL3pp_asyncio.h:374
void Destroy()
Destroy a previously-created async I/O task queue.
Definition SDL3pp_asyncio.h:929
std::optional< AsyncIOOutcome > GetAsyncIOResult(AsyncIOQueueRef queue)
Query an async I/O task queue for completed tasks.
Definition SDL3pp_asyncio.h:954
AsyncIOQueue()
Create a task queue for tracking multiple I/O operations.
Definition SDL3pp_asyncio.h:893
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:441
::Sint64 Sint64
A signed 64-bit integer type.
Definition SDL3pp_stdinc.h:311
std::chrono::milliseconds Milliseconds
Duration in Miliseconds (Uint32).
Definition SDL3pp_stdinc.h:341
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:326
Main include header for the SDL3pp library.
Sint32 narrowS32(T value)
Narrows to Sint32.
Definition SDL3pp_stdinc.h:6263
Base class to AsyncIO.
Definition SDL3pp_asyncio.h:126
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
Base class to AsyncIOQueue.
Definition SDL3pp_asyncio.h:409
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
A queue of completed asynchronous I/O tasks.
Definition SDL3pp_asyncio.h:586
~AsyncIOQueue()
Destructor.
Definition SDL3pp_asyncio.h:627
constexpr AsyncIOQueue(AsyncIOQueue &&other) noexcept
Move constructor.
Definition SDL3pp_asyncio.h:602
constexpr AsyncIOQueue & operator=(AsyncIOQueue &&other) noexcept
Assignment operator.
Definition SDL3pp_asyncio.h:630
constexpr AsyncIOQueue(AsyncIOQueueRaw resource) noexcept
Constructs from raw AsyncIOQueue.
Definition SDL3pp_asyncio.h:596
The asynchronous I/O operation structure.
Definition SDL3pp_asyncio.h:285
constexpr AsyncIO(AsyncIORaw resource) noexcept
Constructs from raw AsyncIO.
Definition SDL3pp_asyncio.h:295
constexpr AsyncIO & operator=(AsyncIO &&other) noexcept
Assignment operator.
Definition SDL3pp_asyncio.h:357
~AsyncIO()
Destructor.
Definition SDL3pp_asyncio.h:348
constexpr AsyncIO(AsyncIO &&other) noexcept
Move constructor.
Definition SDL3pp_asyncio.h:301
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:93