From d0e171784ce78f191a9bcb40857fc2656a1079d8 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Tue, 22 Aug 2017 21:05:51 +0300 Subject: [PATCH] Implemented fs::file::get_handle --- Utilities/File.cpp | 28 ++++++++++++++++++++++++++-- Utilities/File.h | 15 +++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Utilities/File.cpp b/Utilities/File.cpp index 4abdd90c36..225c7d4547 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -772,7 +772,7 @@ fs::file::file(const std::string& path, bs_t mode) return; } - class windows_file final : public file_base + class windows_file final : public file_base, public get_native_handle { const HANDLE m_handle; @@ -879,6 +879,11 @@ fs::file::file(const std::string& path, bs_t mode) return size.QuadPart; } + + native_handle get() override + { + return m_handle; + } }; m_file = std::make_unique(handle); @@ -902,7 +907,7 @@ fs::file::file(const std::string& path, bs_t mode) return; } - class unix_file final : public file_base + class unix_file final : public file_base, public get_native_handle { const int m_fd; @@ -991,6 +996,11 @@ fs::file::file(const std::string& path, bs_t mode) return file_info.st_size; } + + native_handle get() override + { + return m_fd; + } }; m_file = std::make_unique(fd); @@ -1066,6 +1076,20 @@ fs::file::file(const void* ptr, std::size_t size) m_file = std::make_unique(ptr, size); } +fs::native_handle fs::file::get_handle() const +{ + if (auto getter = dynamic_cast(m_file.get())) + { + return getter->get(); + } + +#ifdef _WIN32 + return INVALID_HANDLE_VALUE; +#else + return -1; +#endif +} + void fs::dir::xnull() const { fmt::throw_exception("fs::dir is null"); diff --git a/Utilities/File.h b/Utilities/File.h index 8c99fc11ce..9293410461 100644 --- a/Utilities/File.h +++ b/Utilities/File.h @@ -10,6 +10,12 @@ namespace fs { +#ifdef _WIN32 + using native_handle = void*; +#else + using native_handle = int; +#endif + // File open mode flags enum class open_mode : u32 { @@ -55,6 +61,12 @@ namespace fs s64 ctime; }; + // Native handle getter + struct get_native_handle + { + virtual native_handle get() = 0; + }; + // File handle base struct file_base { @@ -347,6 +359,9 @@ namespace fs if (seek(0), !read(result)) xfail(); return result; } + + // Get native handle if available + native_handle get_handle() const; }; class dir final