From 0af3ea788f3e98d4f3748a2abb2098dc2c3a682f Mon Sep 17 00:00:00 2001 From: Marco Randazzo Date: Fri, 14 Feb 2025 09:29:02 +0100 Subject: [PATCH] Improvements to the macros controlling the behaviour of yarp::dev::ReturnValue --- .../AudioFromFileDevice.cpp | 2 +- src/libYARP_dev/src/yarp/dev/ReturnValue.cpp | 10 +++++-- src/libYARP_dev/src/yarp/dev/ReturnValue.h | 26 +++++++++---------- src/libYARP_dev/tests/ReturnValueTest.cpp | 16 ++++++++++-- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/devices/audioFromFileDevice/AudioFromFileDevice.cpp b/src/devices/audioFromFileDevice/AudioFromFileDevice.cpp index 3c1c9ce343..ebdc4f452d 100644 --- a/src/devices/audioFromFileDevice/AudioFromFileDevice.cpp +++ b/src/devices/audioFromFileDevice/AudioFromFileDevice.cpp @@ -103,7 +103,7 @@ bool AudioFromFileDevice::close() ReturnValue AudioFromFileDevice::stopRecording() { ReturnValue b = AudioRecorderDeviceBase::stopRecording(); - if (b && m_reset_on_stop) + if (m_reset_on_stop) { m_bpnt=0; } diff --git a/src/libYARP_dev/src/yarp/dev/ReturnValue.cpp b/src/libYARP_dev/src/yarp/dev/ReturnValue.cpp index c51a3571a5..65660fa021 100644 --- a/src/libYARP_dev/src/yarp/dev/ReturnValue.cpp +++ b/src/libYARP_dev/src/yarp/dev/ReturnValue.cpp @@ -14,7 +14,7 @@ ReturnValue::ReturnValue() { } -#ifndef DISABLE_BOOL_INPUT +#if !YARP_RETURNVALUE_DISABLE_BOOL_INPUT ReturnValue::ReturnValue(const bool& val) { if (val) @@ -68,7 +68,7 @@ ReturnValue& ReturnValue::operator &= (const ReturnValue& other) return *this; } -#ifndef DISABLE_BOOL_INPUT +#if !YARP_RETURNVALUE_DISABLE_BOOL_INPUT ReturnValue& ReturnValue::operator=(const bool& bool_val) { if (bool_val) @@ -123,6 +123,12 @@ bool ReturnValue::operator == (const return_code& code) const return false; } +bool ReturnValue::operator == (const ReturnValue& value) const +{ + if (value == this->value_b) return true; + return false; +} + bool ReturnValue::read(yarp::os::ConnectionReader& connection) { connection.convertTextMode(); diff --git a/src/libYARP_dev/src/yarp/dev/ReturnValue.h b/src/libYARP_dev/src/yarp/dev/ReturnValue.h index cd1a8509d7..8881d45e80 100644 --- a/src/libYARP_dev/src/yarp/dev/ReturnValue.h +++ b/src/libYARP_dev/src/yarp/dev/ReturnValue.h @@ -12,17 +12,16 @@ #include #include -// The following macro is used for testing/development purposes only, but it must be generally not enabled. -// If enabled, a bool value cannot be automatically converted to ReturnValue -// In this way, the developer can check if some old devices/interfaces do unwanted automatic -// conversions and fix them. - #define DISABLE_BOOL_INPUT +// If this macro is enabled (default value=enabled), a bool value cannot be automatically converted to ReturnValue. +// Automatic conversions of this type should be generally avoided, because they make the code less clear. +// It is thus preferable that user assigns a precise value to a ReturnValue. +#define YARP_RETURNVALUE_DISABLE_BOOL_INPUT 1 -// The following macro is used for testing/development purposes only, but it must be generally not enabled. -// If enabled, a ReturnValue cannot be automatically converted to bool unless explicitly requested -// using the bool() operator. +// If this macro is enabled (default value=disabled), a ReturnValue cannot be automatically converted to bool +// unless explicitly requested using the bool() operator. +// The following macro is used for testing/development purposes only. // If enabled, it will break backward compatibility with user-application code. -// #define DISABLE_BOOL_OUTPUT +#define YARP_RETURNVALUE_DISABLE_BOOL_OUTPUT 0 namespace yarp::dev { @@ -47,22 +46,23 @@ class YARP_dev_API ReturnValue : public yarp::os::Portable public: ReturnValue(); ~ReturnValue() = default; -#ifndef DISABLE_BOOL_INPUT +#if !YARP_RETURNVALUE_DISABLE_BOOL_INPUT ReturnValue(const bool& val); #endif ReturnValue(return_code code); ReturnValue(const ReturnValue& other) = default; ReturnValue& operator && (const ReturnValue& other); ReturnValue& operator &= (const ReturnValue& other); -#ifndef DISABLE_BOOL_INPUT +#if !YARP_RETURNVALUE_DISABLE_BOOL_INPUT ReturnValue& operator=(const bool& bool_val); #endif bool operator == (const return_code& code) const; + bool operator == (const ReturnValue& value) const; std::string toString(); -#ifndef DISABLE_BOOL_OUTPUT +#if !YARP_RETURNVALUE_DISABLE_BOOL_OUTPUT operator bool() const; #else - explicit operator bool const(); + explicit operator bool() const; #endif public: diff --git a/src/libYARP_dev/tests/ReturnValueTest.cpp b/src/libYARP_dev/tests/ReturnValueTest.cpp index 3441e352a4..3c144676e7 100644 --- a/src/libYARP_dev/tests/ReturnValueTest.cpp +++ b/src/libYARP_dev/tests/ReturnValueTest.cpp @@ -23,7 +23,7 @@ ReturnValue test_method2() TEST_CASE("dev::ReturnValue", "[yarp::dev]") { -#ifndef DISABLE_BOOL_INPUT +#if !YARP_RETURNVALUE_DISABLE_BOOL_INPUT SECTION("test block 1") { ReturnValue val_f1(false); @@ -94,7 +94,7 @@ TEST_CASE("dev::ReturnValue", "[yarp::dev]") CHECK(val3 == ReturnValue::return_code::return_value_error_method_failed); } -#ifndef DISABLE_BOOL_INPUT +#ifndef YARP_RETURNVALUE_DISABLE_BOOL_INPUT SECTION("test block 4a") { ReturnValue val_f1(false); @@ -116,6 +116,18 @@ TEST_CASE("dev::ReturnValue", "[yarp::dev]") CHECK(bool_t2 == true); } +#ifndef YARP_RETURNVALUE_DISABLE_BOOL_INPUT + SECTION("test block 4c") + { + ReturnValue val_f1 = false; + ReturnValue val_t1 = true; + bool bool_f1 = val_f1; + bool bool_t1 = val_t1; + CHECK (bool_f1 == false); + CHECK (bool_t1 == true); + } +#endif + SECTION("test block 5") { ReturnValue val_f(ReturnValue::return_code::return_value_error_method_failed);