fix: make Variant conversion operator explicit (#428)

Having explicit conversion operator is a good practice according to the C++ core guidelines, as it makes the code safer and better follows the principle of least astonishment. Also, it is consistent with the standard library style, where wrappers like std::variant, std::any, std::optional... also do not provide an implicit conversion to the underlying type. Last but not least, it paves the way for the upcoming std::variant <-> sdbus::Variant implicit conversions without surprising behavior in some edge cases.
This commit is contained in:
Stanislav Angelovič
2024-04-24 20:20:29 +02:00
parent 42f0bd07c0
commit 32a97f214f
5 changed files with 9 additions and 8 deletions
+3 -3
View File
@@ -107,9 +107,9 @@ TYPED_TEST(SdbusTestObject, CallsMethodWithStructVariantsAndGetMapSuccesfully)
std::vector<int32_t> x{-2, 0, 2};
sdbus::Struct<sdbus::Variant, sdbus::Variant> y{false, true};
std::map<int32_t, sdbus::Variant> mapOfVariants = this->m_proxy->getMapOfVariants(x, y);
decltype(mapOfVariants) res{ {sdbus::Variant{-2}, sdbus::Variant{false}}
, {sdbus::Variant{0}, sdbus::Variant{false}}
, {sdbus::Variant{2}, sdbus::Variant{true}}};
decltype(mapOfVariants) res{ {-2, sdbus::Variant{false}}
, {0, sdbus::Variant{false}}
, {2, sdbus::Variant{true}}};
ASSERT_THAT(mapOfVariants[-2].get<bool>(), Eq(res[-2].get<bool>()));
ASSERT_THAT(mapOfVariants[0].get<bool>(), Eq(res[0].get<bool>()));
@@ -198,7 +198,7 @@ public:
public:
uint32_t action()
{
return proxy_->getProperty("action").onInterface(INTERFACE_NAME);
return proxy_->getProperty("action").onInterface(INTERFACE_NAME).get<uint32_t>();
}
void action(const uint32_t& value)
@@ -208,7 +208,7 @@ public:
bool blocking()
{
return proxy_->getProperty("blocking").onInterface(INTERFACE_NAME);
return proxy_->getProperty("blocking").onInterface(INTERFACE_NAME).get<bool>();
}
void blocking(const bool& value)
@@ -218,7 +218,7 @@ public:
std::string state()
{
return proxy_->getProperty("state").onInterface(INTERFACE_NAME);
return proxy_->getProperty("state").onInterface(INTERFACE_NAME).get<std::string>();
}
private: