forked from fmtlib/fmt
fmt::ostream - aggregate buffer instead of inheriting it (#3139)
Some MSVC-specific behavior: When class fmt::ostream inherits detail::buffer - the last gets implicitly exported when fmt is built as a shared library. Unless os.h is included, the compiler assumes detail::buffer is not externally exported and instantiates a local copy of it, which causes ODR violation. With aggregation - there is no extra exporting of detail::buffer symbols.
This commit is contained in:
@ -379,6 +379,28 @@ struct ostream_params {
|
|||||||
# endif
|
# endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class file_buffer final : public buffer<char> {
|
||||||
|
file file_;
|
||||||
|
|
||||||
|
FMT_API void grow(size_t) override;
|
||||||
|
|
||||||
|
public:
|
||||||
|
FMT_API file_buffer(cstring_view path, const ostream_params& params);
|
||||||
|
FMT_API file_buffer(file_buffer&& other);
|
||||||
|
FMT_API ~file_buffer();
|
||||||
|
|
||||||
|
void flush() {
|
||||||
|
if (size() == 0) return;
|
||||||
|
file_.write(data(), size() * sizeof(data()[0]));
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void close() {
|
||||||
|
flush();
|
||||||
|
file_.close();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
FMT_END_DETAIL_NAMESPACE
|
FMT_END_DETAIL_NAMESPACE
|
||||||
|
|
||||||
// Added {} below to work around default constructor error known to
|
// Added {} below to work around default constructor error known to
|
||||||
@ -386,49 +408,32 @@ FMT_END_DETAIL_NAMESPACE
|
|||||||
constexpr detail::buffer_size buffer_size{};
|
constexpr detail::buffer_size buffer_size{};
|
||||||
|
|
||||||
/** A fast output stream which is not thread-safe. */
|
/** A fast output stream which is not thread-safe. */
|
||||||
class FMT_API ostream final : private detail::buffer<char> {
|
class FMT_API ostream {
|
||||||
private:
|
private:
|
||||||
file file_;
|
FMT_MSC_WARNING(suppress : 4251)
|
||||||
|
detail::file_buffer buffer_;
|
||||||
void grow(size_t) override;
|
|
||||||
|
|
||||||
ostream(cstring_view path, const detail::ostream_params& params)
|
ostream(cstring_view path, const detail::ostream_params& params)
|
||||||
: file_(path, params.oflag) {
|
: buffer_(path, params) {}
|
||||||
set(new char[params.buffer_size], params.buffer_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ostream(ostream&& other)
|
ostream(ostream&& other) : buffer_(std::move(other.buffer_)) {}
|
||||||
: detail::buffer<char>(other.data(), other.size(), other.capacity()),
|
|
||||||
file_(std::move(other.file_)) {
|
|
||||||
other.clear();
|
|
||||||
other.set(nullptr, 0);
|
|
||||||
}
|
|
||||||
~ostream() {
|
|
||||||
flush();
|
|
||||||
delete[] data();
|
|
||||||
}
|
|
||||||
|
|
||||||
void flush() {
|
~ostream();
|
||||||
if (size() == 0) return;
|
|
||||||
file_.write(data(), size());
|
void flush() { buffer_.flush(); }
|
||||||
clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename... T>
|
template <typename... T>
|
||||||
friend ostream output_file(cstring_view path, T... params);
|
friend ostream output_file(cstring_view path, T... params);
|
||||||
|
|
||||||
void close() {
|
void close() { buffer_.close(); }
|
||||||
flush();
|
|
||||||
file_.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(detail::buffer_appender<char>(*this), fmt,
|
vformat_to(detail::buffer_appender<char>(buffer_), fmt,
|
||||||
fmt::make_format_args(args...));
|
fmt::make_format_args(args...));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
26
src/os.cc
26
src/os.cc
@ -366,8 +366,32 @@ long getpagesize() {
|
|||||||
# endif
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
FMT_API void ostream::grow(size_t) {
|
FMT_BEGIN_DETAIL_NAMESPACE
|
||||||
|
|
||||||
|
void file_buffer::grow(size_t) {
|
||||||
if (this->size() == this->capacity()) flush();
|
if (this->size() == this->capacity()) flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file_buffer::file_buffer(cstring_view path,
|
||||||
|
const detail::ostream_params& params)
|
||||||
|
: file_(path, params.oflag) {
|
||||||
|
set(new char[params.buffer_size], params.buffer_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
file_buffer::file_buffer(file_buffer&& other)
|
||||||
|
: detail::buffer<char>(other.data(), other.size(), other.capacity()),
|
||||||
|
file_(std::move(other.file_)) {
|
||||||
|
other.clear();
|
||||||
|
other.set(nullptr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
file_buffer::~file_buffer() {
|
||||||
|
flush();
|
||||||
|
delete[] data();
|
||||||
|
}
|
||||||
|
|
||||||
|
FMT_END_DETAIL_NAMESPACE
|
||||||
|
|
||||||
|
ostream::~ostream() = default;
|
||||||
#endif // FMT_USE_FCNTL
|
#endif // FMT_USE_FCNTL
|
||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
|
Reference in New Issue
Block a user