Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt::ostream - aggregate buffer instead of inheriting it #3139

Merged
merged 6 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions include/fmt/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,32 +379,23 @@ struct ostream_params {
# endif
};

FMT_END_DETAIL_NAMESPACE

// Added {} below to work around default constructor error known to
// occur in Xcode versions 7.2.1 and 8.2.1.
constexpr detail::buffer_size buffer_size{};

/** A fast output stream which is not thread-safe. */
class FMT_API ostream final : private detail::buffer<char> {
private:
class ostream_buffer final : public detail::buffer<char> {
file file_;

void grow(size_t) override;
FMT_API void grow(size_t) override;
Youw marked this conversation as resolved.
Show resolved Hide resolved

ostream(cstring_view path, const detail::ostream_params& params)
public:
ostream_buffer(cstring_view path, const detail::ostream_params& params)
: file_(path, params.oflag) {
set(new char[params.buffer_size], params.buffer_size);
}

public:
ostream(ostream&& other)
ostream_buffer(ostream_buffer&& other)
: detail::buffer<char>(other.data(), other.size(), other.capacity()),
file_(std::move(other.file_)) {
other.clear();
other.set(nullptr, 0);
}
~ostream() {
~ostream_buffer() {
flush();
delete[] data();
}
Expand All @@ -415,20 +406,45 @@ class FMT_API ostream final : private detail::buffer<char> {
clear();
}

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

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

FMT_END_DETAIL_NAMESPACE

// Added {} below to work around default constructor error known to
// occur in Xcode versions 7.2.1 and 8.2.1.
constexpr detail::buffer_size buffer_size{};

/** A fast output stream which is not thread-safe. */
class FMT_API ostream final {
Youw marked this conversation as resolved.
Show resolved Hide resolved
private:
FMT_MSC_WARNING(suppress : 4251)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

Copy link
Contributor Author

@Youw Youw Oct 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning C4251: class 'detail::file_buffer' needs to have dll-interface ...

I.e. MSVC doesn't like that ostream is exported and detail::file_buffer - is not

detail::ostream_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 ostream output_file(cstring_view path, T... params);

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(detail::buffer_appender<char>(*this), fmt,
vformat_to(detail::buffer_appender<char>(buffer_), fmt,
fmt::make_format_args(args...));
}
};
Expand Down
4 changes: 3 additions & 1 deletion src/os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,10 @@ long getpagesize() {
# endif
}

FMT_API void ostream::grow(size_t) {
FMT_API void detail::ostream_buffer::grow(size_t) {
if (this->size() == this->capacity()) flush();
}

Youw marked this conversation as resolved.
Show resolved Hide resolved
ostream::~ostream() = default;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need to be outlined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it guess that's my old habit - always anchor class by its d-tor
otherwise - how would compiler know where to place definition of functions marked with FMT_API?

maybe that is not the case for this particular class - but that's the case for polymorphic classes
at least I didn't check if it would behave fine w/o it

#endif // FMT_USE_FCNTL
FMT_END_NAMESPACE