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 support for loading vector from configuration files with parenthesis #677

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading