Skip to content

Commit

Permalink
cleanup interface
Browse files Browse the repository at this point in the history
Signed-off-by: Timm Ruppert <[email protected]>
  • Loading branch information
TimmRuppert committed Nov 14, 2024
1 parent 94dec06 commit b4f1c35
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 89 deletions.
25 changes: 20 additions & 5 deletions include/osi-utilities/tracefile/Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <google/protobuf/message.h>

#include <filesystem>
#include <memory>
#include <optional>
#include <string>
Expand Down Expand Up @@ -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<TraceFileReader> 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<TraceFileReader> createReader(const std::filesystem::path& path);
};

} // namespace osi3
#endif
36 changes: 3 additions & 33 deletions include/osi-utilities/tracefile/Writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 <typename T>
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 <typename T>
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<std::string, std::string>& 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<TraceFileWriter> CreateTraceFileWriter(const std::string& format);

} // namespace osi3
#endif
16 changes: 15 additions & 1 deletion include/osi-utilities/tracefile/writer/MCAPTraceFileWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
bool WriteMessage(T top_level_message, const std::string& topic = "");
bool SetMetadata(const std::string& name, const std::unordered_map<std::string, std::string>& 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<std::string, std::string>& metadata_entries);

/**
* @brief Adds a new channel to the MCAP file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string>& /*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 <typename T>
bool WriteMessage(T top_level_message);

template <typename T>
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_
#endif // OSIUTILITIES_TRACEFILE_WRITER_NATIVEBINARYTRACEFILEWRITER_H_
9 changes: 6 additions & 3 deletions include/osi-utilities/tracefile/writer/TXTHTraceFileWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string>& 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 <typename T>
bool WriteMessage(T top_level_message);

private:
std::ofstream trace_file_;
bool file_open_ = false;

};

} // namespace osi3
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 14 additions & 11 deletions src/tracefile/reader/Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
#include "osi-utilities/tracefile/reader/TXTHTraceFileReader.h"
#include "osi-utilities/tracefile/reader/MCAPTraceFileReader.h"

std::unique_ptr<osi3::TraceFileReader> createTraceFileReader(const std::string& format) {
if (format == "mcap") {
return std::make_unique<osi3::MCAPTraceFileReader>();
class TraceFileFactory {
public:
static std::unique_ptr<osi3::TraceFileReader> createReader(const std::filesystem::path& path) {
if (path.extension().string() == ".osi") {
return std::make_unique<osi3::NativeBinaryTraceFileReader>();
}
if (path.extension().string() == ".mcap") {
return std::make_unique<osi3::MCAPTraceFileReader>();
}
if (path.extension().string() == ".txth") {
return std::make_unique<osi3::TXTHTraceFileReader>();
}
throw std::invalid_argument("Unsupported format: " + path.extension().string());
}
if (format == "osi") {
return std::make_unique<osi3::NativeBinaryTraceFileReader>();
}
if (format == "txth") {
return std::make_unique<osi3::TXTHTraceFileReader>();
}
throw std::invalid_argument("Unsupported format: " + format);
}
};
1 change: 0 additions & 1 deletion src/tracefile/writer/MCAPTraceFileWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
26 changes: 0 additions & 26 deletions src/tracefile/writer/Writer.cpp

This file was deleted.

0 comments on commit b4f1c35

Please sign in to comment.