From 07ca0880354690d2cf08dbcbf935e529428b34d7 Mon Sep 17 00:00:00 2001 From: Ihor Dutchak Date: Thu, 20 Oct 2022 19:57:45 +0300 Subject: [PATCH] non-template solution attempt --- include/fmt/os.h | 33 +++++++++------------------------ src/os.cc | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/include/fmt/os.h b/include/fmt/os.h index f9cee262..01a31baf 100644 --- a/include/fmt/os.h +++ b/include/fmt/os.h @@ -379,35 +379,20 @@ struct ostream_params { # endif }; -template class file_buffer final : public detail::buffer { +class file_buffer final : public detail::buffer { file file_; - void grow(size_t) override { - if (this->size() == this->capacity()) flush(); - } + FMT_API void grow(size_t) override; public: - file_buffer(cstring_view path, const detail::ostream_params& params) - : file_(path, params.oflag) { - detail::buffer::set(new Char[params.buffer_size], params.buffer_size); - } - file_buffer(file_buffer&& other) - : detail::buffer(other.data(), other.size(), other.capacity()), - file_(std::move(other.file_)) { - other.clear(); - other.set(nullptr, 0); - } - ~file_buffer() { - flush(); - delete[] detail::buffer::data(); - } + FMT_API file_buffer(cstring_view path, const detail::ostream_params& params); + FMT_API file_buffer(file_buffer&& other); + FMT_API ~file_buffer(); void flush() { - if (detail::buffer::size() == 0) return; - file_.write( - detail::buffer::data(), - detail::buffer::size() * sizeof(detail::buffer::data()[0])); - detail::buffer::clear(); + if (size() == 0) return; + file_.write(data(), size() * sizeof(data()[0])); + clear(); } void close() { @@ -426,7 +411,7 @@ constexpr detail::buffer_size buffer_size{}; class FMT_API ostream final { private: FMT_MSC_WARNING(suppress : 4251) - detail::file_buffer buffer_; + detail::file_buffer buffer_; ostream(cstring_view path, const detail::ostream_params& params) : buffer_(path, params) {} diff --git a/src/os.cc b/src/os.cc index 06f400df..999d2a75 100644 --- a/src/os.cc +++ b/src/os.cc @@ -366,6 +366,32 @@ long getpagesize() { # endif } +FMT_BEGIN_DETAIL_NAMESPACE + +void file_buffer::grow(size_t) { + 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(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 FMT_END_NAMESPACE