From d60a4829671580927c9f09e9246b34afd0e8762e Mon Sep 17 00:00:00 2001 From: Marco Randazzo Date: Tue, 21 Nov 2023 14:12:13 +0100 Subject: [PATCH] New class for return values of interface methods. --- .../RemoteControlBoard/RemoteControlBoard.cpp | 4 +- .../RemoteControlBoard/RemoteControlBoard.h | 4 +- .../ControlBoardRemapper.cpp | 4 +- .../ControlBoardRemapper.h | 4 +- .../fakeMotionControl/fakeMotionControl.cpp | 8 +- .../fakeMotionControl/fakeMotionControl.h | 4 +- src/libYARP_dev/src/yarp/dev/IMotor.h | 9 +- .../src/yarp/dev/ImplementMotor.cpp | 8 +- src/libYARP_dev/src/yarp/dev/ImplementMotor.h | 4 +- src/libYARP_os/src/CMakeLists.txt | 2 + src/libYARP_os/tests/CMakeLists.txt | 1 + src/libYARP_os/tests/ReturnValueTest.cpp | 101 ++++++++++++++++++ 12 files changed, 129 insertions(+), 24 deletions(-) create mode 100644 src/libYARP_os/tests/ReturnValueTest.cpp diff --git a/src/devices/RemoteControlBoard/RemoteControlBoard.cpp b/src/devices/RemoteControlBoard/RemoteControlBoard.cpp index 1d748421255..1fcf5ee135a 100644 --- a/src/devices/RemoteControlBoard/RemoteControlBoard.cpp +++ b/src/devices/RemoteControlBoard/RemoteControlBoard.cpp @@ -1567,12 +1567,12 @@ bool RemoteControlBoard::getNumberOfMotors(int *num) return get1V1I(VOCAB_MOTORS_NUMBER, *num); } -bool RemoteControlBoard::getTemperature (int m, double* val) +yarp::os::yarp_ret_value RemoteControlBoard::getTemperature (int m, double* val) { return get1V1I1D(VOCAB_TEMPERATURE, m, val); } -bool RemoteControlBoard::getTemperatures (double *vals) +yarp::os::yarp_ret_value RemoteControlBoard::getTemperatures (double *vals) { return get1VDA(VOCAB_TEMPERATURES, vals); } diff --git a/src/devices/RemoteControlBoard/RemoteControlBoard.h b/src/devices/RemoteControlBoard/RemoteControlBoard.h index 549d73f352b..68011f0da41 100644 --- a/src/devices/RemoteControlBoard/RemoteControlBoard.h +++ b/src/devices/RemoteControlBoard/RemoteControlBoard.h @@ -329,8 +329,8 @@ class RemoteControlBoard : // IMotor bool getNumberOfMotors(int *num) override; - bool getTemperature(int m, double* val) override; - bool getTemperatures(double *vals) override; + yarp::os::yarp_ret_value getTemperature(int m, double* val) override; + yarp::os::yarp_ret_value getTemperatures(double *vals) override; bool getTemperatureLimit (int m, double* val) override; bool setTemperatureLimit (int m, const double val) override; bool getGearboxRatio(int m, double* val) override; diff --git a/src/devices/controlBoardRemapper/ControlBoardRemapper.cpp b/src/devices/controlBoardRemapper/ControlBoardRemapper.cpp index 6d717437514..8a0a42ff285 100644 --- a/src/devices/controlBoardRemapper/ControlBoardRemapper.cpp +++ b/src/devices/controlBoardRemapper/ControlBoardRemapper.cpp @@ -1986,7 +1986,7 @@ bool ControlBoardRemapper::getNumberOfMotors(int *num) return true; } -bool ControlBoardRemapper::getTemperature(int m, double* val) +yarp::os::yarp_ret_value ControlBoardRemapper::getTemperature(int m, double* val) { int off=(int)remappedControlBoards.lut[m].axisIndexInSubControlBoard; size_t subIndex=remappedControlBoards.lut[m].subControlBoardIndex; @@ -2006,7 +2006,7 @@ bool ControlBoardRemapper::getTemperature(int m, double* val) return false; } -bool ControlBoardRemapper::getTemperatures(double *vals) +yarp::os::yarp_ret_value ControlBoardRemapper::getTemperatures(double *vals) { bool ret=true; for(int l=0;l #include #include +#include namespace yarp::dev { class IMotorRaw; @@ -41,14 +42,14 @@ class YARP_dev_API yarp::dev::IMotorRaw * @param val retrieved motor temperature * @return true/false */ - virtual bool getTemperatureRaw(int m, double* val)=0; + virtual yarp::os::yarp_ret_value getTemperatureRaw(int m, double* val)=0; /** * Get temperature of all the motors. * @param vals pointer to an array containing all motor temperatures * @return true/false */ - virtual bool getTemperaturesRaw(double *vals)=0; + virtual yarp::os::yarp_ret_value getTemperaturesRaw(double *vals)=0; /** * Retreives the current temperature limit for a specific motor. @@ -111,14 +112,14 @@ class YARP_dev_API yarp::dev::IMotor * @param val retrieved motor temperature * @return true/false */ - virtual bool getTemperature(int m, double *val)=0; + virtual yarp::os::yarp_ret_value getTemperature(int m, double *val)=0; /** * Get temperature of all the motors. * @param vals pointer to an array containing all motor temperatures * @return true/false */ - virtual bool getTemperatures(double *vals)=0; + virtual yarp::os::yarp_ret_value getTemperatures(double *vals)=0; /** * Retreives the current temperature limit for a specific motor. diff --git a/src/libYARP_dev/src/yarp/dev/ImplementMotor.cpp b/src/libYARP_dev/src/yarp/dev/ImplementMotor.cpp index 4828b26e661..7e1af8f448f 100644 --- a/src/libYARP_dev/src/yarp/dev/ImplementMotor.cpp +++ b/src/libYARP_dev/src/yarp/dev/ImplementMotor.cpp @@ -67,10 +67,10 @@ bool ImplementMotor::getNumberOfMotors(int *num) return true; } -bool ImplementMotor::getTemperature(int m, double* value) +yarp::os::yarp_ret_value ImplementMotor::getTemperature(int m, double* value) { JOINTIDCHECK - bool ret; + yarp::os::yarp_ret_value ret; int k=castToMapper(helper)->toHw(m); ret=imotor->getTemperatureRaw(k, value); @@ -122,11 +122,11 @@ bool ImplementMotor::setGearboxRatio(int m, const double value) return ret; } -bool ImplementMotor::getTemperatures(double *v) +yarp::os::yarp_ret_value ImplementMotor::getTemperatures(double *v) { yarp::dev::impl::Buffer buffValues = doubleBuffManager->getBuffer(); - bool ret = imotor->getTemperaturesRaw(buffValues.getData()); + yarp::os::yarp_ret_value ret = imotor->getTemperaturesRaw(buffValues.getData()); for (size_t i=0; i< buffValues.getSize(); i++) { int k = castToMapper(helper)->toHw(i); diff --git a/src/libYARP_dev/src/yarp/dev/ImplementMotor.h b/src/libYARP_dev/src/yarp/dev/ImplementMotor.h index 21694a6dbad..12006175d34 100644 --- a/src/libYARP_dev/src/yarp/dev/ImplementMotor.h +++ b/src/libYARP_dev/src/yarp/dev/ImplementMotor.h @@ -54,8 +54,8 @@ class YARP_dev_API yarp::dev::ImplementMotor: public IMotor bool getNumberOfMotors(int *num) override; - bool getTemperature (int m, double* val) override; - bool getTemperatures (double *vals) override; + yarp::os::yarp_ret_value getTemperature (int m, double* val) override; + yarp::os::yarp_ret_value getTemperatures (double *vals) override; bool getTemperatureLimit (int m, double* val) override; bool setTemperatureLimit (int m, const double val) override; bool getGearboxRatio (int m, double* val) override; diff --git a/src/libYARP_os/src/CMakeLists.txt b/src/libYARP_os/src/CMakeLists.txt index 440c3f186dd..9b410c9172a 100644 --- a/src/libYARP_os/src/CMakeLists.txt +++ b/src/libYARP_os/src/CMakeLists.txt @@ -96,6 +96,7 @@ set(YARP_os_HDRS yarp/os/Random.h yarp/os/ResourceFinder.h yarp/os/ResourceFinderOptions.h + yarp/os/ReturnValue.h yarp/os/RFModule.h yarp/os/RFPlugin.h yarp/os/RosNameSpace.h @@ -210,6 +211,7 @@ set(YARP_os_SRCS yarp/os/Random.cpp yarp/os/ResourceFinder.cpp yarp/os/ResourceFinderOptions.cpp + yarp/os/ReturnValue.cpp yarp/os/RFModule.cpp yarp/os/RFPlugin.cpp yarp/os/RosNameSpace.cpp diff --git a/src/libYARP_os/tests/CMakeLists.txt b/src/libYARP_os/tests/CMakeLists.txt index 95405be88f5..545b92e5d00 100644 --- a/src/libYARP_os/tests/CMakeLists.txt +++ b/src/libYARP_os/tests/CMakeLists.txt @@ -28,6 +28,7 @@ target_sources(harness_os PRIVATE PropertyTest.cpp PublisherTest.cpp ResourceFinderTest.cpp + ReturnValueTest.cpp RFModuleTest.cpp RouteTest.cpp SemaphoreTest.cpp diff --git a/src/libYARP_os/tests/ReturnValueTest.cpp b/src/libYARP_os/tests/ReturnValueTest.cpp new file mode 100644 index 00000000000..bcdf15a0c94 --- /dev/null +++ b/src/libYARP_os/tests/ReturnValueTest.cpp @@ -0,0 +1,101 @@ +/* + * SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT) + * SPDX-License-Identifier: BSD-3-Clause + */ + + +#include + +#include +#include + +using namespace yarp::os; + +TEST_CASE("os::ReturnValue", "[yarp::os]") +{ + SECTION("test block 1") + { + yarp_ret_value val_f1(false); + yarp_ret_value val_f2(false); + + yarp_ret_value val_t1(true); + yarp_ret_value val_t2(true); + + CHECK(val_f1 == val_f2); + CHECK(val_t1 == val_t2); + CHECK(val_f1 != val_t1); + CHECK(val_t1); + CHECK(!val_f1); + + std::string sf = val_f1.toString(); + std::string st = val_t1.toString(); + } + + SECTION("test block 2") + { + yarp_ret_value val1; + CHECK(val1 == yarp_ret_value::return_code::return_value_unitialized); + + std::string s; + val1 = yarp_ret_value::return_code::return_value_ok; + s = val1.toString(); + CHECK(val1); + CHECK(s!="unknown"); + + val1 = yarp_ret_value::return_code::return_value_error_generic; + s = val1.toString(); + CHECK(!val1); + CHECK(s != "unknown"); + + val1 = yarp_ret_value::return_code::return_value_error_method_failed; + s = val1.toString(); + CHECK(!val1); + CHECK(s != "unknown"); + + val1 = yarp_ret_value::return_code::return_value_error_not_implemented_by_device; + s = val1.toString(); + CHECK(!val1); + CHECK(s != "unknown"); + + val1 = yarp_ret_value::return_code::return_value_error_nws_nwc_communication_error; + s = val1.toString(); + CHECK(!val1); + CHECK(s != "unknown"); + + val1 = yarp_ret_value::return_code::return_value_unitialized; + s = val1.toString(); + CHECK(!val1); + CHECK(s != "unknown"); + } + + SECTION("test block 3") + { + yarp_ret_value val1; + val1 = yarp_ret_value::return_code::return_value_ok; + yarp_ret_value val2(val1); + CHECK(val2); + CHECK(val2 == yarp_ret_value::return_code::return_value_ok); + + val1 = yarp_ret_value::return_code::return_value_error_method_failed; + yarp_ret_value val3 = val1; + CHECK(!val3); + CHECK(val3 == yarp_ret_value::return_code::return_value_error_method_failed); + } + + SECTION("test block 4") + { + yarp_ret_value val_f1(false); + yarp_ret_value val_t1(true); + bool bool_f1 = val_f1; + bool bool_t1 = val_t1; + CHECK (bool_f1 == false); + CHECK (bool_t1 == true); + + yarp_ret_value val_f2(yarp_ret_value::return_code::return_value_error_method_failed); + yarp_ret_value val_t2(yarp_ret_value::return_code::return_value_ok); + bool bool_f2 = val_f2; + bool bool_t2 = val_t2; + CHECK(bool_f2 == false); + CHECK(bool_t2 == true); + } +}