Allow appending std::vector<bool>
std::vector<bool> when done with a for loop returns the type of
std::_bit_reference, which doesn't have a direct conversion to the
underlying boolean type.
Rather than relying on the type returned by begin(), rely on
T::value_type to specifically get the type in the container.
There isn't a particular use for this, but
https://gerrit.openbmc.org/c/openbmc/entity-manager/+/81474
Is trying to consolidate code, and having all types use the same
pack/unpack code is advantageous to avoid special cases. I don't
know of a place we're actually packing/unpacking arrays of bool.
Tested: Booted gb200nvl-bmc. Services launched, no new crashes seen.
Change-Id: I07fc150a190ae44f7bdc90531ad2a4ce7f47e0a1
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/include/sdbusplus/message/append.hpp b/include/sdbusplus/message/append.hpp
index 31dad9f..38d79ab 100644
--- a/include/sdbusplus/message/append.hpp
+++ b/include/sdbusplus/message/append.hpp
@@ -225,11 +225,12 @@
template <typename S>
static void op(sdbusplus::SdBusInterface* intf, sd_bus_message* m, S&& s)
{
- constexpr auto dbusType = utility::tuple_to_array(types::type_id<T>());
+ constexpr auto dbusType =
+ utility::tuple_to_array(types::type_id<typename T::value_type>());
intf->sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY,
- dbusType.data() + 1);
- for (auto&& i : s)
+ dbusType.data());
+ for (const typename T::value_type& i : s)
{
sdbusplus::message::append(intf, m, i);
}
diff --git a/test/message/append.cpp b/test/message/append.cpp
index bbba074..936454e 100644
--- a/test/message/append.cpp
+++ b/test/message/append.cpp
@@ -294,6 +294,21 @@
new_message().append(v);
}
+TEST_F(AppendTest, VectorBoolean)
+{
+ const std::vector<bool> v{false, true, false, true};
+ {
+ testing::InSequence seq;
+ expect_open_container(SD_BUS_TYPE_ARRAY, "b");
+ for (const auto& i : v)
+ {
+ expect_basic<bool>(SD_BUS_TYPE_BOOLEAN, (int)i);
+ }
+ expect_close_container();
+ }
+ new_message().append(v);
+}
+
TEST_F(AppendTest, VectorNestIntegral)
{
const std::vector<std::array<int32_t, 3>> v{