diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d9bb67..8d62e1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,10 @@ if(WIN32) include/kabufuda/winsupport.hpp lib/kabufuda/AsyncIOWin32.cpp ) +elseif(NX) + target_sources(kabufuda PRIVATE + lib/kabufuda/AsyncIONX.cpp + ) else() target_sources(kabufuda PRIVATE lib/kabufuda/AsyncIOPosix.cpp diff --git a/include/kabufuda/AsyncIO.hpp b/include/kabufuda/AsyncIO.hpp index 63f1091..51af61d 100644 --- a/include/kabufuda/AsyncIO.hpp +++ b/include/kabufuda/AsyncIO.hpp @@ -1,8 +1,13 @@ #pragma once #ifndef _WIN32 +#ifdef __SWITCH__ +#include +using SizeReturn = ssize_t; +#else #include using SizeReturn = ssize_t; +#endif #else #include using SizeReturn = DWORD; @@ -16,7 +21,9 @@ using SizeReturn = DWORD; namespace kabufuda { class AsyncIO { -#ifndef _WIN32 +#ifdef __SWITCH__ + FILE* m_fd; +#elif !defined(_WIN32) int m_fd = -1; std::vector> m_queue; #else @@ -34,13 +41,19 @@ public: AsyncIO& operator=(AsyncIO&& other); AsyncIO(const AsyncIO* other) = delete; AsyncIO& operator=(const AsyncIO& other) = delete; - void resizeQueue(size_t queueSz) { m_queue.resize(queueSz); } + void resizeQueue(size_t queueSz) { +#ifndef __SWITCH__ + m_queue.resize(queueSz); +#endif + } bool asyncRead(size_t qIdx, void* buf, size_t length, off_t offset); bool asyncWrite(size_t qIdx, const void* buf, size_t length, off_t offset); ECardResult pollStatus(size_t qIdx, SizeReturn* szRet = nullptr) const; ECardResult pollStatus() const; void waitForCompletion() const; -#ifndef _WIN32 +#ifdef __SWITCH__ + explicit operator bool() const { return m_fd != nullptr; } +#elif !defined(_WIN32) explicit operator bool() const { return m_fd != -1; } #else explicit operator bool() const { return m_fh != INVALID_HANDLE_VALUE; } diff --git a/lib/kabufuda/AsyncIONX.cpp b/lib/kabufuda/AsyncIONX.cpp new file mode 100644 index 0000000..2ec06c3 --- /dev/null +++ b/lib/kabufuda/AsyncIONX.cpp @@ -0,0 +1,48 @@ +#include "kabufuda/AsyncIO.hpp" + +#include +#include + +namespace kabufuda { + +AsyncIO::AsyncIO(SystemStringView filename, bool truncate) { m_fd = fopen(filename.data(), truncate ? "rw" : "ra"); } + +AsyncIO::~AsyncIO() { + if (*this) { + fclose(m_fd); + } +} + +AsyncIO::AsyncIO(AsyncIO&& other) { + m_fd = other.m_fd; + other.m_fd = nullptr; + m_maxBlock = other.m_maxBlock; +} + +AsyncIO& AsyncIO::operator=(AsyncIO&& other) { + if (*this) { + fclose(m_fd); + } + m_fd = other.m_fd; + other.m_fd = nullptr; + m_maxBlock = other.m_maxBlock; + return *this; +} + +void AsyncIO::_waitForOperation(size_t qIdx) const {} + +bool AsyncIO::asyncRead(size_t qIdx, void* buf, size_t length, off_t offset) { + return fread(buf, length, offset, m_fd); +} + +bool AsyncIO::asyncWrite(size_t qIdx, const void* buf, size_t length, off_t offset) { + return fwrite(buf, length, offset, m_fd); +} + +ECardResult AsyncIO::pollStatus(size_t qIdx, SizeReturn* szRet) const { return ECardResult::READY; } + +ECardResult AsyncIO::pollStatus() const { return ECardResult::READY; } + +void AsyncIO::waitForCompletion() const {} + +} // namespace kabufuda