fs/io/FileOutputStream: move code to new class BaseFileOutputStream

This commit is contained in:
Max Kellermann
2015-03-23 22:35:43 +01:00
parent 6dc3097998
commit 06827cfcf1
2 changed files with 101 additions and 49 deletions

View File

@@ -37,7 +37,7 @@
class Path;
class FileOutputStream final : public OutputStream {
class BaseFileOutputStream : public OutputStream {
const AllocatedPath path;
#ifdef WIN32
@@ -46,6 +46,73 @@ class FileOutputStream final : public OutputStream {
FileDescriptor fd;
#endif
protected:
#ifdef WIN32
template<typename P>
BaseFileOutputStream(P &&_path)
:path(std::forward<P>(_path)),
handle(INVALID_HANDLE_VALUE) {}
#else
template<typename P>
BaseFileOutputStream(P &&_path)
:path(std::forward<P>(_path)),
fd(FileDescriptor::Undefined()) {}
#endif
~BaseFileOutputStream() {
assert(!IsDefined());
}
#ifdef WIN32
void SetHandle(HANDLE _handle) {
assert(!IsDefined());
handle = _handle;
assert(IsDefined());
}
#else
FileDescriptor &SetFD() {
assert(!IsDefined());
return fd;
}
const FileDescriptor &GetFD() const {
return fd;
}
#endif
bool Close() {
assert(IsDefined());
#ifdef WIN32
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
return true;
#else
return fd.Close();
#endif
}
public:
bool IsDefined() const {
#ifdef WIN32
return handle != INVALID_HANDLE_VALUE;
#else
return fd.IsDefined();
#endif
}
Path GetPath() const {
return path;
}
/* virtual methods from class OutputStream */
bool Write(const void *data, size_t size, Error &error) override;
};
class FileOutputStream final : public BaseFileOutputStream {
#ifdef HAVE_LINKAT
/**
* Was O_TMPFILE used? If yes, then linkat() must be used to
@@ -64,19 +131,8 @@ public:
static FileOutputStream *Create(Path path, Error &error);
bool IsDefined() const {
#ifdef WIN32
return handle != INVALID_HANDLE_VALUE;
#else
return fd.IsDefined();
#endif
}
bool Commit(Error &error);
void Cancel();
/* virtual methods from class OutputStream */
bool Write(const void *data, size_t size, Error &error) override;
};
#endif