diff --git a/cpp/mcap/conanfile.py b/cpp/mcap/conanfile.py index 0c9d7b827..4b91e6d42 100644 --- a/cpp/mcap/conanfile.py +++ b/cpp/mcap/conanfile.py @@ -1,4 +1,4 @@ -from conans import ConanFile, tools +from conan import ConanFile, tools class McapConan(ConanFile): diff --git a/cpp/mcap/include/mcap/writer.hpp b/cpp/mcap/include/mcap/writer.hpp index f18da5e33..2af6edd5b 100644 --- a/cpp/mcap/include/mcap/writer.hpp +++ b/cpp/mcap/include/mcap/writer.hpp @@ -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; @@ -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: @@ -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: @@ -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()`. @@ -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; }; diff --git a/cpp/mcap/include/mcap/writer.inl b/cpp/mcap/include/mcap/writer.inl index 2ae3e6dba..dd9b82cb9 100644 --- a/cpp/mcap/include/mcap/writer.inl +++ b/cpp/mcap/include/mcap/writer.inl @@ -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_); @@ -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_; } @@ -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; }