diff --git a/include/osi-utilities/tracefile/Reader.h b/include/osi-utilities/tracefile/Reader.h index b292633..279fb88 100644 --- a/include/osi-utilities/tracefile/Reader.h +++ b/include/osi-utilities/tracefile/Reader.h @@ -8,6 +8,7 @@ #include +#include #include #include #include @@ -106,12 +107,26 @@ class TraceFileReader { virtual bool HasNext() = 0; }; -// TODO change to function which guesses on the filename endings /** - * @brief Factory function to create trace file readers based on the input file format - * @param format The format of the input file (e.g., "mcap") - * @return Unique pointer to a TraceFileReader implementation + * @brief Factory class for creating trace file readers based on file extensions */ -std::unique_ptr createTraceFileReader(const std::string& format); +class TraceFileFactory { + public: + /** + * @brief Creates a reader instance based on the file extension + * @param path Path to the trace file + * @return Unique pointer to a TraceFileReader instance + * @throws std::invalid_argument if the file extension is not supported + * + * Supported formats: + * - .osi: Native binary format (NativeBinaryTraceFileReader) + * - .mcap: MCAP format (MCAPTraceFileReader) + * - .txth: TXTH format (TXTHTraceFileReader) + * + * @note It is still required to call Open(path) on the returned reader instance + */ + static std::unique_ptr createReader(const std::filesystem::path& path); +}; + } // namespace osi3 #endif diff --git a/include/osi-utilities/tracefile/Writer.h b/include/osi-utilities/tracefile/Writer.h index 2f3d3a6..b6124ad 100644 --- a/include/osi-utilities/tracefile/Writer.h +++ b/include/osi-utilities/tracefile/Writer.h @@ -19,6 +19,9 @@ namespace osi3 { * * This class provides an interface for writing protobuf messages to trace files. * Different implementations can support various file formats like MCAP. + * + * @Note The WriteMessage() function is intentionally omitted from this base class since it is format-specific. + * Users should dynamically cast to the concrete implementation class to access the appropriate WriteMessage() function. */ class TraceFileWriter { public: @@ -47,45 +50,12 @@ class TraceFileWriter { */ virtual bool Open(const std::string& file_path) = 0; - /** - * @brief Writes a protobuf message to the file - * @tparam T Type of the protobuf message - * @param top_level_message The message to write - * @return true if successful, false otherwise - */ - template - bool WriteMessage(T top_level_message) = delete; - - /** - * @brief Writes a protobuf message to the file - * @tparam T Type of the protobuf message - * @param top_level_message The message to write - * @param topic Optional topic name for the message - * @return true if successful, false otherwise - */ - template - bool WriteMessage(T top_level_message, const std::string& topic = "") = delete; - - /** - * @brief Sets metadata for the trace file - * @param name Name of the metadata entry - * @param metadata_entries Key-value pairs of metadata - * @return true if successful, false otherwise - */ - virtual bool SetMetadata(const std::string& name, const std::unordered_map& metadata_entries) = 0; - /** * @brief Closes the trace file */ virtual void Close() = 0; }; -/** - * @brief Factory function to create trace file writers - * @param format The desired output format (e.g., "mcap") - * @return Unique pointer to a TraceFileWriter implementation - */ -std::unique_ptr CreateTraceFileWriter(const std::string& format); } // namespace osi3 #endif \ No newline at end of file diff --git a/include/osi-utilities/tracefile/writer/MCAPTraceFileWriter.h b/include/osi-utilities/tracefile/writer/MCAPTraceFileWriter.h index d766867..b657b81 100644 --- a/include/osi-utilities/tracefile/writer/MCAPTraceFileWriter.h +++ b/include/osi-utilities/tracefile/writer/MCAPTraceFileWriter.h @@ -33,9 +33,23 @@ class MCAPTraceFileWriter final : public osi3::TraceFileWriter { */ bool Open(const std::string& file_path, const mcap::McapWriterOptions& options); + /** + * @brief Writes a protobuf message to the file + * @tparam T Type of the protobuf message + * @param top_level_message The message to write + * @param topic Optional topic name for the message + * @return true if successful, false otherwise + */ template bool WriteMessage(T top_level_message, const std::string& topic = ""); - bool SetMetadata(const std::string& name, const std::unordered_map& metadata_entries) override; + + /** + * @brief Sets metadata for the trace file + * @param name Name of the metadata entry + * @param metadata_entries Key-value pairs of metadata + * @return true if successful, false otherwise + */ + bool SetMetadata(const std::string& name, const std::unordered_map& metadata_entries); /** * @brief Adds a new channel to the MCAP file diff --git a/include/osi-utilities/tracefile/writer/NativeBinaryTraceFileWriter.h b/include/osi-utilities/tracefile/writer/NativeBinaryTraceFileWriter.h index 88b81ec..8132924 100644 --- a/include/osi-utilities/tracefile/writer/NativeBinaryTraceFileWriter.h +++ b/include/osi-utilities/tracefile/writer/NativeBinaryTraceFileWriter.h @@ -23,20 +23,19 @@ class NativeBinaryTraceFileWriter final : public TraceFileWriter { bool Open(const std::string& file_path) override; void Close() override; - bool SetMetadata(const std::string& /*name*/, const std::unordered_map& /*metadata_entries*/) override { return true; } - + /** + * @brief Writes a protobuf message to the file + * @tparam T Type of the protobuf message + * @param top_level_message The message to write + * @return true if successful, false otherwise + */ template bool WriteMessage(T top_level_message); - template - static bool WriteMessage(T /*top_level_message*/, const std::string& /*topic*/) { - return false; - } - private: std::ofstream trace_file_; bool file_open_ = false; }; } // namespace osi3 -#endif // OSIUTILITIES_TRACEFILE_WRITER_NATIVEBINARYTRACEFILEWRITER_H_ \ No newline at end of file +#endif // OSIUTILITIES_TRACEFILE_WRITER_NATIVEBINARYTRACEFILEWRITER_H_ \ No newline at end of file diff --git a/include/osi-utilities/tracefile/writer/TXTHTraceFileWriter.h b/include/osi-utilities/tracefile/writer/TXTHTraceFileWriter.h index 8b9cbc1..36f8ca4 100644 --- a/include/osi-utilities/tracefile/writer/TXTHTraceFileWriter.h +++ b/include/osi-utilities/tracefile/writer/TXTHTraceFileWriter.h @@ -23,15 +23,18 @@ class TXTHTraceFileWriter final : public TraceFileWriter { bool Open(const std::string& file_path) override; void Close() override; - bool SetMetadata(const std::string& name, const std::unordered_map& metadata_entries) override { return false; } - + /** + * @brief Writes a protobuf message to the file + * @tparam T Type of the protobuf message + * @param top_level_message The message to write + * @return true if successful, false otherwise + */ template bool WriteMessage(T top_level_message); private: std::ofstream trace_file_; bool file_open_ = false; - }; } // namespace osi3 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 722775a..2816e94 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,7 +7,6 @@ option(BUILD_TESTING "Build the testing suite" ON) # specify library source files set(OSIUtilities_SRCS tracefile/reader/Reader.cpp - tracefile/writer/Writer.cpp tracefile/MCAPImplementation.cpp tracefile/reader/NativeBinaryTraceFileReader.cpp tracefile/writer/NativeBinaryTraceFileWriter.cpp diff --git a/src/tracefile/reader/Reader.cpp b/src/tracefile/reader/Reader.cpp index 7d4aa9a..6f83da9 100644 --- a/src/tracefile/reader/Reader.cpp +++ b/src/tracefile/reader/Reader.cpp @@ -9,15 +9,18 @@ #include "osi-utilities/tracefile/reader/TXTHTraceFileReader.h" #include "osi-utilities/tracefile/reader/MCAPTraceFileReader.h" -std::unique_ptr createTraceFileReader(const std::string& format) { - if (format == "mcap") { - return std::make_unique(); +class TraceFileFactory { +public: + static std::unique_ptr createReader(const std::filesystem::path& path) { + if (path.extension().string() == ".osi") { + return std::make_unique(); + } + if (path.extension().string() == ".mcap") { + return std::make_unique(); + } + if (path.extension().string() == ".txth") { + return std::make_unique(); + } + throw std::invalid_argument("Unsupported format: " + path.extension().string()); } - if (format == "osi") { - return std::make_unique(); - } - if (format == "txth") { - return std::make_unique(); - } - throw std::invalid_argument("Unsupported format: " + format); -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/tracefile/writer/MCAPTraceFileWriter.cpp b/src/tracefile/writer/MCAPTraceFileWriter.cpp index 6544ee4..0c6e6dd 100644 --- a/src/tracefile/writer/MCAPTraceFileWriter.cpp +++ b/src/tracefile/writer/MCAPTraceFileWriter.cpp @@ -96,7 +96,6 @@ bool MCAPTraceFileWriter::WriteMessage(T top_level_message, const std::string& t std::cerr << "Error: Failed to write message " << status.message; return false; } - std::cout << "Wrote message with timestamp: " << msg.logTime << std::endl; // todo remove debug print return true; } diff --git a/src/tracefile/writer/Writer.cpp b/src/tracefile/writer/Writer.cpp deleted file mode 100644 index f54d0dc..0000000 --- a/src/tracefile/writer/Writer.cpp +++ /dev/null @@ -1,26 +0,0 @@ -// -// Copyright (c) 2024, Bayerische Motoren Werke Aktiengesellschaft (BMW AG) -// SPDX-License-Identifier: MPL-2.0 -// - -#include "osi-utilities/tracefile/Writer.h" - -#include "osi-utilities/tracefile/writer/MCAPTraceFileWriter.h" -#include "osi-utilities/tracefile/writer/NativeBinaryTraceFileWriter.h" -#include "osi-utilities/tracefile/writer/TXTHTraceFileWriter.h" - -// todo use enum or something else instead of a string -// todo for the writer use mcap as default if the "string" is empty -// todo makes a writer factory really sense? -std::unique_ptr CreateTraceFileWriter(const std::string& format) { - if (format == "mcap") { - return std::make_unique(); - } - if (format == "osi") { - return std::make_unique(); - } - if (format == "txth") { - return std::make_unique(); - } - throw std::invalid_argument("Unsupported format: " + format); -} \ No newline at end of file