Simplified code examples

- renamed variables to be less confusing
- removed wrapper for properties
- added example usage of sdbusplus::unpackPropertiesNoThrow

Tested:
- Console output from both examples is the same
- Both examples still return error code 0 after Ctl+C

Signed-off-by: Krzysztof Grobelny <krzysztof.grobelny@intel.com>
Change-Id: I1e1a7922e64551510960ccaf02c58c9e7a2e4257
diff --git a/example/get-all-properties.cpp b/example/get-all-properties.cpp
index 1b3c508..eaa2116 100644
--- a/example/get-all-properties.cpp
+++ b/example/get-all-properties.cpp
@@ -7,26 +7,12 @@
 
 #include <iostream>
 
-namespace xyz
-{
-namespace demo
-{
-
-const std::string path = "/xyz/demo";
-const std::string name = "xyz.demo";
-const std::string interface = "xyz.demo.interface";
-
-} // namespace demo
-} // namespace xyz
-
-namespace name
-{
-
-const std::string greetings = "Greetings";
-const std::string goodbyes = "Goodbyes";
-const std::string value = "Value";
-
-} // namespace name
+const std::string demoServiceName = "demo.service";
+const std::string demoObjectPath = "/xyz/demo";
+const std::string demoInterfaceName = "xyz.demo";
+const std::string propertyGrettingName = "Greetings";
+const std::string propertyGoodbyesName = "Goodbyes";
+const std::string propertyValueName = "Value";
 
 class Application
 {
@@ -36,15 +22,15 @@
         ioc_(ioc),
         bus_(bus), objServer_(objServer)
     {
-        demo_ = objServer_.add_unique_interface(xyz::demo::path,
-                                                xyz::demo::interface);
+        demo_ =
+            objServer_.add_unique_interface(demoObjectPath, demoInterfaceName);
 
-        demo_->register_property_r(name::greetings, std::string(),
+        demo_->register_property_r(propertyGrettingName, std::string(),
                                    sdbusplus::vtable::property_::const_,
                                    [this](const auto&) { return greetings_; });
 
         demo_->register_property_rw(
-            name::goodbyes, std::string(),
+            propertyGoodbyesName, std::string(),
             sdbusplus::vtable::property_::emits_change,
             [this](const auto& newPropertyValue, const auto&) {
                 goodbyes_ = newPropertyValue;
@@ -53,7 +39,8 @@
             [this](const auto&) { return goodbyes_; });
 
         demo_->register_property_r(
-            name::value, uint32_t{42}, sdbusplus::vtable::property_::const_,
+            propertyValueName, uint32_t{42},
+            sdbusplus::vtable::property_::const_,
             [](const auto& value) -> uint32_t { return value; });
 
         demo_->initialize();
@@ -76,6 +63,12 @@
         ++fatalErrors_;
     }
 
+    void logBadProperty(const std::string& badProperty)
+    {
+        std::cerr << "BadProperty: " << badProperty << "\n";
+        ++fatalErrors_;
+    }
+
     void logExpectedException(
         const sdbusplus::exception::UnpackPropertyError& error)
     {
@@ -87,7 +80,7 @@
     void asyncGetAllPropertiesStringTypeOnly()
     {
         sdbusplus::asio::getAllProperties(
-            bus_, xyz::demo::name, xyz::demo::path, xyz::demo::interface,
+            bus_, demoServiceName, demoObjectPath, demoInterfaceName,
             [this](boost::system::error_code ec,
                    std::vector<std::pair<
                        std::string, std::variant<std::monostate, std::string>>>&
@@ -97,26 +90,31 @@
                     logSystemErrorCode(ec);
                     return;
                 }
-                try
                 {
                     std::string greetings;
                     std::string goodbyes;
-                    sdbusplus::unpackProperties(properties, name::greetings,
-                                                greetings, name::goodbyes,
-                                                goodbyes);
+                    std::optional<std::string> badProperty =
+                        sdbusplus::unpackPropertiesNoThrow(
+                            properties, propertyGrettingName, greetings,
+                            propertyGoodbyesName, goodbyes);
 
-                    std::cout << "value of greetings: " << greetings << "\n";
-                    std::cout << "value of goodbyes: " << goodbyes << "\n";
-                }
-                catch (const sdbusplus::exception::UnpackPropertyError& error)
-                {
-                    logException(error);
+                    if (badProperty)
+                    {
+                        logBadProperty(*badProperty);
+                    }
+                    else
+                    {
+                        std::cout << "value of greetings: " << greetings
+                                  << "\n";
+                        std::cout << "value of goodbyes: " << goodbyes << "\n";
+                    }
                 }
 
                 try
                 {
                     std::string value;
-                    sdbusplus::unpackProperties(properties, name::value, value);
+                    sdbusplus::unpackProperties(properties, propertyValueName,
+                                                value);
 
                     std::cerr << "Error: it should fail because of "
                                  "not matched type\n";
@@ -132,7 +130,7 @@
     void asyncGetAllProperties()
     {
         sdbusplus::asio::getAllProperties(
-            bus_, xyz::demo::name, xyz::demo::path, xyz::demo::interface,
+            bus_, demoServiceName, demoObjectPath, demoInterfaceName,
             [this](boost::system::error_code ec,
                    std::vector<std::pair<
                        std::string,
@@ -148,9 +146,10 @@
                     std::string greetings;
                     std::string goodbyes;
                     uint32_t value = 0u;
-                    sdbusplus::unpackProperties(properties, name::greetings,
-                                                greetings, name::goodbyes,
-                                                goodbyes, name::value, value);
+                    sdbusplus::unpackProperties(properties,
+                                                propertyGrettingName, greetings,
+                                                propertyGoodbyesName, goodbyes,
+                                                propertyValueName, value);
 
                     std::cout << "value of greetings: " << greetings << "\n";
                     std::cout << "value of goodbyes: " << goodbyes << "\n";
@@ -179,8 +178,8 @@
                 try
                 {
                     uint32_t notMatchingType;
-                    sdbusplus::unpackProperties(properties, name::greetings,
-                                                notMatchingType);
+                    sdbusplus::unpackProperties(
+                        properties, propertyGrettingName, notMatchingType);
 
                     std::cerr << "Error: it should fail because of "
                                  "not matched type\n";
@@ -216,7 +215,7 @@
     auto bus = std::make_shared<sdbusplus::asio::connection>(ioc);
     auto objServer = std::make_unique<sdbusplus::asio::object_server>(bus);
 
-    bus->request_name(xyz::demo::name.c_str());
+    bus->request_name(demoServiceName.c_str());
 
     Application app(ioc, *bus, *objServer);
 
diff --git a/example/register-property.cpp b/example/register-property.cpp
index a95181c..6537aca 100644
--- a/example/register-property.cpp
+++ b/example/register-property.cpp
@@ -6,62 +6,11 @@
 
 #include <iostream>
 
-namespace xyz
-{
-namespace demo
-{
-
-const std::string path = "/xyz/demo";
-const std::string name = "xyz.demo";
-
-} // namespace demo
-} // namespace xyz
-
-namespace name
-{
-
-const std::string greetings = "Greetings";
-const std::string goodbyes = "Goodbyes";
-
-} // namespace name
-
-namespace utils
-{
-
-template <class T>
-class Property
-{
-  public:
-    Property(sdbusplus::asio::connection& bus, std::string_view service,
-             std::string_view path, std::string_view interface,
-             std::string_view name) :
-        bus_(bus),
-        service_(service), path_(path), interface_(interface), name_(name)
-    {}
-
-    template <class Handler>
-    void async_get(Handler&& handler)
-    {
-        sdbusplus::asio::getProperty<T>(bus_, service_, path_, interface_,
-                                        name_, std::forward<Handler>(handler));
-    }
-
-    template <class Handler>
-    void async_set(const T& value, Handler&& handler)
-    {
-        sdbusplus::asio::setProperty(bus_, service_, path_, interface_, name_,
-                                     value, std::forward<Handler>(handler));
-    }
-
-  private:
-    sdbusplus::asio::connection& bus_;
-    std::string service_;
-    std::string path_;
-    std::string interface_;
-    std::string name_;
-};
-
-} // namespace utils
+const std::string demoServiceName = "demo.service";
+const std::string demoObjectPath = "/xyz/demo";
+const std::string demoInterfaceName = "xyz.demo";
+const std::string propertyGrettingName = "Greetings";
+const std::string propertyGoodbyesName = "Goodbyes";
 
 class Application
 {
@@ -72,15 +21,15 @@
         bus_(bus), objServer_(objServer)
     {
         demo_ = objServer_.add_unique_interface(
-            xyz::demo::path, xyz::demo::name,
+            demoObjectPath, demoInterfaceName,
             [this](sdbusplus::asio::dbus_interface& demo) {
                 demo.register_property_r(
-                    name::greetings, std::string(),
+                    propertyGrettingName, std::string(),
                     sdbusplus::vtable::property_::const_,
                     [this](const auto&) { return greetings_; });
 
                 demo.register_property_rw(
-                    name::goodbyes, std::string(),
+                    propertyGoodbyesName, std::string(),
                     sdbusplus::vtable::property_::emits_change,
                     [this](const auto& newPropertyValue, const auto&) {
                         goodbyes_ = newPropertyValue;
@@ -105,17 +54,15 @@
 
     void asyncReadPropertyWithIncorrectType()
     {
-        utils::Property<uint32_t> propertyWithWrongType{
-            bus_, xyz::demo::name, xyz::demo::path, xyz::demo::name,
-            name::greetings};
-
-        propertyWithWrongType.async_get(
-            [this](boost::system::error_code error, uint32_t) {
-                if (error)
+        sdbusplus::asio::getProperty<uint32_t>(
+            bus_, demoServiceName, demoObjectPath, demoInterfaceName,
+            propertyGrettingName,
+            [this](boost::system::error_code ec, uint32_t) {
+                if (ec)
                 {
                     std::cout
                         << "As expected failed to getProperty with wrong type: "
-                        << error << "\n";
+                        << ec << "\n";
                     return;
                 }
 
@@ -127,7 +74,9 @@
 
     void asyncReadProperties()
     {
-        propertyGreetings.async_get(
+        sdbusplus::asio::getProperty<std::string>(
+            bus_, demoServiceName, demoObjectPath, demoInterfaceName,
+            propertyGrettingName,
             [this](boost::system::error_code ec, std::string value) {
                 if (ec)
                 {
@@ -137,7 +86,9 @@
                 std::cout << "Greetings value is: " << value << "\n";
             });
 
-        propertyGoodbyes.async_get(
+        sdbusplus::asio::getProperty<std::string>(
+            bus_, demoServiceName, demoObjectPath, demoInterfaceName,
+            propertyGoodbyesName,
             [this](boost::system::error_code ec, std::string value) {
                 if (ec)
                 {
@@ -150,13 +101,15 @@
 
     void asyncChangeProperty()
     {
-        propertyGreetings.async_set(
-            "Hi, hey, hello", [this](const boost::system::error_code& error) {
-                if (error)
+        sdbusplus::asio::setProperty(
+            bus_, demoServiceName, demoObjectPath, demoInterfaceName,
+            propertyGrettingName, "Hi, hey, hello",
+            [this](const boost::system::error_code& ec) {
+                if (ec)
                 {
                     std::cout
                         << "As expected, failed to set greetings property: "
-                        << error << "\n";
+                        << ec << "\n";
                     return;
                 }
 
@@ -165,14 +118,16 @@
                 ++fatalErrors_;
             });
 
-        propertyGoodbyes.async_set(
-            "Bye bye", [this](const boost::system::error_code& error) {
-                if (error)
+        sdbusplus::asio::setProperty(
+            bus_, demoServiceName, demoObjectPath, demoInterfaceName,
+            propertyGoodbyesName, "Bye bye",
+            [this](const boost::system::error_code& ec) {
+                if (ec)
                 {
                     std::cout
                         << "Error: it supposed to be ok to change goodbyes "
                            "property: "
-                        << error << "\n";
+                        << ec << "\n";
                     ++fatalErrors_;
                     return;
                 }
@@ -184,7 +139,7 @@
     void syncChangeGoodbyes(std::string_view value)
     {
         goodbyes_ = value;
-        demo_->signal_property(name::goodbyes);
+        demo_->signal_property(propertyGoodbyesName);
     }
 
   private:
@@ -197,13 +152,6 @@
     std::string goodbyes_ = "Bye";
 
     uint32_t fatalErrors_ = 0u;
-
-    utils::Property<std::string> propertyGreetings{
-        bus_, xyz::demo::name, xyz::demo::path, xyz::demo::name,
-        name::greetings};
-    utils::Property<std::string> propertyGoodbyes{
-        bus_, xyz::demo::name, xyz::demo::path, xyz::demo::name,
-        name::goodbyes};
 };
 
 int main(int, char**)
@@ -217,7 +165,7 @@
     auto bus = std::make_shared<sdbusplus::asio::connection>(ioc);
     auto objServer = std::make_unique<sdbusplus::asio::object_server>(bus);
 
-    bus->request_name(xyz::demo::name.c_str());
+    bus->request_name(demoServiceName.c_str());
 
     Application app(ioc, *bus, *objServer);