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 Ref class to GeneriContainer::Vector #102

Merged
merged 5 commits into from
Sep 10, 2020

Conversation

S-Dafarra
Copy link
Member

@S-Dafarra S-Dafarra commented Sep 9, 2020

The class Ref is used as a substitution to a classical reference to a Vector. The advantage of using this, is that custom vectors (all those supported by GenericContainer::Vector) can be implicitly casted to Ref.
Ref does not allocate any memory in construction, hence can be used as a parameter to be passed by copy. The operator = clones the content.
Ref inherits Vector<T>, hence it can be used as it was a Vector<T>.

This goes in the direction of fixing #95, at least to simplify the use of GenericContainer::Vector<double> in public interfaces.

Copy link
Member

@GiulioRomualdi GiulioRomualdi left a comment

Choose a reason for hiding this comment

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

That's cool, @S-Dafarra!

So thanks to this PR we can change
https://github.com/dic-iit/bipedal-locomotion-framework/blob/6923a1fba872796d6143e7a3bf735a49df8e194c/src/ParametersHandler/include/BipedalLocomotion/ParametersHandler/IParametersHandler.h#L35

into

 virtual bool getParameter(const std::string& parameterName, GenericContainer::Vector<int>::Ref parameter) const = 0

right?

@S-Dafarra
Copy link
Member Author

That's cool, @S-Dafarra!

So thanks to this PR we can change
https://github.com/dic-iit/bipedal-locomotion-framework/blob/6923a1fba872796d6143e7a3bf735a49df8e194c/src/ParametersHandler/include/BipedalLocomotion/ParametersHandler/IParametersHandler.h#L35

into

 virtual bool getParameter(const std::string& parameterName, GenericContainer::Vector<int>::Ref parameter) const = 0

right?

Yes, we can get rid also of the template method, and move all those using a GenericContainer::Vector from protected to public. At least, that was the plan, but I wanted to do things gradually.

@GiulioRomualdi
Copy link
Member

but I wanted to do things gradually.

I agree, feel free to merge it

@S-Dafarra S-Dafarra merged commit 436ae4c into ami-iit:master Sep 10, 2020
Comment on lines +946 to +947
template <class Vector, typename = typename std::enable_if<!GenericContainer::is_vector<Vector>::value &&
!std::is_same<Vector, std::string>::value &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

in C++17 we have is_same_v which can replace is_same<T, T>::value

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it is a little more verbose in this way, but some static analyzers complain less sometimes 😁

Comment on lines +981 to +984
template <class Vector, typename = typename std::enable_if<!GenericContainer::is_vector<Vector>::value &&
!std::is_same<Vector, std::string>::value &&
GenericContainer::is_vector_constructible<Vector>::value &&
std::is_const_v<T>>::type>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a curiosity. Can this be SFINAE construct be simplified using an if constexpr in this specific scenario? If not, could you tell me why?

Copy link
Member Author

Choose a reason for hiding this comment

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

SFINAE makes the function/method below disappear. This allows the compiler to choose one version or the other. if constexpr instead is used inside the function. Hence, you can use it to compile a method in two different ways while maintaining the same signature. Rule of thumb:

  • use SFINAE to drive the compiler toward the correct method, when different methods have ambiguous signatures
  • use if constexpr to conditionally compile some pieces of code inside a method depending on some condition known at compile time.

With c++20 SFINAE may be substituted with a concept

Copy link
Collaborator

Choose a reason for hiding this comment

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

Understood! Thanks for the pointers!

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.

3 participants