Skip to content

Commit

Permalink
Data container: Make mapping to include multi-id and topic name. Refo…
Browse files Browse the repository at this point in the history
…rmat code
  • Loading branch information
ThomasDebrunner committed Sep 20, 2023
1 parent 0367d17 commit c2c2c48
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/ulog_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int main(int argc, char** argv)
}

// Get a particular subscription
const auto& subscription = data_container->subscription("vehicle_status_0");
const auto& subscription = data_container->subscription("vehicle_status");

// Get message format of subscription
auto message_format = subscription->format();
Expand Down
16 changes: 9 additions & 7 deletions ulog_cpp/data_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ void DataContainer::error(const std::string& msg, bool is_recoverable)
void DataContainer::headerComplete()
{
// try to resolve all message formats
for (auto &it : _message_formats) {
auto &message_format = it.second;
for (auto& it : _message_formats) {
auto& message_format = it.second;
message_format->resolveDefinition(_message_formats);
}
_header_complete = true;
Expand Down Expand Up @@ -77,7 +77,8 @@ void DataContainer::addLoggedMessage(const AddLoggedMessage& add_logged_message)
if (_header_complete && _storage_config == StorageConfig::Header) {
return;
}
if (_subscriptions_by_message_id.find(add_logged_message.msgId()) != _subscriptions_by_message_id.end()) {
if (_subscriptions_by_message_id.find(add_logged_message.msgId()) !=
_subscriptions_by_message_id.end()) {
throw ParsingException("Duplicate AddLoggedMessage message ID");
}

Expand All @@ -86,12 +87,13 @@ void DataContainer::addLoggedMessage(const AddLoggedMessage& add_logged_message)
throw ParsingException("AddLoggedMessage message format not found");
}

auto new_subscription = std::make_shared<Subscription>(add_logged_message,
std::vector<Data>{}, format_iter->second);
auto new_subscription =
std::make_shared<Subscription>(add_logged_message, std::vector<Data>{}, format_iter->second);
_subscriptions_by_message_id.insert({add_logged_message.msgId(), new_subscription});

const auto key = add_logged_message.messageName() + "_" + std::to_string(add_logged_message.multiId());
_subscriptions_by_name.insert({key, new_subscription});
const NameAndMultiIdKey key{add_logged_message.messageName(),
static_cast<int>(add_logged_message.multiId())};
_subscriptions_by_name_and_multi_id.insert({key, new_subscription});
}
void DataContainer::logging(const Logging& logging)
{
Expand Down
56 changes: 45 additions & 11 deletions ulog_cpp/data_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
****************************************************************************/
#pragma once

#include <memory>
#include <unordered_map>
#include <vector>
#include <memory>
#include "subscription.hpp"

#include "data_handler_interface.hpp"
#include "subscription.hpp"

namespace ulog_cpp {

Expand All @@ -19,6 +20,28 @@ class DataContainer : public DataHandlerInterface {
FullLog, ///< keep full log in memory
};

struct NameAndMultiIdKey {
std::string name;
int multi_id;

NameAndMultiIdKey(const std::string& name, int multi_id) : name(name), multi_id(multi_id) {}

bool operator==(const NameAndMultiIdKey& other) const
{
return name == other.name && multi_id == other.multi_id;
}

bool operator<(const NameAndMultiIdKey& other) const
{
return name < other.name || (name == other.name && multi_id < other.multi_id);
}

bool operator>(const NameAndMultiIdKey& other) const
{
return name > other.name || (name == other.name && multi_id > other.multi_id);
}
};

explicit DataContainer(StorageConfig storage_config);
virtual ~DataContainer() = default;

Expand Down Expand Up @@ -46,7 +69,10 @@ class DataContainer : public DataHandlerInterface {
{
return _message_info_multi;
}
const std::map<std::string, std::shared_ptr<MessageFormat>>& messageFormats() const { return _message_formats; }
const std::map<std::string, std::shared_ptr<MessageFormat>>& messageFormats() const
{
return _message_formats;
}
const std::map<std::string, Parameter>& initialParameters() const { return _initial_parameters; }
const std::map<std::string, ParameterDefault>& defaultParameters() const
{
Expand All @@ -56,20 +82,28 @@ class DataContainer : public DataHandlerInterface {
const std::vector<Logging>& logging() const { return _logging; }
const std::vector<Dropout>& dropouts() const { return _dropouts; }

const std::map<std::string, std::shared_ptr<Subscription>>& subscriptionsByName() {
return _subscriptions_by_name;
const std::map<NameAndMultiIdKey, std::shared_ptr<Subscription>>& subscriptionsByNameAndMultiId()
{
return _subscriptions_by_name_and_multi_id;
}

std::vector<std::string> subscriptionNames() const {
std::vector<std::string> subscriptionNames() const
{
std::vector<std::string> names;
for (const auto& kv : _subscriptions_by_name) {
names.push_back(kv.first);
for (const auto& kv : _subscriptions_by_name_and_multi_id) {
names.push_back(kv.first.name);
}
return names;
}

std::shared_ptr<Subscription> subscription(const std::string& name) {
return _subscriptions_by_name.at(name);
std::shared_ptr<Subscription> subscription(const std::string& name, int multi_id) const
{
return _subscriptions_by_name_and_multi_id.at({name, multi_id});
}

std::shared_ptr<Subscription> subscription(const std::string& name) const
{
return _subscriptions_by_name_and_multi_id.at({name, 0});
}

private:
Expand All @@ -87,7 +121,7 @@ class DataContainer : public DataHandlerInterface {
std::map<std::string, ParameterDefault> _default_parameters;
std::vector<Parameter> _changed_parameters;
std::map<uint16_t, std::shared_ptr<Subscription>> _subscriptions_by_message_id;
std::map<std::string, std::shared_ptr<Subscription>> _subscriptions_by_name;
std::map<NameAndMultiIdKey, std::shared_ptr<Subscription>> _subscriptions_by_name_and_multi_id;
std::vector<Logging> _logging;
std::vector<Dropout> _dropouts;
};
Expand Down

0 comments on commit c2c2c48

Please sign in to comment.