Skip to content

Commit cd0d7e5

Browse files
mergify[bot]saikishorchristophfroehlich
authored
Fix missing copy and move operations of data_type_ variable (backport #2903) (#2905)
--------- Co-authored-by: Sai Kishor Kothakota <[email protected]> Co-authored-by: Christoph Froehlich <[email protected]>
1 parent c5932f4 commit cd0d7e5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

hardware_interface/include/hardware_interface/handle.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ class Handle
638638
interface_name_ = other.interface_name_;
639639
handle_name_ = other.handle_name_;
640640
value_ = other.value_;
641+
data_type_ = other.data_type_;
641642
if (std::holds_alternative<std::monostate>(value_))
642643
{
643644
value_ptr_ = other.value_ptr_;
@@ -655,10 +656,12 @@ class Handle
655656
std::swap(first.interface_name_, second.interface_name_);
656657
std::swap(first.handle_name_, second.handle_name_);
657658
std::swap(first.value_, second.value_);
659+
std::swap(first.data_type_, second.data_type_);
658660
std::swap(first.value_ptr_, second.value_ptr_);
659661
}
660662

661663
protected:
664+
/// @note The methods copy and swap need to be updated, if new members are added.
662665
std::string prefix_name_;
663666
std::string interface_name_;
664667
std::string handle_name_;

hardware_interface/test/test_handle.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,36 @@ TEST(TestHandle, interface_description_bool_data_type)
234234
ASSERT_THROW({ std::ignore = handle.get_optional<double>(); }, std::runtime_error);
235235
}
236236

237+
TEST(TestHandle, interface_description_bool_data_type_copy)
238+
{
239+
const std::string collision_interface = "collision";
240+
const std::string itf_name = "joint1";
241+
InterfaceInfo info;
242+
info.name = collision_interface;
243+
info.data_type = "bool";
244+
InterfaceDescription interface_descr(itf_name, info);
245+
StateInterface handle{interface_descr};
246+
247+
auto handle_copy = handle; // Copy constructor
248+
249+
ASSERT_EQ(hardware_interface::HandleDataType::BOOL, interface_descr.get_data_type());
250+
ASSERT_EQ(hardware_interface::HandleDataType::BOOL, handle_copy.get_data_type());
251+
EXPECT_EQ(handle_copy.get_name(), itf_name + "/" + collision_interface);
252+
EXPECT_EQ(handle_copy.get_interface_name(), collision_interface);
253+
EXPECT_EQ(handle_copy.get_prefix_name(), itf_name);
254+
EXPECT_NO_THROW({ std::ignore = handle_copy.get_optional<bool>(); });
255+
ASSERT_FALSE(handle_copy.get_optional<bool>().value()) << "Default value should be false";
256+
ASSERT_TRUE(handle_copy.set_value(true));
257+
ASSERT_TRUE(handle_copy.get_optional<bool>().value());
258+
ASSERT_TRUE(handle_copy.set_value(false));
259+
ASSERT_FALSE(handle_copy.get_optional<bool>().value());
260+
261+
// Test the assertions
262+
ASSERT_THROW({ std::ignore = handle.set_value(-1.0); }, std::runtime_error);
263+
ASSERT_THROW({ std::ignore = handle.set_value(0.0); }, std::runtime_error);
264+
EXPECT_THROW({ std::ignore = handle.get_optional<double>(); }, std::runtime_error);
265+
}
266+
237267
TEST(TestHandle, handle_constructor_double_data_type)
238268
{
239269
const std::string POSITION_INTERFACE = "position";

0 commit comments

Comments
 (0)