Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nice data access API #2

Merged
merged 12 commits into from
Nov 14, 2023
Merged

Add nice data access API #2

merged 12 commits into from
Nov 14, 2023

Conversation

ThomasDebrunner
Copy link
Member

@ThomasDebrunner ThomasDebrunner commented Sep 20, 2023

This changes the library the following way:

  • The field class now also stores sizes and offsets in the message format
  • The value class is overhauled to automatically cast to the right types based on the message definitions
  • The value class can automatically cast to the users desired types
  • The MessageFormat contains all information needed to parse the message
  • The MessageFormats are resolved recursively once the header is received

Still to do:

  • Nested types are resolved correctly, but the API to access in a nested way is missing
  • Make sure all the previous tests and use cases are still supported
  • Create new tests for the added functionlity

This allows the user to read data like this:

  // Read out some data

  // List all subscription names
  for (const auto& sub : data_container->subscriptionNames()) {
    std::cout << sub << std::endl;
  }

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

  // Get message format of subscription
  auto message_format = subscription->format();
  std::cout << "Message format: " << message_format->name() << std::endl;

  // List all field names
  for (const std::string &field : subscription->fieldNames()) {
    std::cout << field << std::endl;
  }

  // Get particular field
  auto nav_state_field = subscription->field("nav_state");

  // Iterate over all samples
  for (const auto &sample : *subscription) {
    // always correctly extracts the type as defined in the message definition,
    // gets cast to the value you put in int.
    // This also works for arrays and strings.
    auto nav_state = sample[nav_state_field].as<int>();
    std::cout << nav_state << std::endl;
  }

  // get a specific sample
  auto sample_12 = subscription->at(12);

  // access values by name
  auto timestamp = sample_12["timestamp"].as<uint64_t>();

  std::cout << timestamp << std::endl;

@bkueng bkueng self-requested a review September 20, 2023 13:16
@ThomasDebrunner ThomasDebrunner marked this pull request as ready for review October 4, 2023 14:20
@ThomasDebrunner ThomasDebrunner changed the title [WIP] Add nice data access API Add nice data access API Oct 4, 2023
Copy link
Member

@bkueng bkueng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! I like the type magic.

Would be good to have some unit tests for the nested data access case.

ulog_cpp/subscription.hpp Outdated Show resolved Hide resolved
ulog_cpp/subscription.hpp Outdated Show resolved Hide resolved
ulog_cpp/reader.cpp Outdated Show resolved Hide resolved
test/CMakeLists.txt Outdated Show resolved Hide resolved
ulog_cpp/data_container.hpp Outdated Show resolved Hide resolved
ulog_cpp/data_container.cpp Show resolved Hide resolved
ulog_cpp/data_container.hpp Outdated Show resolved Hide resolved
ulog_cpp/messages.hpp Show resolved Hide resolved
ulog_cpp/messages.hpp Outdated Show resolved Hide resolved
ulog_cpp/messages.hpp Outdated Show resolved Hide resolved
@ThomasDebrunner
Copy link
Member Author

Thank you for the review! I'll work on adding more unit tests.

@ThomasDebrunner
Copy link
Member Author

Also merged in main

ThomasDebrunner and others added 4 commits October 17, 2023 11:51
This changes the library the following way:

- The field class now also stores sizes and offsets in the message format
- The value class is overhauled to automatically cast to the right types based on the message definitions
- The value class can automatically cast to the users desired types
- The MessageFormat contains all information needed to parse the message
- The MessageFormats are resolved recursively once the header is received
allows derived classes to modify the data
@ThomasDebrunner
Copy link
Member Author

Also added a bunch of unit testing for the added functionality

examples/ulog_info.cpp Outdated Show resolved Hide resolved
test/CMakeLists.txt Outdated Show resolved Hide resolved
@bkueng bkueng merged commit 2e8552e into PX4:main Nov 14, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants