proxygen
folly::gen::detail::FileWriter Class Reference

#include <File-inl.h>

Inheritance diagram for folly::gen::detail::FileWriter:
folly::gen::Operator< FileWriter > folly::gen::FBounded< Self >

Public Member Functions

 FileWriter (File file, std::unique_ptr< IOBuf > buffer)
 
template<class Source , class Value >
void compose (const GenImpl< Value, Source > &source) const
 
- Public Member Functions inherited from folly::gen::Operator< FileWriter >
ResultGen compose (const GenImpl< Value, Source > &source) const
 
- Public Member Functions inherited from folly::gen::FBounded< Self >
const Self & self () const
 
Self & self ()
 

Private Member Functions

void write (ByteRange v) const
 
void flushBuffer () const
 

Private Attributes

File file_
 
std::unique_ptr< IOBufbuffer_
 

Additional Inherited Members

- Protected Member Functions inherited from folly::gen::Operator< FileWriter >
 Operator ()=default
 
 Operator (Operator &&) noexcept=default
 
 Operator (const Operator &)=default
 
Operatoroperator= (Operator &&) noexcept=default
 
Operatoroperator= (const Operator &)=default
 

Detailed Description

Definition at line 64 of file File-inl.h.

Constructor & Destructor Documentation

folly::gen::detail::FileWriter::FileWriter ( File  file,
std::unique_ptr< IOBuf buffer 
)
inline

Definition at line 66 of file File-inl.h.

References folly::gen::detail::FileReader::buffer_.

67  : file_(std::move(file)), buffer_(std::move(buffer)) {
68  if (buffer_) {
69  buffer_->clear();
70  }
71  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< IOBuf > buffer_
Definition: File-inl.h:118

Member Function Documentation

template<class Source , class Value >
void folly::gen::detail::FileWriter::compose ( const GenImpl< Value, Source > &  source) const
inline

Definition at line 74 of file File-inl.h.

References folly::gen::detail::FileReader::buffer_, folly::File::close(), folly::gen::detail::FileReader::file_, folly::gen::GenImpl< Value, Self >::foreach(), folly::gen::move, v, and fizz::detail::write().

74  {
75  auto fn = [&](ByteRange v) {
76  if (!this->buffer_ || v.size() >= this->buffer_->capacity()) {
77  this->flushBuffer();
78  this->write(v);
79  } else {
80  if (v.size() > this->buffer_->tailroom()) {
81  this->flushBuffer();
82  }
83  memcpy(this->buffer_->writableTail(), v.data(), v.size());
84  this->buffer_->append(v.size());
85  }
86  };
87 
88  // Iterate
89  source.foreach(std::move(fn));
90 
91  flushBuffer();
92  file_.close();
93  }
auto v
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void write(ByteRange v) const
Definition: File-inl.h:96
void close()
Definition: File.cpp:117
std::unique_ptr< IOBuf > buffer_
Definition: File-inl.h:118
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
void folly::gen::detail::FileWriter::flushBuffer ( ) const
inlineprivate

Definition at line 110 of file File-inl.h.

References folly::gen::detail::FileReader::buffer_, and fizz::detail::write().

110  {
111  if (buffer_ && buffer_->length() != 0) {
112  write(ByteRange(buffer_->data(), buffer_->length()));
113  buffer_->clear();
114  }
115  }
void write(ByteRange v) const
Definition: File-inl.h:96
std::unique_ptr< IOBuf > buffer_
Definition: File-inl.h:118
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
void folly::gen::detail::FileWriter::write ( ByteRange  v) const
inlineprivate

Definition at line 96 of file File-inl.h.

References folly::Range< Iter >::advance(), folly::Range< Iter >::data(), folly::Range< Iter >::empty(), folly::File::fd(), folly::gen::detail::FileReader::file_, folly::Range< Iter >::size(), and fizz::detail::write().

96  {
97  ssize_t n;
98  while (!v.empty()) {
99  do {
100  n = ::write(file_.fd(), v.data(), v.size());
101  } while (n == -1 && errno == EINTR);
102  if (n == -1) {
103  throw std::system_error(
104  errno, std::system_category(), "write() failed");
105  }
106  v.advance(size_t(n));
107  }
108  }
void write(ByteRange v) const
Definition: File-inl.h:96
int fd() const
Definition: File.h:85

Member Data Documentation

std::unique_ptr<IOBuf> folly::gen::detail::FileWriter::buffer_
private

Definition at line 118 of file File-inl.h.

File folly::gen::detail::FileWriter::file_
mutableprivate

Definition at line 117 of file File-inl.h.


The documentation for this class was generated from the following file: