diff options
author | Max Kellermann <max@duempel.org> | 2015-03-23 22:35:43 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2015-03-23 22:36:03 +0100 |
commit | 06827cfcf14f33578432b1c5435f827caaf1de6f (patch) | |
tree | b0747e78e4c77b9433115fe6ab76b07d10e444d5 /src/fs/io/FileOutputStream.hxx | |
parent | 6dc3097998e69ec97aff68b9ef80ac9eb8dd76d8 (diff) | |
download | mpd-06827cfcf14f33578432b1c5435f827caaf1de6f.tar.gz mpd-06827cfcf14f33578432b1c5435f827caaf1de6f.tar.xz mpd-06827cfcf14f33578432b1c5435f827caaf1de6f.zip |
fs/io/FileOutputStream: move code to new class BaseFileOutputStream
Diffstat (limited to '')
-rw-r--r-- | src/fs/io/FileOutputStream.hxx | 80 |
1 files changed, 68 insertions, 12 deletions
diff --git a/src/fs/io/FileOutputStream.hxx b/src/fs/io/FileOutputStream.hxx index 7d30d95bb..ce53427e2 100644 --- a/src/fs/io/FileOutputStream.hxx +++ b/src/fs/io/FileOutputStream.hxx @@ -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 |