Skip to content

Commit

Permalink
Merge pull request #390 from dic-iit/fix/parametersHandler_vector_bool
Browse files Browse the repository at this point in the history
Fix the `TOML` and `YARP` implementation of the parameters handler when a `std::vector<bool>` is passed to the `setParameter()`
  • Loading branch information
GiulioRomualdi authored Aug 11, 2021
2 parents cb505a6 + a8e2589 commit 585d377
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ env:
LieGroupControllers_TAG: v0.1.1
osqp_TAG: v0.6.2
OsqpEigen_TAG: v0.6.3
tomlplusplus_TAG: v2.4.0
tomlplusplus_TAG: 4f21332bdd7555bbf9add1aafe82909a2f8aa52b~

# Overwrite the VCPKG_INSTALLATION_ROOT env variable defined by GitHub Actions to point to our vcpkg
VCPKG_INSTALLATION_ROOT: C:\robotology\vcpkg
Expand Down Expand Up @@ -278,8 +278,9 @@ jobs:
# Tomlplusplus
cd ${GITHUB_WORKSPACE}
git clone -b ${tomlplusplus_TAG} https://github.com/marzer/tomlplusplus
git clone https://github.com/marzer/tomlplusplus
cd tomlplusplus
git checkout ${tomlplusplus_TAG}
mkdir -p build
cd build
cmake -DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/install/deps ..
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ All notable changes to this project are documented in this file.
### Fix
- Fixed the crashing of `YarpSensorBridge` while trying to access unconfigured control board sensors data by adding some checks (https://github.com/dic-iit/bipedal-locomotion-framework/pull/378)
- Fixed the compilation of Python bindings (enabled by the `FRAMEWORK_COMPILE_PYTHON_BINDINGS` CMake option) when compiling with Visual Studio (https://github.com/dic-iit/bipedal-locomotion-framework/pull/380).
- Fixed the `TOML` and `YARP` implementation of the parameters handler when a `std::vector<bool>` is passed to the `setParameter()` method (https://github.com/dic-iit/bipedal-locomotion-framework/pull/390).

## [0.2.0] - 2021-06-15
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ class TomlImplementation : public IParametersHandler
~TomlImplementation() = default;
};

/**
* Private implementation of setParameter
* @param parameterName name of the parameter
* @param parameter parameter
* @note The specialization is required because std::vector<bool> is not a container and the
* operator[] does not return a bool&
*/
template <>
void TomlImplementation::setParameterPrivate<std::vector<bool>>(const std::string& parameterName,
const std::vector<bool>& parameter);
} // namespace ParametersHandler
} // namespace BipedalLocomotion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@

using namespace BipedalLocomotion::ParametersHandler;

template <>
void TomlImplementation::setParameterPrivate<std::vector<bool>>(const std::string& parameterName,
const std::vector<bool>& parameter)
{
// vector<bool> is (usually) specialized explicitly to store each bool in a single bit,
// The range loop using reference leads to a compilation error.
// https://stackoverflow.com/questions/34079390/range-for-loops-and-stdvectorbool
m_container.insert_or_assign(parameterName, toml::array());
for (bool element : parameter)
{
m_container[parameterName].as_array()->push_back(element);
}
}

bool TomlImplementation::getParameter(const std::string& parameterName, int& parameter) const
{
return getParameterPrivate(parameterName, parameter);
Expand Down Expand Up @@ -93,6 +107,7 @@ void TomlImplementation::setParameter(const std::string& parameterName,
{
return setParameterPrivate(parameterName, parameter);
}

void TomlImplementation::setParameter(
const std::string& parameterName,
const GenericContainer::Vector<const std::string>::Ref parameter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void YarpImplementation::setParameterPrivate<std::vector<bool>>(const std::strin
yarp::os::Value yarpNewList;
auto newList = yarpNewList.asList();

for (const auto& element : parameter)
for (bool element : parameter)
{
// if the parameter is a boolean we cannot use the usual way to add the new parameter
// Please check https://github.com/robotology/yarp/issues/2584#issuecomment-847778679
Expand Down
9 changes: 9 additions & 0 deletions src/ParametersHandler/tests/ParametersHandlerTomlTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ TEST_CASE("Get parameters")
REQUIRE(element == fibonacciNumbers);
}

SECTION("Set bool Vector")
{
std::vector<bool> element;
const std::vector<bool> newFlags{false, true, false, true, true, true, false};
parameterHandler->setParameter("new_flags", newFlags);
REQUIRE(parameterHandler->getParameter("new_flags", element));
REQUIRE(element == newFlags);
}

SECTION("Get bool Vector")
{
std::vector<bool> element;
Expand Down

0 comments on commit 585d377

Please sign in to comment.