Skip to content

Commit bcee9a9

Browse files
committed
[#24195] Added operators for ReturnCodeValue
Signed-off-by: danipiza <dpizarrogallego@gmail.com>
1 parent 668352e commit bcee9a9

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

cpp_utils/include/cpp_utils/ReturnCode.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,18 @@ class ReturnCode
6969
bool operator ==(
7070
const ReturnCode& c) const noexcept;
7171

72+
CPP_UTILS_DllAPI
73+
bool operator ==(
74+
const ReturnCodeValue& c) const noexcept;
75+
7276
CPP_UTILS_DllAPI
7377
bool operator !=(
7478
const ReturnCode& c) const noexcept;
7579

80+
CPP_UTILS_DllAPI
81+
bool operator !=(
82+
const ReturnCodeValue& c) const noexcept;
83+
7684
CPP_UTILS_DllAPI
7785
bool operator <(
7886
const ReturnCode& other) const noexcept;
@@ -97,8 +105,28 @@ class ReturnCode
97105
std::ostream& os,
98106
const ReturnCode& code);
99107

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+
100118
};
101119

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+
102130
//! \c ReturnCode to stream serializator
103131
CPP_UTILS_DllAPI
104132
std::ostream& operator <<(

cpp_utils/src/cpp/ReturnCode.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,24 @@ bool ReturnCode::operator ==(
7878
return value_ == c.value_;
7979
}
8080

81+
bool ReturnCode::operator ==(
82+
const ReturnCode::ReturnCodeValue& c) const noexcept
83+
{
84+
return value_ == c;
85+
}
86+
8187
bool ReturnCode::operator !=(
8288
const ReturnCode& c) const noexcept
8389
{
8490
return value_ != c.value_;
8591
}
8692

93+
bool ReturnCode::operator !=(
94+
const ReturnCode::ReturnCodeValue& c) const noexcept
95+
{
96+
return value_ != c;
97+
}
98+
8799
bool ReturnCode::operator <(
88100
const ReturnCode& other) const noexcept
89101
{
@@ -95,6 +107,20 @@ bool ReturnCode::operator !() const noexcept
95107
return value_ != ReturnCode::RETCODE_OK;
96108
}
97109

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+
98124
const std::map<ReturnCode::ReturnCodeValue, std::string> ReturnCode::to_string_conversion_ =
99125
{
100126
{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(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)