Skip to content

Commit ff0dc4d

Browse files
authored
Fix missing copy and move operations of data_type_ variable (#2903)
1 parent eaee0da commit ff0dc4d

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

hardware_interface/include/hardware_interface/handle.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ class Handle
596596
interface_name_ = other.interface_name_;
597597
handle_name_ = other.handle_name_;
598598
value_ = other.value_;
599+
data_type_ = other.data_type_;
599600
if (std::holds_alternative<std::monostate>(value_))
600601
{
601602
value_ptr_ = other.value_ptr_;
@@ -613,10 +614,12 @@ class Handle
613614
std::swap(first.interface_name_, second.interface_name_);
614615
std::swap(first.handle_name_, second.handle_name_);
615616
std::swap(first.value_, second.value_);
617+
std::swap(first.data_type_, second.data_type_);
616618
std::swap(first.value_ptr_, second.value_ptr_);
617619
}
618620

619621
protected:
622+
/// @note The methods copy and swap need to be updated, if new members are added.
620623
std::string prefix_name_;
621624
std::string interface_name_;
622625
std::string handle_name_;

hardware_interface/test/test_handle.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,39 @@ TEST(TestHandle, interface_description_bool_data_type)
245245
EXPECT_NO_THROW({ std::ignore = handle.get_optional<double>(); });
246246
}
247247

248+
TEST(TestHandle, interface_description_bool_data_type_copy)
249+
{
250+
const std::string collision_interface = "collision";
251+
const std::string itf_name = "joint1";
252+
InterfaceInfo info;
253+
info.name = collision_interface;
254+
info.data_type = "bool";
255+
InterfaceDescription interface_descr(itf_name, info);
256+
StateInterface handle{interface_descr};
257+
258+
auto handle_copy = handle; // Copy constructor
259+
260+
ASSERT_EQ(hardware_interface::HandleDataType::BOOL, interface_descr.get_data_type());
261+
ASSERT_EQ(hardware_interface::HandleDataType::BOOL, handle_copy.get_data_type());
262+
EXPECT_EQ(handle_copy.get_name(), itf_name + "/" + collision_interface);
263+
EXPECT_EQ(handle_copy.get_interface_name(), collision_interface);
264+
EXPECT_EQ(handle_copy.get_prefix_name(), itf_name);
265+
EXPECT_NO_THROW({ std::ignore = handle_copy.get_optional<bool>(); });
266+
ASSERT_FALSE(handle_copy.get_optional<bool>().value()) << "Default value should be false";
267+
ASSERT_TRUE(handle_copy.set_value(true));
268+
ASSERT_TRUE(handle_copy.get_optional<bool>().value());
269+
ASSERT_EQ(handle_copy.get_optional(), 1.0);
270+
ASSERT_TRUE(handle_copy.set_value(false));
271+
ASSERT_FALSE(handle_copy.get_optional<bool>().value());
272+
ASSERT_EQ(handle_copy.get_optional(), 0.0);
273+
274+
// Test the assertions
275+
ASSERT_THROW({ std::ignore = handle.set_value(-1.0); }, std::runtime_error);
276+
ASSERT_THROW({ std::ignore = handle.set_value(0.0); }, std::runtime_error);
277+
278+
EXPECT_NO_THROW({ std::ignore = handle.get_optional<double>(); });
279+
}
280+
248281
TEST(TestHandle, handle_constructor_double_data_type)
249282
{
250283
const std::string POSITION_INTERFACE = "position";

0 commit comments

Comments
 (0)