Skip to content

Commit 07c9f62

Browse files
Fix ReturnCode comparisons (#146)
* Add missing operators Signed-off-by: Emilio Cuesta <emiliocuesta@eprosima.com> * Add new constructor to avoid mischievous conversions Signed-off-by: Emilio Cuesta <emiliocuesta@eprosima.com> * Removing comparison operators not needed anymore Signed-off-by: Emilio Cuesta <emiliocuesta@eprosima.com> * Changing variable type for consistency Signed-off-by: Emilio Cuesta <emiliocuesta@eprosima.com> * [#24195] Added operators for ReturnCodeValue Signed-off-by: danipiza <dpizarrogallego@gmail.com> * [#24195] Solved namespace error in retcode test Signed-off-by: danipiza <dpizarrogallego@gmail.com> --------- Signed-off-by: Emilio Cuesta <emiliocuesta@eprosima.com> Signed-off-by: danipiza <dpizarrogallego@gmail.com> Co-authored-by: danipiza <dpizarrogallego@gmail.com>
1 parent 8a826bb commit 07c9f62

4 files changed

Lines changed: 96 additions & 3 deletions

File tree

cpp_utils/include/cpp_utils/ReturnCode.hpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,29 @@ class ReturnCode
5858
ReturnCode(
5959
const fastdds::dds::ReturnCode_t& value);
6060

61+
CPP_UTILS_DllAPI
62+
ReturnCode(
63+
const ReturnCodeValue& value);
64+
6165
CPP_UTILS_DllAPI
6266
std::uint32_t operator ()() const noexcept;
6367

6468
CPP_UTILS_DllAPI
6569
bool operator ==(
6670
const ReturnCode& c) const noexcept;
6771

72+
CPP_UTILS_DllAPI
73+
bool operator ==(
74+
const ReturnCodeValue& c) const noexcept;
75+
6876
CPP_UTILS_DllAPI
6977
bool operator !=(
7078
const ReturnCode& c) const noexcept;
7179

80+
CPP_UTILS_DllAPI
81+
bool operator !=(
82+
const ReturnCodeValue& c) const noexcept;
83+
7284
CPP_UTILS_DllAPI
7385
bool operator <(
7486
const ReturnCode& other) const noexcept;
@@ -84,18 +96,37 @@ class ReturnCode
8496
//! Link every ReturnCodeValue available with a string to deserialize
8597
static const std::map<ReturnCodeValue, std::string> to_string_conversion_;
8698

87-
//! \c ReturnCode value
88-
std::uint32_t value_;
89-
99+
//! \c ReturnCodeValue
100+
ReturnCodeValue value_;
90101

91102
// operator << needs access to the object
92103
CPP_UTILS_DllAPI
93104
friend std::ostream& operator <<(
94105
std::ostream& os,
95106
const ReturnCode& code);
96107

108+
CPP_UTILS_DllAPI
109+
friend bool operator ==(
110+
const ReturnCodeValue& lhs,
111+
const ReturnCode& rhs) noexcept;
112+
113+
CPP_UTILS_DllAPI
114+
friend bool operator !=(
115+
const ReturnCodeValue& lhs,
116+
const ReturnCode& rhs) noexcept;
117+
97118
};
98119

120+
CPP_UTILS_DllAPI
121+
bool operator ==(
122+
const ReturnCode::ReturnCodeValue& lhs,
123+
const ReturnCode& rhs) noexcept;
124+
125+
CPP_UTILS_DllAPI
126+
bool operator !=(
127+
const ReturnCode::ReturnCodeValue& lhs,
128+
const ReturnCode& rhs) noexcept;
129+
99130
//! \c ReturnCode to stream serializator
100131
CPP_UTILS_DllAPI
101132
std::ostream& operator <<(

cpp_utils/src/cpp/ReturnCode.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ ReturnCode::ReturnCode(
6161
}
6262
}
6363

64+
ReturnCode::ReturnCode(
65+
const ReturnCode::ReturnCodeValue& value)
66+
{
67+
value_ = value;
68+
}
69+
6470
std::uint32_t ReturnCode::operator ()() const noexcept
6571
{
6672
return value_;
@@ -72,12 +78,24 @@ bool ReturnCode::operator ==(
7278
return value_ == c.value_;
7379
}
7480

81+
bool ReturnCode::operator ==(
82+
const ReturnCode::ReturnCodeValue& c) const noexcept
83+
{
84+
return value_ == c;
85+
}
86+
7587
bool ReturnCode::operator !=(
7688
const ReturnCode& c) const noexcept
7789
{
7890
return value_ != c.value_;
7991
}
8092

93+
bool ReturnCode::operator !=(
94+
const ReturnCode::ReturnCodeValue& c) const noexcept
95+
{
96+
return value_ != c;
97+
}
98+
8199
bool ReturnCode::operator <(
82100
const ReturnCode& other) const noexcept
83101
{
@@ -89,6 +107,20 @@ bool ReturnCode::operator !() const noexcept
89107
return value_ != ReturnCode::RETCODE_OK;
90108
}
91109

110+
bool operator ==(
111+
const ReturnCode::ReturnCodeValue& lhs,
112+
const ReturnCode& rhs) noexcept
113+
{
114+
return rhs == lhs;
115+
}
116+
117+
bool operator !=(
118+
const ReturnCode::ReturnCodeValue& lhs,
119+
const ReturnCode& rhs) noexcept
120+
{
121+
return rhs != lhs;
122+
}
123+
92124
const std::map<ReturnCode::ReturnCodeValue, std::string> ReturnCode::to_string_conversion_ =
93125
{
94126
{ReturnCode::RETCODE_OK, "Ok"},

cpp_utils/test/unittest/return_code/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ set(TEST_SOURCES
2121

2222
set(TEST_LIST
2323
serializator
24+
compare_against_return_code_value_rhs
25+
compare_against_return_code_value_lhs
2426
)
2527

2628
set(TEST_EXTRA_LIBRARIES

cpp_utils/test/unittest/return_code/ReturnCodeTest.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ TEST(ReturnCodeTest, serializator)
4343
}
4444
}
4545

46+
/**
47+
* Test ReturnCode compares directly against ReturnCodeValue values.
48+
*/
49+
TEST(ReturnCodeTest, compare_against_return_code_value_rhs)
50+
{
51+
// Right side of the comparation
52+
53+
ReturnCode from_fastdds(eprosima::fastdds::dds::RETCODE_NO_DATA);
54+
55+
ASSERT_TRUE(from_fastdds == ReturnCode::RETCODE_NO_DATA);
56+
ASSERT_FALSE(from_fastdds == ReturnCode::RETCODE_ERROR);
57+
ASSERT_TRUE(from_fastdds != ReturnCode::RETCODE_ERROR);
58+
}
59+
60+
/**
61+
* Test ReturnCodeValue compares directly against ReturnCode from lhs.
62+
*/
63+
TEST(ReturnCodeTest, compare_against_return_code_value_lhs)
64+
{
65+
// Left side of the comparation
66+
67+
ReturnCode ret(ReturnCode::RETCODE_NOT_ENABLED);
68+
69+
ASSERT_TRUE(ReturnCode::RETCODE_NOT_ENABLED == ret);
70+
ASSERT_TRUE(ReturnCode::RETCODE_OK != ret);
71+
ASSERT_FALSE(ReturnCode::RETCODE_OK == ret);
72+
}
73+
4674
int main(
4775
int argc,
4876
char** argv)

0 commit comments

Comments
 (0)