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

Expose std::unordered_map<std::string, double> to SWIG bindings #1180

Merged
merged 1 commit into from
Apr 23, 2024

Conversation

diegoferigo
Copy link
Member

@diegoferigo diegoferigo commented Apr 23, 2024

This is necessary for the new methods introduced in #1174 to accept a dictionary from the target language and convert it to C++. I only tested that it works from Python.

@diegoferigo diegoferigo self-assigned this Apr 23, 2024
@@ -19,6 +20,9 @@
// Wrap the std::vector<std::int> params
%template(IntVector) std::vector<int>;

// Wrap the std::unordered_map<std::string, double>
%template(StringToFloatUnorderedMap) std::unordered_map<std::string, double>;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
%template(StringToFloatUnorderedMap) std::unordered_map<std::string, double>;
%template(StringToFloatUnorderedMap) std::unordered_map<std::string, double>;

Why not StringToDoubleUnorderedMap ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Bad Python habits :) Fixed in 6ef64eb.

@traversaro
Copy link
Member

Cool! Can you add the snippet of code that you used to test if this worked?

@diegoferigo diegoferigo force-pushed the bindings/unordered_map_python branch from 6eeb847 to 6ef64eb Compare April 23, 2024 12:34
@diegoferigo
Copy link
Member Author

Cool! Can you add the snippet of code that you used to test if this worked?

Sure:

import idyntree.bindings as idt
import resolve_robotics_uri_py

urdf_path = resolve_robotics_uri_py.resolve_robotics_uri(
    uri="model://ergoCubSN001/model.urdf"
)

mdl_loader = idt.ModelLoader()

assert mdl_loader.loadReducedModelFromString(
    urdf_path.read_text(),
    [],
    {"my_joint": 3.14}
)

@traversaro
Copy link
Member

Great, so basically the user never needs to use the StringToDoubleUnorderedMap type explicitly, great.

@diegoferigo
Copy link
Member Author

diegoferigo commented Apr 23, 2024

Great, so basically the user never needs to use the StringToDoubleUnorderedMap type explicitly, great.

Yep! SWIG automatically converts a Python dictionary to a C++ unordered map. It is not even too picky on the type, from Python the dictionary could also be dict[str, int] and it still works.

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