diff --git a/src/devices/RemoteControlBoard/RemoteControlBoard.cpp b/src/devices/RemoteControlBoard/RemoteControlBoard.cpp index 1d74842125..542bcef9fa 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_ret_value RemoteControlBoard::getTemperature (int m, double* val) { return get1V1I1D(VOCAB_TEMPERATURE, m, val); } -bool RemoteControlBoard::getTemperatures (double *vals) +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 549d73f352..6e49668736 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::dev::yarp_ret_value getTemperature(int m, double* val) override; + yarp::dev::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 6d71743751..cd4c6bbfc4 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::dev::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::dev::yarp_ret_value ControlBoardRemapper::getTemperatures(double *vals) { bool ret=true; for(int l=0;l #include #include +#include namespace yarp::dev { class IMotorRaw; @@ -41,17 +42,17 @@ 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::dev::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::dev::yarp_ret_value getTemperaturesRaw(double *vals)=0; /** - * Retreives the current temperature limit for a specific motor. + * Retrieves the current temperature limit for a specific motor. * The specific behavior of the motor when the temperature limit is exceeded depends on the implementation (power off recommended) * @param m motor number * @param temp the current temperature limit. @@ -111,17 +112,17 @@ 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::dev::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::dev::yarp_ret_value getTemperatures(double *vals)=0; /** - * Retreives the current temperature limit for a specific motor. + * Retrieves the current temperature limit for a specific motor. * The specific behavior of the motor when the temperature limit is exceeded depends on the implementation (power off recommended) * @param m motor number * @param temp the current temperature limit. diff --git a/src/libYARP_dev/src/yarp/dev/ImplementMotor.cpp b/src/libYARP_dev/src/yarp/dev/ImplementMotor.cpp index 4828b26e66..beee9bb057 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::dev::yarp_ret_value ImplementMotor::getTemperature(int m, double* value) { JOINTIDCHECK - bool ret; + yarp::dev::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::dev::yarp_ret_value ImplementMotor::getTemperatures(double *v) { yarp::dev::impl::Buffer buffValues = doubleBuffManager->getBuffer(); - bool ret = imotor->getTemperaturesRaw(buffValues.getData()); + yarp::dev::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 21694a6dba..d2b3d1c8ea 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::dev::yarp_ret_value getTemperature (int m, double* val) override; + yarp::dev::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_dev/src/yarp/dev/ReturnValue.cpp b/src/libYARP_dev/src/yarp/dev/ReturnValue.cpp new file mode 100644 index 0000000000..5084edc768 --- /dev/null +++ b/src/libYARP_dev/src/yarp/dev/ReturnValue.cpp @@ -0,0 +1,94 @@ +/* + * SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT) + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include + +using namespace yarp::dev; + +yarp_ret_value::yarp_ret_value() +{ +} + +yarp_ret_value::yarp_ret_value(const bool& val) +{ + if (val) + { + value_b = return_code::return_value_ok; + } + else + { + value_b = return_code::return_value_error_generic; + } +} + +/*/yarp_ret_value::yarp_ret_value(const yarp_ret_value& other) +{ + this->value_b=other.value_b; +}*/ + +yarp_ret_value& yarp_ret_value::operator&=(const yarp_ret_value& other) +{ + if (other.operator bool() == true) + { + return *this; + } + else + { + value_b = return_code::return_value_error_generic; + } + return *this; +} + +yarp_ret_value& yarp_ret_value::operator=(const bool& bool_val) +{ + if (bool_val) + { + value_b = return_code::return_value_ok; + } + else + { + value_b = return_code::return_value_error_generic; + } + return *this; +} + +std::string yarp_ret_value::toString() +{ + switch (value_b) + { + case return_code::return_value_ok: + return std::string("ok"); + case return_code::return_value_unitialized: + return std::string("return_value_unitialized"); + case return_code::return_value_error_deprecated: + return std::string("return_value_error_deprecated"); + case return_code::return_value_error_generic: + return std::string("return_value_error_generic"); + case return_code::return_value_error_method_failed: + return std::string("return_value_error_method_fail"); + case return_code::return_value_error_not_implemented_by_device: + return std::string("return_value_error_not_implemented_by_device"); + case return_code::return_value_error_nws_nwc_communication_error: + return std::string("return_value_error_nws_nwc_communication_error"); + default: + return std::string("unknown"); + } +} + +yarp_ret_value::operator bool() const +{ + return value_b == return_code::return_value_ok; +} + +yarp_ret_value::yarp_ret_value(return_code code) +{ + value_b = code; +} + +bool yarp_ret_value::operator == (const return_code& code) const +{ + if (code == this->value_b) return true; + return false; +} diff --git a/src/libYARP_dev/src/yarp/dev/ReturnValue.h b/src/libYARP_dev/src/yarp/dev/ReturnValue.h new file mode 100644 index 0000000000..3f0a231ce2 --- /dev/null +++ b/src/libYARP_dev/src/yarp/dev/ReturnValue.h @@ -0,0 +1,46 @@ +/* + * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) + * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef YARP_RET_VALUE_H +#define YARP_RET_VALUE_H + +#include +#include + +namespace yarp::dev { + +class YARP_dev_API yarp_ret_value +{ + public: + enum class YARP_dev_API return_code + { + return_value_ok = 0, + return_value_error_generic = 1, + return_value_error_not_implemented_by_device = 2, + return_value_error_nws_nwc_communication_error = 3, + return_value_error_deprecated = 4, + return_value_error_method_failed = 5, + return_value_unitialized = 100 + }; + + private: + return_code value_b = return_code::return_value_unitialized; + + public: + yarp_ret_value(); + yarp_ret_value(const bool& val); + yarp_ret_value(return_code code); + yarp_ret_value(const yarp_ret_value& other) = default; + yarp_ret_value& operator&=(const yarp_ret_value& other); + yarp_ret_value& operator=(const bool& bool_val); + bool operator == (const return_code& code) const; + std::string toString(); + operator bool() const; +}; + +} + +#endif // YARP_RET_VALUE_H diff --git a/src/libYARP_dev/tests/CMakeLists.txt b/src/libYARP_dev/tests/CMakeLists.txt index c06251f2e6..79305221db 100644 --- a/src/libYARP_dev/tests/CMakeLists.txt +++ b/src/libYARP_dev/tests/CMakeLists.txt @@ -8,6 +8,7 @@ target_sources(harness_dev PRIVATE MapGrid2DTest.cpp PolyDriverTest.cpp + ReturnValueTest.cpp ) target_link_libraries(harness_dev diff --git a/src/libYARP_dev/tests/ReturnValueTest.cpp b/src/libYARP_dev/tests/ReturnValueTest.cpp new file mode 100644 index 0000000000..201ced223d --- /dev/null +++ b/src/libYARP_dev/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::dev; + +TEST_CASE("dev::ReturnValue", "[yarp::dev]") +{ + 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); + } +}