Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions cpp_utils/include/cpp_utils/ReturnCode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,29 @@ class ReturnCode
ReturnCode(
const fastdds::dds::ReturnCode_t& value);

CPP_UTILS_DllAPI
ReturnCode(
const ReturnCodeValue& value);

CPP_UTILS_DllAPI
std::uint32_t operator ()() const noexcept;

CPP_UTILS_DllAPI
bool operator ==(
const ReturnCode& c) const noexcept;

CPP_UTILS_DllAPI
bool operator ==(
const ReturnCodeValue& c) const noexcept;

CPP_UTILS_DllAPI
bool operator !=(
const ReturnCode& c) const noexcept;

CPP_UTILS_DllAPI
bool operator !=(
const ReturnCodeValue& c) const noexcept;

CPP_UTILS_DllAPI
bool operator <(
const ReturnCode& other) const noexcept;
Expand All @@ -84,18 +96,37 @@ class ReturnCode
//! Link every ReturnCodeValue available with a string to deserialize
static const std::map<ReturnCodeValue, std::string> to_string_conversion_;

//! \c ReturnCode value
std::uint32_t value_;

//! \c ReturnCodeValue
ReturnCodeValue value_;

// operator << needs access to the object
CPP_UTILS_DllAPI
friend std::ostream& operator <<(
std::ostream& os,
const ReturnCode& code);

CPP_UTILS_DllAPI
friend bool operator ==(
const ReturnCodeValue& lhs,
const ReturnCode& rhs) noexcept;

CPP_UTILS_DllAPI
friend bool operator !=(
const ReturnCodeValue& lhs,
const ReturnCode& rhs) noexcept;

};

CPP_UTILS_DllAPI
bool operator ==(
const ReturnCode::ReturnCodeValue& lhs,
const ReturnCode& rhs) noexcept;

CPP_UTILS_DllAPI
bool operator !=(
const ReturnCode::ReturnCodeValue& lhs,
const ReturnCode& rhs) noexcept;

//! \c ReturnCode to stream serializator
CPP_UTILS_DllAPI
std::ostream& operator <<(
Expand Down
32 changes: 32 additions & 0 deletions cpp_utils/src/cpp/ReturnCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ ReturnCode::ReturnCode(
}
}

ReturnCode::ReturnCode(
const ReturnCode::ReturnCodeValue& value)
{
value_ = value;
}

std::uint32_t ReturnCode::operator ()() const noexcept
{
return value_;
Expand All @@ -72,12 +78,24 @@ bool ReturnCode::operator ==(
return value_ == c.value_;
}

bool ReturnCode::operator ==(
const ReturnCode::ReturnCodeValue& c) const noexcept
{
return value_ == c;
}

bool ReturnCode::operator !=(
const ReturnCode& c) const noexcept
{
return value_ != c.value_;
}

bool ReturnCode::operator !=(
const ReturnCode::ReturnCodeValue& c) const noexcept
{
return value_ != c;
}

bool ReturnCode::operator <(
const ReturnCode& other) const noexcept
{
Expand All @@ -89,6 +107,20 @@ bool ReturnCode::operator !() const noexcept
return value_ != ReturnCode::RETCODE_OK;
}

bool operator ==(
const ReturnCode::ReturnCodeValue& lhs,
const ReturnCode& rhs) noexcept
{
return rhs == lhs;
}

bool operator !=(
const ReturnCode::ReturnCodeValue& lhs,
const ReturnCode& rhs) noexcept
{
return rhs != lhs;
}

const std::map<ReturnCode::ReturnCodeValue, std::string> ReturnCode::to_string_conversion_ =
{
{ReturnCode::RETCODE_OK, "Ok"},
Expand Down
2 changes: 2 additions & 0 deletions cpp_utils/test/unittest/return_code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ set(TEST_SOURCES

set(TEST_LIST
serializator
compare_against_return_code_value_rhs
compare_against_return_code_value_lhs
)

set(TEST_EXTRA_LIBRARIES
Expand Down
28 changes: 28 additions & 0 deletions cpp_utils/test/unittest/return_code/ReturnCodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ TEST(ReturnCodeTest, serializator)
}
}

/**
* Test ReturnCode compares directly against ReturnCodeValue values.
*/
TEST(ReturnCodeTest, compare_against_return_code_value_rhs)
{
// Right side of the comparation

ReturnCode from_fastdds(eprosima::fastdds::dds::RETCODE_NO_DATA);

ASSERT_TRUE(from_fastdds == ReturnCode::RETCODE_NO_DATA);
ASSERT_FALSE(from_fastdds == ReturnCode::RETCODE_ERROR);
ASSERT_TRUE(from_fastdds != ReturnCode::RETCODE_ERROR);
}

/**
* Test ReturnCodeValue compares directly against ReturnCode from lhs.
*/
TEST(ReturnCodeTest, compare_against_return_code_value_lhs)
{
// Left side of the comparation

ReturnCode ret(ReturnCode::RETCODE_NOT_ENABLED);

ASSERT_TRUE(ReturnCode::RETCODE_NOT_ENABLED == ret);
ASSERT_TRUE(ReturnCode::RETCODE_OK != ret);
ASSERT_FALSE(ReturnCode::RETCODE_OK == ret);
}

int main(
int argc,
char** argv)
Expand Down