Add concurrency handling implementation with ticket management and file locking
This commit is contained in:
49
cpp/src/Public/sweepstore/utils/file_handle.h
Normal file
49
cpp/src/Public/sweepstore/utils/file_handle.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
class SweepstoreFileHandle {
|
||||
private:
|
||||
std::string path;
|
||||
std::unique_ptr<std::fstream> stream;
|
||||
|
||||
public:
|
||||
SweepstoreFileHandle(const std::string& p, std::ios::openmode mode = std::ios::in | std::ios::out | std::ios::binary)
|
||||
: path(p), stream(std::make_unique<std::fstream>(p, mode)) {
|
||||
if (!stream->is_open()) {
|
||||
throw std::runtime_error("Failed to open file: " + path);
|
||||
}
|
||||
}
|
||||
|
||||
const std::string& getPath() const { return path; }
|
||||
|
||||
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; }
|
||||
|
||||
bool isOpen() const { return stream && stream->is_open(); }
|
||||
|
||||
void close() {
|
||||
if (stream) {
|
||||
stream->close();
|
||||
}
|
||||
}
|
||||
|
||||
SweepstoreFileHandle(SweepstoreFileHandle&&) noexcept = default;
|
||||
SweepstoreFileHandle& operator=(SweepstoreFileHandle&&) noexcept = default;
|
||||
|
||||
SweepstoreFileHandle(const SweepstoreFileHandle&) = delete;
|
||||
SweepstoreFileHandle& operator=(const SweepstoreFileHandle&) = delete;
|
||||
|
||||
~SweepstoreFileHandle() {
|
||||
close();
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user