From 3e84b254e9603935cb5fc180c4d2214d7024ccbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanislav=20Angelovi=C4=8D?= Date: Wed, 9 Aug 2023 12:39:16 +0200 Subject: [PATCH] refactor: improve type extensibility and its safety (#348) --- include/sdbus-c++/Message.h | 7 +++---- include/sdbus-c++/Types.h | 2 +- src/Message.cpp | 8 ++++++-- tests/integrationtests/DBusMethodsTests.cpp | 4 +++- tests/integrationtests/DBusSignalsTests.cpp | 2 +- tests/integrationtests/DBusStandardInterfacesTests.cpp | 2 +- tests/integrationtests/TestAdaptor.cpp | 2 +- tests/stresstests/sdbus-c++-stress-tests.cpp | 4 ++-- 8 files changed, 18 insertions(+), 13 deletions(-) diff --git a/include/sdbus-c++/Message.h b/include/sdbus-c++/Message.h index 91f613c..915a071 100644 --- a/include/sdbus-c++/Message.h +++ b/include/sdbus-c++/Message.h @@ -158,6 +158,9 @@ namespace sdbus { Message& enterStruct(const std::string& signature); Message& exitStruct(); + Message& appendArray(char type, const void *ptr, size_t size); + Message& readArray(char type, const void **ptr, size_t *size); + explicit operator bool() const; void clearFlags(); @@ -199,9 +202,6 @@ namespace sdbus { template void deserializeArraySlow(std::vector<_Element, _Allocator>& items); - void appendArray(char type, const void *ptr, size_t size); - void readArray(char type, const void **ptr, size_t *size); - template void serializeDictionary(const _Dictionary& items); template @@ -529,7 +529,6 @@ namespace sdbus { while (true) { _Element elem; - // TODO: Is there a way to find D-Bus message container size upfront? We could reserve space in vector upfront. if (*this >> elem) items.emplace_back(std::move(elem)); else diff --git a/include/sdbus-c++/Types.h b/include/sdbus-c++/Types.h index 9633f73..22fe8be 100644 --- a/include/sdbus-c++/Types.h +++ b/include/sdbus-c++/Types.h @@ -56,7 +56,7 @@ namespace sdbus { Variant(); template - Variant(const _ValueType& value) + explicit Variant(const _ValueType& value) : Variant() { msg_.openVariant(signature_of<_ValueType>::str()); diff --git a/src/Message.cpp b/src/Message.cpp index 3373fe0..b68801d 100644 --- a/src/Message.cpp +++ b/src/Message.cpp @@ -226,10 +226,12 @@ Message& Message::operator<<(const UnixFd &item) return *this; } -void Message::appendArray(char type, const void *ptr, size_t size) +Message& Message::appendArray(char type, const void *ptr, size_t size) { auto r = sd_bus_message_append_array((sd_bus_message*)msg_, type, ptr, size); SDBUS_THROW_ERROR_IF(r < 0, "Failed to serialize an array", -r); + + return *this; } Message& Message::operator>>(bool& item) @@ -323,13 +325,15 @@ Message& Message::operator>>(uint64_t& item) return *this; } -void Message::readArray(char type, const void **ptr, size_t *size) +Message& Message::readArray(char type, const void **ptr, size_t *size) { auto r = sd_bus_message_read_array((sd_bus_message*)msg_, type, ptr, size); if (r == 0) ok_ = false; SDBUS_THROW_ERROR_IF(r < 0, "Failed to deserialize an array", -r); + + return *this; } Message& Message::operator>>(double& item) diff --git a/tests/integrationtests/DBusMethodsTests.cpp b/tests/integrationtests/DBusMethodsTests.cpp index 93ca28e..2d777b8 100644 --- a/tests/integrationtests/DBusMethodsTests.cpp +++ b/tests/integrationtests/DBusMethodsTests.cpp @@ -103,7 +103,9 @@ TEST_F(SdbusTestObject, CallsMethodWithStructVariantsAndGetMapSuccesfully) std::vector x{-2, 0, 2}; sdbus::Struct y{false, true}; auto mapOfVariants = m_proxy->getMapOfVariants(x, y); - decltype(mapOfVariants) res{{-2, false}, {0, false}, {2, true}}; + decltype(mapOfVariants) res{ {sdbus::Variant{-2}, sdbus::Variant{false}} + , {sdbus::Variant{0}, sdbus::Variant{false}} + , {sdbus::Variant{2}, sdbus::Variant{true}}}; ASSERT_THAT(mapOfVariants[-2].get(), Eq(res[-2].get())); ASSERT_THAT(mapOfVariants[0].get(), Eq(res[0].get())); diff --git a/tests/integrationtests/DBusSignalsTests.cpp b/tests/integrationtests/DBusSignalsTests.cpp index 5ac40d8..6f907cd 100644 --- a/tests/integrationtests/DBusSignalsTests.cpp +++ b/tests/integrationtests/DBusSignalsTests.cpp @@ -104,7 +104,7 @@ TEST_F(SdbusTestObject, EmitsSignalWithLargeMapSuccesfully) TEST_F(SdbusTestObject, EmitsSignalWithVariantSuccesfully) { double d = 3.14; - m_adaptor->emitSignalWithVariant(d); + m_adaptor->emitSignalWithVariant(sdbus::Variant{d}); ASSERT_TRUE(waitUntil(m_proxy->m_gotSignalWithVariant)); ASSERT_THAT(m_proxy->m_variantFromSignal, DoubleEq(d)); diff --git a/tests/integrationtests/DBusStandardInterfacesTests.cpp b/tests/integrationtests/DBusStandardInterfacesTests.cpp index 6381587..3ccfa2a 100644 --- a/tests/integrationtests/DBusStandardInterfacesTests.cpp +++ b/tests/integrationtests/DBusStandardInterfacesTests.cpp @@ -86,7 +86,7 @@ TEST_F(SdbusTestObject, SetsPropertyViaPropertiesInterface) { uint32_t newActionValue = 2345; - m_proxy->Set(INTERFACE_NAME, "action", newActionValue); + m_proxy->Set(INTERFACE_NAME, "action", sdbus::Variant{newActionValue}); ASSERT_THAT(m_proxy->action(), Eq(newActionValue)); } diff --git a/tests/integrationtests/TestAdaptor.cpp b/tests/integrationtests/TestAdaptor.cpp index 3b3355b..6a521e1 100644 --- a/tests/integrationtests/TestAdaptor.cpp +++ b/tests/integrationtests/TestAdaptor.cpp @@ -77,7 +77,7 @@ std::vector TestAdaptor::getInts16FromStruct(const sdbus::Struct(v.get()); + sdbus::Variant res{static_cast(v.get())}; return res; } diff --git a/tests/stresstests/sdbus-c++-stress-tests.cpp b/tests/stresstests/sdbus-c++-stress-tests.cpp index 19384fe..2a4b1d3 100644 --- a/tests/stresstests/sdbus-c++-stress-tests.cpp +++ b/tests/stresstests/sdbus-c++-stress-tests.cpp @@ -427,8 +427,8 @@ int main(int argc, char *argv[]) while (!stopClients) { std::map param; - param["key1"] = "sdbus-c++-stress-tests"; - param["key2"] = ++localCounter; + param["key1"] = sdbus::Variant{"sdbus-c++-stress-tests"}; + param["key2"] = sdbus::Variant{++localCounter}; concatenator.concatenate(param);