Skip to content

Commit

Permalink
cpp: add flush to IWritable
Browse files Browse the repository at this point in the history
  • Loading branch information
james-rms committed Nov 7, 2024
1 parent ba3c9c0 commit 570e29c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cpp/mcap/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from conans import ConanFile, tools
from conan import ConanFile, tools


class McapConan(ConanFile):
Expand Down
12 changes: 12 additions & 0 deletions cpp/mcap/include/mcap/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ class MCAP_PUBLIC IWritable {
*/
void resetCrc();

/**
* @brief flushes any buffered data to the output. Defaults to a no-op.
*/
virtual void flush() = 0;

protected:
virtual void handleWrite(const std::byte* data, uint64_t size) = 0;

Expand All @@ -162,6 +167,7 @@ class MCAP_PUBLIC FileWriter final : public IWritable {

void handleWrite(const std::byte* data, uint64_t size) override;
void end() override;
void flush() override;
uint64_t size() const override;

private:
Expand All @@ -179,6 +185,7 @@ class MCAP_PUBLIC StreamWriter final : public IWritable {

void handleWrite(const std::byte* data, uint64_t size) override;
void end() override;
void flush() override;
uint64_t size() const override;

private:
Expand All @@ -205,6 +212,7 @@ class MCAP_PUBLIC IChunkWriter : public IWritable {
* @brief Returns the size in bytes of the uncompressed data.
*/
virtual uint64_t size() const override = 0;

/**
* @brief Returns the size in bytes of the compressed data. This will only be
* called after `end()`.
Expand All @@ -230,6 +238,10 @@ class MCAP_PUBLIC IChunkWriter : public IWritable {
*/
virtual const std::byte* compressedData() const = 0;

// No-op IWritable::flush() implementation. Chunk writers have no concept of an "underlying
// stream" to flush to.
void flush() override {}

protected:
virtual void handleClear() = 0;
};
Expand Down
13 changes: 12 additions & 1 deletion cpp/mcap/include/mcap/writer.inl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ void FileWriter::handleWrite(const std::byte* data, uint64_t size) {
size_ += size;
}

void FileWriter::flush() {
if (file_) {
std::fflush(file_);
}
}

void FileWriter::end() {
if (file_) {
std::fclose(file_);
Expand All @@ -84,10 +90,14 @@ void StreamWriter::handleWrite(const std::byte* data, uint64_t size) {
size_ += size;
}

void StreamWriter::end() {
void StreamWriter::flush() {
stream_.flush();
}

void StreamWriter::end() {
flush();
}

uint64_t StreamWriter::size() const {
return size_;
}
Expand Down Expand Up @@ -921,6 +931,7 @@ uint64_t McapWriter::write(IWritable& output, const Chunk& chunk) {
write(output, chunk.compression);
write(output, chunk.compressedSize);
write(output, chunk.records, chunk.compressedSize);
output.flush();

return 9 + recordSize;
}
Expand Down

0 comments on commit 570e29c

Please sign in to comment.