-
Notifications
You must be signed in to change notification settings - Fork 33
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
Implement ParametersHandler library #13
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A minor comment. However, how is this supposed to be used? The code that consumes the parameters need to be templated on the template of IParametersHandler
to be used?
src/ParametersHandler/YarpImplementation/tests/ParametesHandlerYarpTest.cpp
Outdated
Show resolved
Hide resolved
Yes this is a drawback. template <typename T>
bool initialize(std::unique_ptr<IParametersHandler<T>> params); |
I see, the main problem that I see is that would be tricky with this to implement plugins (opened using dlopen) and mantain a stable ABI, but both are things that we don't need right now, so not a big problem. I do not fully understood why it was not possible to use classical runtime polymorphism with a virtual interface with a method, however this is something that can be always changed in the future, so I think it is ok to merge this as it is. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
I would suggest adding the following, but it is not blocking:
- an example with a custom parser
- setter methods
- printing methods
- an alias (see Implement ParametersHandler library #13 (comment))
) | ||
|
||
# add an executable to the project using the specified source files. | ||
add_library(${LIBRARY_TARGET_NAME} SHARED ${${LIBRARY_TARGET_NAME}_SRC} ${${LIBRARY_TARGET_NAME}_HDR}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hence there is no way to compile STATIC
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I try to compile STATIC
I got the following error.
Probably there is something wrong with YarpUtilities
../../../../lib/libBipedalLocomotionControllersParametersHandlerYarpImplementation.a(YarpImplementation.cpp.o): In function `bool BipedalLocomotionControllers::YarpUtilities::getVectorFromSearchable<std::vector<bool, std::allocator<bool> > >(yarp::os::Searchable const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<bool, std::allocator<bool> >&)':
/home/gromualdi/robot-code/bipedal-locomotion-controllers/src/YarpUtilities/include/BipedalLocomotionControllers/YarpUtilities/Helper.tpp:168: multiple definition of `bool BipedalLocomotionControllers::YarpUtilities::getVectorFromSearchable<std::vector<bool, std::allocator<bool> > >(yarp::os::Searchable const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<bool, std::allocator<bool> >&)'
CMakeFiles/ParametesHandlerYarpTest.dir/ParametesHandlerYarpTest.cpp.o:/home/gromualdi/robot-code/bipedal-locomotion-controllers/src/YarpUtilities/include/BipedalLocomotionControllers/YarpUtilities/Helper.tpp:168: first defined here
collect2: error: ld returned 1 exit status
src/ParametersHandler/YarpImplementation/tests/CMakeFiles/ParametesHandlerYarpTest.dir/build.make:126: recipe for target 'bin/ParametesHandlerYarpTest' failed
make[2]: *** [bin/ParametesHandlerYarpTest] Error 1
CMakeFiles/Makefile2:1138: recipe for target 'src/ParametersHandler/YarpImplementation/tests/CMakeFiles/ParametesHandlerYarpTest.dir/all' failed
make[1]: *** [src/ParametersHandler/YarpImplementation/tests/CMakeFiles/ParametesHandlerYarpTest.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
gromualdi@iiticublap109:~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably it also needs to be compiled STATIC
. @traversaro once suggested me to avoid using SHARED
when adding a library (simply put nothing). Then, in the main CMakeLists you add https://github.com/robotology/how-to-export-cpp-library/blob/master/CMakeLists.txt#L98. See https://cmake.org/cmake/help/v3.0/variable/BUILD_SHARED_LIBS.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I try to follow what you suggested in #13 (comment) I had the following error:
Scanning dependencies of target YarpUtilities
[ 8%] Building CXX object src/YarpUtilities/CMakeFiles/YarpUtilities.dir/src/Helper.cpp.o
[ 16%] Linking CXX static library ../../lib/libBipedalLocomotionControllersYarpUtilities.a
[ 16%] Built target YarpUtilities
Scanning dependencies of target YarpUtilitiesTestMain
[ 25%] Building CXX object src/YarpUtilities/tests/CMakeFiles/YarpUtilitiesTestMain.dir/YarpUtilitiesTestMain.cpp.o
[ 33%] Linking CXX static library ../../../lib/libYarpUtilitiesTestMain.a
[ 33%] Built target YarpUtilitiesTestMain
[ 41%] Linking CXX executable ../../../bin/YarpUtilitiesTest
../../../lib/libBipedalLocomotionControllersYarpUtilities.a(Helper.cpp.o): In function `bool BipedalLocomotionControllers::YarpUtilities::getVectorFromSearchable<std::vector<bool, std::allocator<bool> > >(yarp::os::Searchable const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<bool, std::allocator<bool> >&)':
/home/gromualdi/robot-code/bipedal-locomotion-controllers/src/YarpUtilities/include/BipedalLocomotionControllers/YarpUtilities/Helper.tpp:168: multiple definition of `bool BipedalLocomotionControllers::YarpUtilities::getVectorFromSearchable<std::vector<bool, std::allocator<bool> > >(yarp::os::Searchable const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<bool, std::allocator<bool> >&)'
CMakeFiles/YarpUtilitiesTest.dir/YarpUtilitiesTest.cpp.o:/home/gromualdi/robot-code/bipedal-locomotion-controllers/src/YarpUtilities/include/BipedalLocomotionControllers/YarpUtilities/Helper.tpp:168: first defined here
collect2: error: ld returned 1 exit status
src/YarpUtilities/tests/CMakeFiles/YarpUtilitiesTest.dir/build.make:125: recipe for target 'bin/YarpUtilitiesTest' failed
make[2]: *** [bin/YarpUtilitiesTest] Error 1
CMakeFiles/Makefile2:1055: recipe for target 'src/YarpUtilities/tests/CMakeFiles/YarpUtilitiesTest.dir/all' failed
make[1]: *** [src/YarpUtilities/tests/CMakeFiles/YarpUtilitiesTest.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
I should open a issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you can avoid facing this now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, some good old One Definition Rule (ODR, https://en.wikipedia.org/wiki/One_Definition_Rule) violations. I do not know the details, but the tipical trick is to mark the offending function as inline
or avoid explicit instantiation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...arametersHandler/include/BipedalLocomotionControllers/ParametersHandler/IParametersHandler.h
Show resolved
Hide resolved
What do you mean?
In case of YarpInteface the member attribute is a const yarp::os::Searchable& m_searchable If a setter method is added the
Implemented in 63d472b. The parameters can be printed as: std::cout << handler << std::endl;
I'm investigating on this |
I was thinking of adding a test that could work as a tutorial for implementing a parser different from
Is the |
A map of |
I don't think it is necessary |
From where do you think I got the idea? 😁 |
Unfortunately, the object |
What about using a |
448f528
to
f718bdf
Compare
Passing a |
Can you use a |
You mean a |
The first (the object not the reference). |
Seems that we cannot initialize a |
Bummer...I am afraid that also without setters it may be pretty restricting though 🤔 |
Ok, I think I may have found a solution. Apparently you can load a
Hence, you can do bottle.fromString(searchable.toString()); |
In a68d06c I implemented a simple test with an homemade parameters container based on The only way to solve the problem is to edit the following line std::unique_ptr<IParametersHandler<BasicImplementation>> parameterHandler = std::make_unique<BasicImplementation>(parameters); in std::unique_ptr<BasicImplementation> parameterHandler = std::make_unique<BasicImplementation>(parameters); Am I missing something on CRPT? Do you have any idea? This is the valgrind error
|
|
Co-Authored-By: Silvio Traversaro <silvio.traversaro@iit.it>
…sHandler::YarpImplementation - Implement YarpImplementation::setParameter() function
…as shared library
c7dbe38
to
6f2e259
Compare
The memory leak shown in #13 (comment) has been fixed in 729ca1e |
I would like to use classical polymorphism i.e. class IParametersHandler
{
public:
template <typename T>
virtual bool getParameter(const std::string& parameterName, T& parameter) const = 0;
};
class YarpImplementation : public IParametersHandler
{
public:
template <typename T>
virtual bool getParameter(const std::string& parameterName, T& parameter) const
{
// custom code
}
}; However seems that a a class member function template cannot be virtual. If you have any idea of how to solve the problem without using CRTP let me know. I would prefer to not use CRTP because the consumer libraries must have template member functions. Consequentially class ConsmerLibrary
{
...
public:
template <typename T>
bool initialize(std::unique_ptr<IParameterHandler<T>> handler);
...
} |
@GiulioRomualdi , thanks, everything is clear to me after your explanation, clearly you need to use CRTP if you want to support arbitrary data types that support a given "Concept", i.e. a group of methods. I suspect the proper solution then is to agree on canonical types for vector and matrices, and just use them also in the |
Ok if @S-Dafarra agrees, I merge the PR |
|
||
template <typename T> void setParameter(const std::string& parameterName, const T& parameter) | ||
{ | ||
m_map.insert({parameterName, std::make_any<T>(parameter)}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it's just a test, but can you use the operator[]
to deal also with the case where the parameter already exists?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! Done in #13
std::vector<std::string> element; | ||
REQUIRE(groupHandler->getParameter("Donald's nephews", element)); | ||
REQUIRE(element == std::vector<std::string>{"Huey", "Dewey", "Louie"}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you test also the print function as in https://github.com/dic-iit/bipedal-locomotion-controllers/pull/13/files#diff-2187c40c7243c514d7eca0ce41d286eeR72-R75?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 987775d
return std::make_unique<BasicImplementation>(map); | ||
} | ||
|
||
friend std::ostream& operator<<(std::ostream& os, const BasicImplementation& handler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the friend
required? Wouldn't it be easier with a toString()
method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed F2F the toString()
function has been implemented in #13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
This PR implements a library for retrieving parameters from configuration files. Thanks to this PR we can drop the dependency from YARP for all the consumer libraries.
The classes implemented in the library are structured as follows:
IParametersHandler<Derived>
is a base template class that implementsgetParameter()
andgetGroup()
method.YarpImplementation
is the specific implementation ofIParametersHandler
based on yarp.The Curiously Recurring Template Pattern has been used to implement a static polymorphism. It is required because a class member function template cannot be virtual. (i.e. the following example will not compile)
In the end, thanks to this library the controllers/estimators/planners will not require Yarp anymore for retrieving the parameters from configuration files. The consumer library will link
ParametersHandler
(it is YARP independent) and the entire configuration process will be transparent to the middleware used to retrieve the parameters (i.e. YARP/ROS ...)@prashanthr05 @traversaro