Skip to content

Commit

Permalink
direct_buffered_file -> ostream
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jul 12, 2020
1 parent e1bfb59 commit 415cd51
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
28 changes: 18 additions & 10 deletions include/fmt/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ class file {
// Returns the memory page size.
long getpagesize();

// A buffered file with a direct buffer access and no synchronization.
class direct_buffered_file : private detail::buffer<char> {
// A fast output stream without synchronization.
class ostream : private detail::buffer<char> {
private:
file file_;
char buffer_[BUFSIZ];
Expand All @@ -357,25 +357,33 @@ class direct_buffered_file : private detail::buffer<char> {

void grow(size_t) final;

public:
direct_buffered_file(cstring_view path, int oflag)
ostream(cstring_view path, int oflag)
: buffer<char>(buffer_, 0, BUFSIZ), file_(path, oflag) {}

~direct_buffered_file() { flush(); }
public:
ostream(ostream&& other)
: buffer<char>(buffer_, 0, BUFSIZ), file_(std::move(other.file_)) {
append(other.begin(), other.end());
other.clear();
}
~ostream() { flush(); }

friend ostream output_file(cstring_view path, int oflag);

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

template <typename S, typename... Args>
friend void print(direct_buffered_file& f, const S& format_str,
const Args&... args);
void print(const S& format_str, const Args&... args) {
format_to(detail::buffer_appender<char>(*this), format_str, args...);
}
};

template <typename S, typename... Args>
void print(direct_buffered_file& f, const S& format_str, const Args&... args) {
format_to(detail::buffer_appender<char>(f), format_str, args...);
inline ostream output_file(cstring_view path,
int oflag = file::WRONLY | file::CREATE) {
return {path, oflag};
}
#endif // FMT_USE_FCNTL

Expand Down
2 changes: 1 addition & 1 deletion src/os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ long getpagesize() {
# endif
}

void direct_buffered_file::grow(size_t) {
void ostream::grow(size_t) {
if (this->size() == BUFSIZ) flush();
}
#endif // FMT_USE_FCNTL
Expand Down
16 changes: 7 additions & 9 deletions test/os-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,19 @@ TEST(BufferedFileTest, Fileno) {
EXPECT_READ(copy, FILE_CONTENT);
}

TEST(DirectBufferedFileTest, Print) {
fmt::direct_buffered_file out("test-file",
fmt::file::WRONLY | fmt::file::CREATE);
fmt::print(out, "The answer is {}.\n", 42);
TEST(OStreamTest, Print) {
fmt::ostream out = fmt::output_file("test-file");
out.print("The answer is {}.\n", 42);
out.close();
file in("test-file", file::RDONLY);
EXPECT_READ(in, "The answer is 42.\n");
}

TEST(DirectBufferedFileTest, BufferBoundary) {
TEST(OStreamTest, BufferBoundary) {
auto str = std::string(4096, 'x');
fmt::direct_buffered_file out("test-file",
fmt::file::WRONLY | fmt::file::CREATE);
fmt::print(out, "{}", str);
fmt::print(out, "{}", str);
fmt::ostream out = fmt::output_file("test-file");
out.print("{}", str);
out.print("{}", str);
out.close();
file in("test-file", file::RDONLY);
EXPECT_READ(in, str + str);
Expand Down

0 comments on commit 415cd51

Please sign in to comment.