mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-02 02:29:08 +00:00
direct_buffered_file -> ostream
This commit is contained in:
parent
e1bfb59619
commit
415cd51913
@ -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];
|
||||
@ -357,11 +357,18 @@ 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();
|
||||
@ -369,13 +376,14 @@ class direct_buffered_file : private detail::buffer<char> {
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user