Simplify ostream

This commit is contained in:
Victor Zverovich
2020-08-04 19:39:44 -07:00
parent 5413713c95
commit ea76933802
2 changed files with 10 additions and 15 deletions

View File

@@ -18,7 +18,6 @@
#include <cstddef> #include <cstddef>
#include <cstdio> #include <cstdio>
#include <cstdlib> // for strtod_l #include <cstdlib> // for strtod_l
#include <memory>
#if defined __APPLE__ || defined(__FreeBSD__) #if defined __APPLE__ || defined(__FreeBSD__)
# include <xlocale.h> // for LC_NUMERIC_MASK on OS X # include <xlocale.h> // for LC_NUMERIC_MASK on OS X
@@ -380,32 +379,28 @@ static constexpr detail::buffer_size buffer_size;
class ostream : private detail::buffer<char> { class ostream : private detail::buffer<char> {
private: private:
file file_; file file_;
size_t buffer_size_;
std::unique_ptr<char[]> buffer_;
void flush() { void flush() {
if (size() == 0) return; if (size() == 0) return;
file_.write(buffer_.get(), size()); file_.write(data(), size());
clear(); clear();
} }
void grow(size_t) final; void grow(size_t) final;
ostream(cstring_view path, const detail::ostream_params& params) ostream(cstring_view path, const detail::ostream_params& params)
: file_(path, params.oflag), : file_(path, params.oflag) {
buffer_size_(params.buffer_size), set(new char[params.buffer_size], params.buffer_size);
buffer_(new char[params.buffer_size]) {
set(buffer_.get(), params.buffer_size);
} }
public: public:
ostream(ostream&& other) ostream(ostream&& other) : file_(std::move(other.file_)) {
: file_(std::move(other.file_)), other.set(nullptr, 0);
buffer_size_(other.buffer_size_), }
buffer_(std::move(other.buffer_)) { ~ostream() {
other.clear(); flush();
delete[] data();
} }
~ostream() { flush(); }
template <typename... T> template <typename... T>
friend ostream output_file(cstring_view path, T... params); friend ostream output_file(cstring_view path, T... params);

View File

@@ -315,7 +315,7 @@ long getpagesize() {
} }
void ostream::grow(size_t) { void ostream::grow(size_t) {
if (this->size() == buffer_size_) flush(); if (this->size() == this->capacity()) flush();
} }
#endif // FMT_USE_FCNTL #endif // FMT_USE_FCNTL
FMT_END_NAMESPACE FMT_END_NAMESPACE