Skip to content

Commit

Permalink
Merge pull request #3179 from robotology/ReturnValue_improvements
Browse files Browse the repository at this point in the history
ReturnValue improvements
  • Loading branch information
randaz81 authored Feb 14, 2025
2 parents cfeb64a + 0af3ea7 commit a98ec14
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/devices/audioFromFileDevice/AudioFromFileDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 8 additions & 2 deletions src/libYARP_dev/src/yarp/dev/ReturnValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ReturnValue::ReturnValue()
{
}

#ifndef DISABLE_BOOL_INPUT
#if !YARP_RETURNVALUE_DISABLE_BOOL_INPUT
ReturnValue::ReturnValue(const bool& val)
{
if (val)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
26 changes: 13 additions & 13 deletions src/libYARP_dev/src/yarp/dev/ReturnValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
#include <source_location>
#include <string>

// 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 {

Expand All @@ -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:
Expand Down
16 changes: 14 additions & 2 deletions src/libYARP_dev/tests/ReturnValueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit a98ec14

Please sign in to comment.