Pelagicore / gdbus-codegen-glibmm

Code generator for C++ D-Bus stubs and proxies using Giomm/Glibmm
GNU Lesser General Public License v2.1
23 stars 25 forks source link

Generate sync method/property set calls in proxy #65

Closed martin-ejdestig closed 5 years ago

martin-ejdestig commented 5 years ago

Would simplify certain use cases (e.g. interactive command line tools) where D-Bus methods are called or properties are set but it is not important to be responsive while calls are pending.

If we do not want to generate sync versions all the time a command line option could be added to optionally turn it on.

martin-ejdestig commented 5 years ago

E.g. compare:

void CommandWiFiHotspot::enable()
{
        auto set_enable = [&] {
            manager_->WiFiHotspotEnabled_set(true, [&](const auto &result) {
                try {
                    manager_->WiFiHotspotEnabled_set_finish(result);
                } catch (const Glib::Error &e) {
                    std::cout << "Failed to enable Wi-Fi hotspot: " << e.what() << '\n';
                }
                main_loop_->quit();
            });
        };

        auto property_set_finished = [&] {
            pending_property_sets--;

            if (pending_property_sets == 0)
                set_enable();
        };

        if (!arguments_.name.empty()) {
            pending_property_sets_++;
            manager_->WiFiHotspotSSID_set(arguments_.name, [&](const auto &result) {
                try {
                    manager_->WiFiHotspotSSID_set_finish(result);
                } catch (const Glib::Error &e) {
                    std::cout << "Failed to set Wi-Fi hotspot name: " << e.what() << '\n';
                    main_loop_->quit();
                    return;
                }
                property_set_finished();
            });
        }

        if (!arguments_.passphrase.empty()) {
            pending_property_sets_++;
            manager_->WiFiHotspotPassphrase_set(arguments_.name, [&](const auto &result) {
                try {
                    manager_->WiFiHotspotPassphrase_set_finish(result);
                } catch (const Glib::Error &e) {
                    std::cout << "Failed to set Wi-Fi hotspot name: " << e.what() << '\n';
                    main_loop_->quit();
                    return;
                }
                property_set_finished();
            });
        }

        enable_if_no_pending_property_set();
}

With:

void CommandWiFiHotspot::enable()
{
    try {
        if (!arguments_.name.empty())
            manager_->WiFiHotspotSSID_set_sync(arguments_.name);

        if (!arguments_.passphrase.empty())
            manager_->WiFiHotspotPassphrase_set_sync(arguments_.passphrase);

        manager_->WiFiHotspotEnabled_set_sync(true);
    } catch (const Glib::Error &e) {
        std::cout << "Failed to enable Wi-Fi hotspot: " << e.what() << '\n';
    }
}

Do not have to have accounting for outstanding "property set" calls that must succeed before final property is set, only single try/catch and whole CLI program can be modified to not have any main loop at all.

martin-ejdestig commented 5 years ago

68 was merged, closing.