Skip to content

Commit

Permalink
Merge pull request #677 from robotology/fixvecparenthesis
Browse files Browse the repository at this point in the history
Add support for loading vector from configuration files with parenthesis
  • Loading branch information
traversaro authored Apr 4, 2024
2 parents 6e6de2b + f1ec0f2 commit 3d59783
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 205 deletions.
88 changes: 87 additions & 1 deletion libraries/common/include/GazeboYarpPlugins/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

#include <string>
#include <cmath>
#include <vector>

#include <yarp/os/LogStream.h>
#include <yarp/os/Searchable.h>
#include <yarp/os/Value.h>

namespace GazeboYarpPlugins {

Expand Down Expand Up @@ -70,8 +75,89 @@ namespace GazeboYarpPlugins {
if (pos == std::string::npos) {
return fullString;
} else {
return fullString.substr(pos + separator.size() - 1);
return fullString.substr(pos + separator.size() - 1);
}
}

template<typename T>
inline T readElementFromValue(yarp::os::Value& val);

template<>
inline double readElementFromValue<double>(yarp::os::Value& val)
{
return val.asFloat64();
}

template<>
inline std::string readElementFromValue<std::string>(yarp::os::Value& val)
{
return val.asString();
}

/**
* Get a vector from a parameter, using both the recommended style:
* nameOfList (elem1 elem2 elem3)
* or the deprecated (since YARP 3.10):
* nameOfList elem1 elem2 elem3
*
*
* \brief Get vector from YARP configuration
* \return true if the parsing was successful, false otherwise
*/
template <typename T>
inline bool readVectorFromConfigFile(const yarp::os::Searchable& params, const std::string&listName, std::vector<T>& outputList)
{
bool vectorPopulated = false;
outputList.resize(0);
yarp::os::Value& val = params.find(listName);
if (!val.isNull() && val.isList())
{
yarp::os::Bottle* listBot = val.asList();
outputList.resize(listBot->size());

for (size_t i=0; i < outputList.size(); i++)
{
outputList[i] = readElementFromValue<T>(listBot->get(i));
}

vectorPopulated = true;
}
else
{
// Try to interpreter the list via findGroup
yarp::os::Bottle listBottleAndKey = params.findGroup(listName);
if (!listBottleAndKey.isNull())
{
yWarning() << "Parameter " << listName << " should be a list, but its format is deprecated as parenthesis are missing."
<< " Please add parentesis to the list, as documented in https://github.com/robotology/yarp/discussions/3092 .";

outputList.resize(listBottleAndKey.size()-1);

for (size_t i=0; i < outputList.size(); i++)
{
outputList[i] = readElementFromValue<T>(listBottleAndKey.get(i+1));
}
vectorPopulated = true;
}
}
return vectorPopulated;
}

/**
* Convert a vector to a string for printing.
*/
template <typename T>
inline std::string vectorToString(std::vector<T>& outputList)
{
std::stringstream ss;
for (size_t i = 0; i < outputList.size(); ++i) {
ss << outputList[i];
if (i != outputList.size() - 1)
{
ss << " ";
}
}
return ss.str();
}
}

Expand Down
Loading

0 comments on commit 3d59783

Please sign in to comment.