Skip to content

Commit

Permalink
Simplify ostream
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Aug 17, 2024
1 parent fb07b37 commit e3adc4b
Showing 1 changed file with 15 additions and 32 deletions.
47 changes: 15 additions & 32 deletions include/fmt/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,59 +358,42 @@ struct ostream_params {
# endif
};

class file_buffer final : public buffer<char> {
} // namespace detail

FMT_INLINE_VARIABLE constexpr auto buffer_size = detail::buffer_size();

/// A fast buffered output stream for writing from a single thread. Writing from
/// multiple threads without external synchronization may result in a data race.
class FMT_API ostream : private detail::buffer<char> {
private:
file file_;

ostream(cstring_view path, const detail::ostream_params& params);

FMT_API static void grow(buffer<char>& buf, size_t);

public:
FMT_API file_buffer(cstring_view path, const ostream_params& params);
FMT_API file_buffer(file_buffer&& other) noexcept;
FMT_API ~file_buffer();
ostream(ostream&& other) noexcept;
~ostream();

void flush() {
if (size() == 0) return;
file_.write(data(), size() * sizeof(data()[0]));
clear();
}

template <typename... T>
friend auto output_file(cstring_view path, T... params) -> ostream;

void close() {
flush();
file_.close();
}
};

} // namespace detail

FMT_INLINE_VARIABLE constexpr auto buffer_size = detail::buffer_size();

/// A fast output stream for writing from a single thread. Writing from
/// multiple threads without external synchronization may result in a data race.
class FMT_API ostream {
private:
FMT_MSC_WARNING(suppress : 4251)
detail::file_buffer buffer_;

ostream(cstring_view path, const detail::ostream_params& params)
: buffer_(path, params) {}

public:
ostream(ostream&& other) : buffer_(std::move(other.buffer_)) {}

~ostream();

void flush() { buffer_.flush(); }

template <typename... T>
friend auto output_file(cstring_view path, T... params) -> ostream;

void close() { buffer_.close(); }

/// Formats `args` according to specifications in `fmt` and writes the
/// output to the file.
template <typename... T> void print(format_string<T...> fmt, T&&... args) {
vformat_to(appender(buffer_), fmt, fmt::make_format_args(args...));
vformat_to(appender(*this), fmt, fmt::make_format_args(args...));
}
};

Expand Down

0 comments on commit e3adc4b

Please sign in to comment.