Skip to content

Commit

Permalink
move parser to private section
Browse files Browse the repository at this point in the history
Signed-off-by: Timm Ruppert <[email protected]>
  • Loading branch information
TimmRuppert committed Oct 29, 2024
1 parent 411458e commit e70f64a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 76 deletions.
38 changes: 38 additions & 0 deletions include/osi-utilities/tracefile/NativeBinaryTraceFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
#include <fstream>
#include <functional>

#include "osi_groundtruth.pb.h"
#include "osi_sensordata.pb.h"
#include "osi_sensorview.pb.h"
#include "osi_sensorviewconfiguration.pb.h"
#include "osi_hostvehicledata.pb.h"
#include "osi_trafficcommand.pb.h"
#include "osi_trafficcommandupdate.pb.h"
#include "osi_trafficupdate.pb.h"
#include "osi_motionrequest.pb.h"
#include "osi_streamingupdate.pb.h"


namespace osi3 {

/**
Expand Down Expand Up @@ -52,6 +64,32 @@ class NativeBinaryTraceFileReader final : public osi3::TraceFileReader {
* @return Vector containing the raw message bytes
*/
std::vector<char> ReadNextMessageFromFile();

template<typename T>
std::unique_ptr<google::protobuf::Message> ParseMessage(const std::string& data) {
auto msg = std::make_unique<T>();
if (!msg->ParseFromArray(data.data(), static_cast<int>(data.size()))) {
throw std::runtime_error("Failed to parse message");
}
return std::move(msg);
}

template<typename T>
MessageParserFunc CreateParser() {
return [this](const std::vector<char>& data) { return ParseMessage<T>(data); };
}
const std::unordered_map<ReaderTopLevelMessage, MessageParserFunc> kParserMap_ = {
{ReaderTopLevelMessage::kGroundTruth, CreateParser<GroundTruth>()},
{ReaderTopLevelMessage::kSensorData, CreateParser<SensorData>()},
{ReaderTopLevelMessage::kSensorView, CreateParser<SensorView>()},
{ReaderTopLevelMessage::kSensorViewConfiguration, CreateParser<SensorViewConfiguration>()},
{ReaderTopLevelMessage::kHostVehicleData, CreateParser<HostVehicleData>()},
{ReaderTopLevelMessage::kTrafficCommand, CreateParser<TrafficCommand>()},
{ReaderTopLevelMessage::kTrafficCommandUpdate, CreateParser<TrafficCommandUpdate>()},
{ReaderTopLevelMessage::kTrafficUpdate, CreateParser<TrafficUpdate>()},
{ReaderTopLevelMessage::kMotionRequest, CreateParser<MotionRequest>()},
{ReaderTopLevelMessage::kStreamingUpdate, CreateParser<StreamingUpdate>()}
};
};


Expand Down
50 changes: 45 additions & 5 deletions include/osi-utilities/tracefile/TextTraceFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,70 @@
#include "Reader.h"
#include <fstream>
#include <functional>
#include <google/protobuf/text_format.h>

#include "osi_groundtruth.pb.h"
#include "osi_sensordata.pb.h"
#include "osi_sensorview.pb.h"
#include "osi_sensorviewconfiguration.pb.h"
#include "osi_hostvehicledata.pb.h"
#include "osi_trafficcommand.pb.h"
#include "osi_trafficcommandupdate.pb.h"
#include "osi_trafficupdate.pb.h"
#include "osi_motionrequest.pb.h"
#include "osi_streamingupdate.pb.h"

namespace osi3 {

/**

class TextTraceFileReader final : public TraceFileReader {
/**
* @brief Function type for parsing protobuf TextFormat strings into protobuf objects
*/
using MessageParserFunc = std::function<std::unique_ptr<google::protobuf::Message>(const std::string&)>;
using MessageParserFunc = std::function<std::unique_ptr<google::protobuf::Message>(const std::string&)>;


class TextTraceFileReader final : public TraceFileReader {
public:
bool Open(const std::string& filename) override;
bool Open(const std::string& filename, const ReaderTopLevelMessage message_type);
bool Open(const std::string& filename, ReaderTopLevelMessage message_type);
void Close() override;
bool HasNext() override;
std::optional<ReadResult> ReadMessage() override;

private:
std::ifstream trace_file_;
MessageParserFunc parser_; /**< Message parsing function */
std::string line_indicating_msg_start_="";
std::string line_indicating_msg_start_;
ReaderTopLevelMessage message_type_{ReaderTopLevelMessage::kUnknown}; /**< Current message type */
//MessageParserFunc parser_;
std::string ReadNextMessageFromFile();

template<typename T>
std::unique_ptr<google::protobuf::Message> ParseMessage(const std::string& data) {
auto msg = std::make_unique<T>();
if (google::protobuf::TextFormat::ParseFromString(data, msg.get())) {
throw std::runtime_error("Failed to parse message");
}
return std::move(msg);
}

template<typename T>
MessageParserFunc CreateParser() {
return [this](const std::string& data) { return ParseMessage<T>(data); };
}

const std::unordered_map<ReaderTopLevelMessage, MessageParserFunc> kParserMap_ = {
{ReaderTopLevelMessage::kGroundTruth, CreateParser<GroundTruth>()},
{ReaderTopLevelMessage::kSensorData, CreateParser<SensorData>()},
{ReaderTopLevelMessage::kSensorView, CreateParser<SensorView>()},
{ReaderTopLevelMessage::kSensorViewConfiguration, CreateParser<SensorViewConfiguration>()},
{ReaderTopLevelMessage::kHostVehicleData, CreateParser<HostVehicleData>()},
{ReaderTopLevelMessage::kTrafficCommand, CreateParser<TrafficCommand>()},
{ReaderTopLevelMessage::kTrafficCommandUpdate, CreateParser<TrafficCommandUpdate>()},
{ReaderTopLevelMessage::kTrafficUpdate, CreateParser<TrafficUpdate>()},
{ReaderTopLevelMessage::kMotionRequest, CreateParser<MotionRequest>()},
{ReaderTopLevelMessage::kStreamingUpdate, CreateParser<StreamingUpdate>()}
};
};

} // namespace osi3
Expand Down
34 changes: 0 additions & 34 deletions src/tracefile/NativeBinaryTraceFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,6 @@
#include <sstream>
#include <filesystem>

#include "osi_groundtruth.pb.h"
#include "osi_sensordata.pb.h"
#include "osi_sensorview.pb.h"
#include "osi_sensorviewconfiguration.pb.h"
#include "osi_hostvehicledata.pb.h"
#include "osi_trafficcommand.pb.h"
#include "osi_trafficcommandupdate.pb.h"
#include "osi_trafficupdate.pb.h"
#include "osi_motionrequest.pb.h"
#include "osi_streamingupdate.pb.h"


namespace {
template<typename T>
std::unique_ptr<google::protobuf::Message> ParseMessage(const std::vector<char>& data) {
auto msg = std::make_unique<T>();
msg->ParseFromArray(data.data(), static_cast<int>(data.size()));
return std::move(msg);
}

const std::unordered_map<osi3::ReaderTopLevelMessage, osi3::MessageParserFunc> kParserMap = {
{osi3::ReaderTopLevelMessage::kGroundTruth, [](const std::vector<char>& data) { return ParseMessage<osi3::GroundTruth>(data); }},
{osi3::ReaderTopLevelMessage::kSensorData, [](const std::vector<char>& data) { return ParseMessage<osi3::SensorData>(data); }},
{osi3::ReaderTopLevelMessage::kSensorView, [](const std::vector<char>& data) { return ParseMessage<osi3::SensorView>(data); }},
{osi3::ReaderTopLevelMessage::kSensorViewConfiguration, [](const std::vector<char>& data) { return ParseMessage<osi3::SensorViewConfiguration>(data); }},
{osi3::ReaderTopLevelMessage::kHostVehicleData, [](const std::vector<char>& data) { return ParseMessage<osi3::HostVehicleData>(data); }},
{osi3::ReaderTopLevelMessage::kTrafficCommand, [](const std::vector<char>& data) { return ParseMessage<osi3::TrafficCommand>(data); }},
{osi3::ReaderTopLevelMessage::kTrafficCommandUpdate, [](const std::vector<char>& data) { return ParseMessage<osi3::TrafficCommandUpdate>(data); }},
{osi3::ReaderTopLevelMessage::kTrafficUpdate, [](const std::vector<char>& data) { return ParseMessage<osi3::TrafficUpdate>(data); }},
{osi3::ReaderTopLevelMessage::kMotionRequest, [](const std::vector<char>& data) { return ParseMessage<osi3::MotionRequest>(data); }},
{osi3::ReaderTopLevelMessage::kStreamingUpdate, [](const std::vector<char>& data) { return ParseMessage<osi3::StreamingUpdate>(data); }}
};

} // unnamed namespace

namespace osi3 {

Expand Down
38 changes: 1 addition & 37 deletions src/tracefile/TextTraceFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,12 @@
//

#include "osi-utilities/tracefile/TextTraceFileReader.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/text_format.h"
#include <sstream>
#include <filesystem>

#include "osi_groundtruth.pb.h"
#include "osi_sensordata.pb.h"
#include "osi_sensorview.pb.h"
#include "osi_sensorviewconfiguration.pb.h"
#include "osi_hostvehicledata.pb.h"
#include "osi_trafficcommand.pb.h"
#include "osi_trafficcommandupdate.pb.h"
#include "osi_trafficupdate.pb.h"
#include "osi_motionrequest.pb.h"
#include "osi_streamingupdate.pb.h"

namespace osi3 {
namespace {
template<typename T>
std::unique_ptr<google::protobuf::Message> ParseMessage(const std::string& data) {
auto msg = std::make_unique<T>();
const auto res = google::protobuf::TextFormat::ParseFromString(data, msg.get());
if (!res) {
throw std::runtime_error("Failed to parse message");
}
return std::move(msg);
}

const std::unordered_map<osi3::ReaderTopLevelMessage, osi3::MessageParserFunc> kParserMap = {
{osi3::ReaderTopLevelMessage::kGroundTruth, [](const std::string& data) { return ParseMessage<osi3::GroundTruth>(data); }},
{osi3::ReaderTopLevelMessage::kSensorData, [](const std::string& data) { return ParseMessage<osi3::SensorData>(data); }},
{osi3::ReaderTopLevelMessage::kSensorView, [](const std::string& data) { return ParseMessage<osi3::SensorView>(data); }},
{osi3::ReaderTopLevelMessage::kSensorViewConfiguration, [](const std::string& data) { return ParseMessage<osi3::SensorViewConfiguration>(data); }},
{osi3::ReaderTopLevelMessage::kHostVehicleData, [](const std::string& data) { return ParseMessage<osi3::HostVehicleData>(data); }},
{osi3::ReaderTopLevelMessage::kTrafficCommand, [](const std::string& data) { return ParseMessage<osi3::TrafficCommand>(data); }},
{osi3::ReaderTopLevelMessage::kTrafficCommandUpdate, [](const std::string& data) { return ParseMessage<osi3::TrafficCommandUpdate>(data); }},
{osi3::ReaderTopLevelMessage::kTrafficUpdate, [](const std::string& data) { return ParseMessage<osi3::TrafficUpdate>(data); }},
{osi3::ReaderTopLevelMessage::kMotionRequest, [](const std::string& data) { return ParseMessage<osi3::MotionRequest>(data); }},
{osi3::ReaderTopLevelMessage::kStreamingUpdate, [](const std::string& data) { return ParseMessage<osi3::StreamingUpdate>(data); }}
};

} // unnamed namespace


bool TextTraceFileReader::Open(const std::string& filename) {
Expand Down Expand Up @@ -73,7 +37,7 @@ bool TextTraceFileReader::Open(const std::string& filename) {
return false;
}

parser_ = kParserMap.at(message_type_);
parser_ = kParserMap_.at(message_type_);

trace_file_ = std::ifstream(filename);

Expand Down

0 comments on commit e70f64a

Please sign in to comment.