Files
SweepStore/cpp/src/Public/sweepstore/utils/file_handle.h

73 lines
2.2 KiB
C++

#pragma once
#include <fstream>
#include <string>
#include <memory>
#ifdef WITH_UNREAL
#include "HAL/PlatformFileManager.h"
#include "GenericPlatform/GenericPlatformFile.h"
#endif
class SweepstoreFileHandle {
private:
std::string path;
#ifdef WITH_UNREAL
IFileHandle* unrealHandle;
#else
std::unique_ptr<std::fstream> stream;
#endif
public:
SweepstoreFileHandle(const std::string& p, std::ios::openmode mode = std::ios::in | std::ios::out | std::ios::binary);
const std::string& getPath() const { return path; }
#ifndef WITH_UNREAL
std::fstream& getStream() { return *stream; }
const std::fstream& getStream() const { return *stream; }
// Smart pointer-like interface
std::fstream* operator->() { return stream.get(); }
const std::fstream* operator->() const { return stream.get(); }
std::fstream& operator*() { return *stream; }
const std::fstream& operator*() const { return *stream; }
#endif
bool isOpen() const;
void close();
void flush();
// Windows-compatible I/O wrappers
#if defined(_WIN32) || defined(WITH_UNREAL)
void readSeek(std::streampos pos, std::ios::seekdir dir = std::ios::beg);
void writeSeek(std::streampos pos, std::ios::seekdir dir = std::ios::beg);
void readBytes(char* buffer, std::streamsize size);
void writeBytes(const char* buffer, std::streamsize size);
#else
// Inline for non-Windows to avoid overhead
inline void readSeek(std::streampos pos, std::ios::seekdir dir = std::ios::beg) {
stream->seekg(pos, dir);
}
inline void writeSeek(std::streampos pos, std::ios::seekdir dir = std::ios::beg) {
stream->seekp(pos, dir);
}
inline void readBytes(char* buffer, std::streamsize size) {
stream->read(buffer, size);
}
inline void writeBytes(const char* buffer, std::streamsize size) {
stream->write(buffer, size);
}
#endif
SweepstoreFileHandle(SweepstoreFileHandle&&) noexcept = default;
SweepstoreFileHandle& operator=(SweepstoreFileHandle&&) noexcept = default;
SweepstoreFileHandle(const SweepstoreFileHandle&) = delete;
SweepstoreFileHandle& operator=(const SweepstoreFileHandle&) = delete;
~SweepstoreFileHandle() {
close();
}
};