FileWrapper[Impl] modifications and actually remove the "Impl" class.
This is a somewhat involved refactoring of this class. Here's an overview of the changes:
* FileWrapper can now be used as a regular class and instances allocated on the stack.
* The type now has support for move semantics and copy isn't allowed.
* New public ctor with FILE* that can be used instead of OpenFromFileHandle.
* New static Open() method. The intent of this is to allow opening a file and getting back a FileWrapper instance. Using this method instead of Create(), will allow us in the future to make the FILE* member pointer, to be const and simplify threading (get rid of the lock).
* Rename the Open() method to is_open() and make it inline.
* The FileWrapper interface is no longer a pure virtual interface. There's only one implementation so there's no need to go through a vtable for everything.
* Functionality offered by the class, is now reduced. No support for looping (not clear if that was actually useful to users of that flag), no need to implement the 'read_only_' functionality in the class, since file APIs implement that already, no support for *not* managing the file handle (this wasn't used). OpenFromFileHandle always "manages" the file.
* Delete the unused WriteText() method and don't support opening files in text mode. Text mode is only different on Windows and on Windows it translates \n to \r\n, which means that files such as log files, could have a slightly different format on Windows than other platforms. Besides, tools on Windows can handle UNIX line endings.
* Remove FileName(), change Trace code to manage its own path.
* Rename id_ member variable to file_.
* Removed the open_ member variable since the same functionality can be gotten from just checking the file pointer.
* Don't call CloseFile inside of Write. Write shouldn't be changing the state of the class beyond just attempting to write.
* Remove concept of looping from FileWrapper and never close inside of Read()
* Changed stream base classes to inherit from a common base class instead of both defining the Rewind method. Ultimately, Id' like to remove these interfaces and just have FileWrapper.
* Remove read_only param from OpenFromFileHandle
* Renamed size_in_bytes_ to position_, since it gets set to 0 when Rewind() is called (and the size actually does not change).
* Switch out rw lock for CriticalSection. The r/w lock was only used for reading when checking the open_ flag.
BUG=
Review-Url: https://codereview.webrtc.org/2054373002
Cr-Commit-Position: refs/heads/master@{#13155}
diff --git a/webrtc/system_wrappers/include/file_wrapper.h b/webrtc/system_wrappers/include/file_wrapper.h
index b32a62f..a1d899c 100644
--- a/webrtc/system_wrappers/include/file_wrapper.h
+++ b/webrtc/system_wrappers/include/file_wrapper.h
@@ -14,6 +14,7 @@
#include <stddef.h>
#include <stdio.h>
+#include "webrtc/base/criticalsection.h"
#include "webrtc/common_types.h"
#include "webrtc/typedefs.h"
@@ -22,55 +23,64 @@
namespace webrtc {
+// TODO(tommi): Remove the base classes, rename to rtc::File and move to base.
class FileWrapper : public InStream, public OutStream {
public:
static const size_t kMaxFileNameSize = 1024;
- // Factory method. Constructor disabled.
+ // Factory methods.
+ // TODO(tommi): Remove Create().
static FileWrapper* Create();
+ static FileWrapper Open(const char* file_name_utf8, bool read_only);
+
+ FileWrapper(FILE* file, size_t max_size);
+ ~FileWrapper() override;
+
+ // Support for move semantics.
+ FileWrapper(FileWrapper&& other);
+ FileWrapper& operator=(FileWrapper&& other);
// Returns true if a file has been opened.
- virtual bool Open() const = 0;
+ bool is_open() const { return file_ != nullptr; }
// Opens a file in read or write mode, decided by the read_only parameter.
- virtual int OpenFile(const char* file_name_utf8,
- bool read_only,
- bool loop = false,
- bool text = false) = 0;
+ bool OpenFile(const char* file_name_utf8, bool read_only);
- // Initializes the wrapper from an existing handle. |read_only| must match in
- // the mode the file was opened in. If |manage_file| is true, the wrapper
+ // Initializes the wrapper from an existing handle. The wrapper
// takes ownership of |handle| and closes it in CloseFile().
- virtual int OpenFromFileHandle(FILE* handle,
- bool manage_file,
- bool read_only,
- bool loop = false) = 0;
+ bool OpenFromFileHandle(FILE* handle);
- virtual int CloseFile() = 0;
+ void CloseFile();
// Limits the file size to |bytes|. Writing will fail after the cap
// is hit. Pass zero to use an unlimited size.
- virtual int SetMaxFileSize(size_t bytes) = 0;
+ // TODO(tommi): Could we move this out into a separate class?
+ void SetMaxFileSize(size_t bytes);
- // Flush any pending writes.
- virtual int Flush() = 0;
+ // Flush any pending writes. Note: Flushing when closing, is not required.
+ int Flush();
- // Returns the opened file's name in |file_name_utf8|. Provide the size of
- // the buffer in bytes in |size|. The name will be truncated if |size| is
- // too small.
- virtual int FileName(char* file_name_utf8,
- size_t size) const = 0;
-
- // Write |format| to the opened file. Arguments are taken in the same manner
- // as printf. That is, supply a format string containing text and
- // specifiers. Returns the number of characters written or -1 on error.
- virtual int WriteText(const char* format, ...) = 0;
-
- // Inherited from both Instream and OutStream.
- // Rewinds the file to the start. Only available when OpenFile() has been
- // called with |loop| == true or |readOnly| == true.
- // virtual int Rewind() = 0;
+ // Rewinds the file to the start.
int Rewind() override;
+ int Read(void* buf, size_t length) override;
+ bool Write(const void* buf, size_t length) override;
+
+ private:
+ FileWrapper();
+
+ void CloseFileImpl();
+ int FlushImpl();
+
+ // TODO(tommi): Remove the lock.
+ rtc::CriticalSection lock_;
+
+ FILE* file_ = nullptr;
+ size_t position_ = 0;
+ size_t max_size_in_bytes_ = 0;
+
+ // Copying is not supported.
+ FileWrapper(const FileWrapper&) = delete;
+ FileWrapper& operator=(const FileWrapper&) = delete;
};
} // namespace webrtc