Skip to content

Commit

Permalink
Merge pull request #288 from dic-iit/parameterHandler/clone
Browse files Browse the repository at this point in the history
Implement clone method in ParametersHandler classes
  • Loading branch information
GiulioRomualdi authored Apr 27, 2021
2 parents 6b5da02 + 0cd62db commit 665e04d
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 67 deletions.
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.
- Implement TaskSpaceInverseDynamics interface (https://github.com/dic-iit/bipedal-locomotion-framework/pull/279)
- Implement `Wrench` class (https://github.com/dic-iit/bipedal-locomotion-framework/pull/279)
- Implement `SO3Task` in `TSID` component (https://github.com/dic-iit/bipedal-locomotion-framework/pull/281)
- Implement clone method in ParametersHandler classes (https://github.com/dic-iit/bipedal-locomotion-framework/pull/288)

### Changed
- Move all the Contacts related classes in Contacts component (https://github.com/dic-iit/bipedal-locomotion-framework/pull/204)
Expand Down
2 changes: 1 addition & 1 deletion src/ParametersHandler/YarpImplementation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if(FRAMEWORK_COMPILE_YarpImplementation)
NAME ParametersHandlerYarpImplementation
SOURCES src/YarpImplementation.cpp
PUBLIC_HEADERS include/BipedalLocomotion/ParametersHandler/YarpImplementation.h include/BipedalLocomotion/ParametersHandler/YarpImplementation.tpp
PUBLIC_LINK_LIBRARIES BipedalLocomotion::ParametersHandler BipedalLocomotion::YarpUtilities YARP::YARP_os
PUBLIC_LINK_LIBRARIES BipedalLocomotion::ParametersHandler BipedalLocomotion::YarpUtilities YARP::YARP_os BipedalLocomotion::TextLogging
INSTALLATION_FOLDER ParametersHandler)

endif()
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ class YarpImplementation : public IParametersHandler
*/
template <typename T>
void setParameterPrivate(const std::string& parameterName, const T& parameter);

/**
* Clone the content of the handler
* @return A pointer to YarpImplementation containing the content of the handler.
*/
std::shared_ptr<YarpImplementation> clonePrivate() const;

public:
/**
* Constructor.
Expand Down Expand Up @@ -235,6 +242,13 @@ class YarpImplementation : public IParametersHandler
*/
void clear() final;

/**
* Clone the content of the content.
* @return a IParametersHandler::shared_ptr clone of the current handler.
* @warning
*/
shared_ptr clone() const final;

/**
* Destructor
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string>

#include <BipedalLocomotion/ParametersHandler/YarpImplementation.h>
#include <BipedalLocomotion/TextLogging/Logger.h>

using namespace BipedalLocomotion::ParametersHandler;

Expand Down Expand Up @@ -159,9 +160,9 @@ bool YarpImplementation::setGroup(const std::string& name, IParametersHandler::s
// m_container
if (downcastedPtr == nullptr)
{
std::cerr << "[YarpImplementation::setGroup] Unable to downcast the pointer to "
"YarpImplementation."
<< std::endl;
log()->error("[YarpImplementation::setGroup] Unable to downcast the pointer to "
"YarpImplementation.");

return false;
}

Expand Down Expand Up @@ -201,3 +202,21 @@ void YarpImplementation::clear()
m_container.clear();
m_lists.clear();
}

std::shared_ptr<YarpImplementation> YarpImplementation::clonePrivate() const
{
auto handler = std::make_shared<YarpImplementation>();

// copy the content of the parameters stored in the handler.
handler->m_container = this->m_container;

for (const auto& [key, value] : m_lists)
handler->m_lists[key] = value->clonePrivate();

return handler;
}

IParametersHandler::shared_ptr YarpImplementation::clone() const
{
return this->clonePrivate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <string>
#include <vector>

#include <BipedalLocomotion/GenericContainer/Vector.h>
#include <BipedalLocomotion/GenericContainer/TemplateHelpers.h>
#include <BipedalLocomotion/GenericContainer/Vector.h>

namespace BipedalLocomotion
{
Expand All @@ -26,7 +26,6 @@ namespace ParametersHandler
class IParametersHandler
{
public:

using unique_ptr = std::unique_ptr<IParametersHandler>;

using shared_ptr = std::shared_ptr<IParametersHandler>;
Expand Down Expand Up @@ -72,31 +71,35 @@ class IParametersHandler
* @param parameter parameter
* @return true/false in case of success/failure
*/
virtual bool getParameter(const std::string& parameterName, std::vector<bool>& parameter) const = 0;
virtual bool getParameter(const std::string& parameterName, //
std::vector<bool>& parameter) const = 0;

/**
* Get a parameter [GenericContainer::Vector<int>]
* @param parameterName name of the parameter
* @param parameter parameter
* @return true/false in case of success/failure
*/
virtual bool getParameter(const std::string& parameterName, GenericContainer::Vector<int>::Ref parameter) const = 0;
virtual bool getParameter(const std::string& parameterName,
GenericContainer::Vector<int>::Ref parameter) const = 0;

/**
* Get a parameter [GenericContainer::Vector<double>]
* @param parameterName name of the parameter
* @param parameter parameter
* @return true/false in case of success/failure
*/
virtual bool getParameter(const std::string& parameterName, GenericContainer::Vector<double>::Ref parameter) const = 0;
virtual bool getParameter(const std::string& parameterName,
GenericContainer::Vector<double>::Ref parameter) const = 0;

/**
* Get a parameter [GenericContainer::Vector<std::string>]
* @param parameterName name of the parameter
* @param parameter parameter
* @return true/false in case of success/failure
*/
virtual bool getParameter(const std::string& parameterName, GenericContainer::Vector<std::string>::Ref parameter) const = 0;
virtual bool getParameter(const std::string& parameterName,
GenericContainer::Vector<std::string>::Ref parameter) const = 0;

/**
* Set a parameter [int]
Expand Down Expand Up @@ -135,34 +138,40 @@ class IParametersHandler
*/
virtual void setParameter(const std::string& parameterName, const bool& parameter) = 0;


/**
* Set a parameter [std::vector<bool>]
* @param parameterName name of the parameter
* @param parameter parameter
*/
virtual void setParameter(const std::string& parameterName, const std::vector<bool>& parameter) = 0;
virtual void setParameter(const std::string& parameterName, const std::vector<bool>& parameter)
= 0;

/**
* Set a parameter [GenericContainer::Vector<int>]
* @param parameterName name of the parameter
* @param parameter parameter
*/
virtual void setParameter(const std::string& parameterName, const GenericContainer::Vector<const int>::Ref parameter) = 0;
virtual void setParameter(const std::string& parameterName,
const GenericContainer::Vector<const int>::Ref parameter)
= 0;

/**
* Set a parameter [GenericContainer::Vector<double>]
* @param parameterName name of the parameter
* @param parameter parameter
*/
virtual void setParameter(const std::string& parameterName, const GenericContainer::Vector<const double>::Ref parameter) = 0;
virtual void setParameter(const std::string& parameterName,
const GenericContainer::Vector<const double>::Ref parameter)
= 0;

/**
* Set a parameter [GenericContainer::Vector<std::string>]
* @param parameterName name of the parameter
* @param parameter parameter
*/
virtual void setParameter(const std::string& parameterName, const GenericContainer::Vector<const std::string>::Ref parameter) = 0;
virtual void setParameter(const std::string& parameterName,
const GenericContainer::Vector<const std::string>::Ref parameter)
= 0;

/**
* Get a Group from the handler.
Expand Down Expand Up @@ -207,11 +216,18 @@ class IParametersHandler
*/
virtual void clear() = 0;

/**
* Clone the content of the content.
* @return a IParametersHandler::shared_ptr clone of the current handler.
* @warning Please implement the specific version of this method in the Derived class. Please
* check YarpImplementation::clone
*/
virtual shared_ptr clone() const = 0;

/**
* Destructor
*/
virtual ~IParametersHandler() = default;

};
} // namespace ParametersHandler
} // namespace BipedalLocomotion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ class StdImplementation : public IParametersHandler
*/
void clear() final;

/**
* Clone the content of the content.
* @return a IParametersHandler::shared_ptr clone of the current handler.
*/
shared_ptr clone() const final;

~StdImplementation() = default;
};
} // namespace ParametersHandler
Expand Down
10 changes: 10 additions & 0 deletions src/ParametersHandler/src/StdImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,13 @@ void StdImplementation::clear()
{
m_map.clear();
}

IParametersHandler::shared_ptr StdImplementation::clone() const
{
auto handler = std::make_shared<StdImplementation>();

// copy the content of the parameters stored in the handler.
handler->m_map = this->m_map;

return handler;
}
34 changes: 34 additions & 0 deletions src/ParametersHandler/tests/ParametersHandlerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,38 @@ TEST_CASE("Get parameters")
parameterHandler->clear();
REQUIRE(parameterHandler->isEmpty());
}

SECTION("Clone")
{
auto newHandler = parameterHandler->clone();
REQUIRE_FALSE(newHandler->isEmpty());

SECTION("Get integer")
{
int element;
REQUIRE(newHandler->getParameter("answer_to_the_ultimate_question_of_life", element));
REQUIRE(element == 42);
}

SECTION("Get Double")
{
double element;
REQUIRE(newHandler->getParameter("pi", element));
REQUIRE(element == 3.14);
}

SECTION("Get String")
{
std::string element;
REQUIRE(newHandler->getParameter("John", element));
REQUIRE(element == "Smith");
}

SECTION("Get Vector")
{
std::vector<int> element;
REQUIRE(newHandler->getParameter("Fibonacci Numbers", element));
REQUIRE(element == std::vector<int>{1, 1, 2, 3, 5, 8, 13, 21});
}
}
}
48 changes: 48 additions & 0 deletions src/ParametersHandler/tests/ParametersHandlerYarpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,52 @@ TEST_CASE("Get parameters")
}
}

SECTION("Clone")
{
IParametersHandler::shared_ptr setGroup = std::make_shared<YarpImplementation>();
REQUIRE(parameterHandler->setGroup("CARTOONS", setGroup));
auto groupHandler = parameterHandler->getGroup("CARTOONS").lock(); //now the pointer should be lockable
REQUIRE(groupHandler);
REQUIRE(groupHandler->isEmpty());

groupHandler->setParameter("Donald's nephews", donaldsNephews);

auto newHandler = parameterHandler->clone();

SECTION("Get integer")
{
int element;
REQUIRE(newHandler->getParameter("answer_to_the_ultimate_question_of_life", element));
REQUIRE(element == 42);
}

SECTION("Get Double")
{
double element;
REQUIRE(newHandler->getParameter("pi", element));
REQUIRE(element == 3.14);
}

SECTION("Get String")
{
std::string element;
REQUIRE(newHandler->getParameter("John", element));
REQUIRE(element == "Smith");
}

SECTION("Get Vector")
{
std::vector<int> element;
REQUIRE(newHandler->getParameter("Fibonacci Numbers", element));
REQUIRE(element == fibonacciNumbers);
}

SECTION("Get group")
{
std::vector<std::string> element;
REQUIRE(newHandler->getGroup("CARTOONS").lock()->getParameter("Donald's nephews",//
element));
REQUIRE(element == donaldsNephews);
}
}
}
2 changes: 1 addition & 1 deletion src/YarpUtilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if(FRAMEWORK_COMPILE_YarpUtilities)
NAME YarpUtilities
SOURCES src/Helper.cpp src/RosPublisher.cpp
PUBLIC_HEADERS include/BipedalLocomotion/YarpUtilities/Helper.h include/BipedalLocomotion/YarpUtilities/Helper.tpp include/BipedalLocomotion/YarpUtilities/RosPublisher.h
PUBLIC_LINK_LIBRARIES ${YARP_LIBRARIES} ${iDynTree_LIBRARIES} BipedalLocomotion::GenericContainer BipedalLocomotion::ParametersHandler
PUBLIC_LINK_LIBRARIES ${YARP_LIBRARIES} ${iDynTree_LIBRARIES} BipedalLocomotion::GenericContainer BipedalLocomotion::ParametersHandler BipedalLocomotion::TextLogging
SUBDIRECTORIES tests)

endif()
Loading

0 comments on commit 665e04d

Please sign in to comment.