mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-28 12:51:03 +00:00
Simplify ostream
This commit is contained in:
parent
fb07b37c5b
commit
020af729dd
@ -358,16 +358,23 @@ struct ostream_params {
|
|||||||
# endif
|
# 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:
|
private:
|
||||||
file file_;
|
file file_;
|
||||||
|
|
||||||
FMT_API static void grow(buffer<char>& buf, size_t);
|
ostream(cstring_view path, const detail::ostream_params& params);
|
||||||
|
|
||||||
|
static void grow(buffer<char>& buf, size_t);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FMT_API file_buffer(cstring_view path, const ostream_params& params);
|
ostream(ostream&& other) noexcept;
|
||||||
FMT_API file_buffer(file_buffer&& other) noexcept;
|
~ostream();
|
||||||
FMT_API ~file_buffer();
|
|
||||||
|
|
||||||
void flush() {
|
void flush() {
|
||||||
if (size() == 0) return;
|
if (size() == 0) return;
|
||||||
@ -375,42 +382,18 @@ class file_buffer final : public buffer<char> {
|
|||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
friend auto output_file(cstring_view path, T... params) -> ostream;
|
||||||
|
|
||||||
void close() {
|
void close() {
|
||||||
flush();
|
flush();
|
||||||
file_.close();
|
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
|
/// Formats `args` according to specifications in `fmt` and writes the
|
||||||
/// output to the file.
|
/// output to the file.
|
||||||
template <typename... T> void print(format_string<T...> fmt, T&&... args) {
|
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...));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
15
src/os.cc
15
src/os.cc
@ -374,30 +374,25 @@ long getpagesize() {
|
|||||||
}
|
}
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
namespace detail {
|
void ostream::grow(buffer<char>& buf, size_t) {
|
||||||
|
if (buf.size() == buf.capacity()) static_cast<ostream&>(buf).flush();
|
||||||
void file_buffer::grow(buffer<char>& buf, size_t) {
|
|
||||||
if (buf.size() == buf.capacity()) static_cast<file_buffer&>(buf).flush();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file_buffer::file_buffer(cstring_view path, const ostream_params& params)
|
ostream::ostream(cstring_view path, const detail::ostream_params& params)
|
||||||
: buffer<char>(grow), file_(path, params.oflag) {
|
: buffer<char>(grow), file_(path, params.oflag) {
|
||||||
set(new char[params.buffer_size], params.buffer_size);
|
set(new char[params.buffer_size], params.buffer_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
file_buffer::file_buffer(file_buffer&& other) noexcept
|
ostream::ostream(ostream&& other) noexcept
|
||||||
: buffer<char>(grow, other.data(), other.size(), other.capacity()),
|
: buffer<char>(grow, other.data(), other.size(), other.capacity()),
|
||||||
file_(std::move(other.file_)) {
|
file_(std::move(other.file_)) {
|
||||||
other.clear();
|
other.clear();
|
||||||
other.set(nullptr, 0);
|
other.set(nullptr, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
file_buffer::~file_buffer() {
|
ostream::~ostream() {
|
||||||
flush();
|
flush();
|
||||||
delete[] data();
|
delete[] data();
|
||||||
}
|
}
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
ostream::~ostream() = default;
|
|
||||||
#endif // FMT_USE_FCNTL
|
#endif // FMT_USE_FCNTL
|
||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
|
Loading…
Reference in New Issue
Block a user