diff options
author | Alex Vakulenko <avakulenko@chromium.org> | 2015-02-23 15:55:21 -0800 |
---|---|---|
committer | ChromeOS Commit Bot <chromeos-commit-bot@chromium.org> | 2015-02-25 23:33:19 +0000 |
commit | c4e01f1a41b5fce64a352f9a39f956c2586887af (patch) | |
tree | 43815d96509bd13c12bcf550a9ae89bbca385666 | |
parent | 59dd2e90057686c1fc7978456e9b24b9688146c4 (diff) | |
download | dbus-binding-generator-c4e01f1a41b5fce64a352f9a39f956c2586887af.tar.gz |
chromeos-dbus-bindings: Generate only single proxy accessor for singletons
For D-Bus objects with single fixed object path assume that they are
singletons and remove the object path parameter from the proxy
accessor ObjectManagerProxy::GetNNNProxy() method.
BUG=brillo:348
TEST=`FEATURES=test emerge-link chromeos-dbus-bindings`
`FEATURES=test emerge-link peerd privetd buffet apmanager lorgnette`
Change-Id: I29419e2db5bd0fc8a4f508804c1e322c1880a3dd
Reviewed-on: https://chromium-review.googlesource.com/252422
Trybot-Ready: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
-rw-r--r-- | chromeos-dbus-bindings/proxy_generator.cc | 48 | ||||
-rw-r--r-- | chromeos-dbus-bindings/proxy_generator_unittest.cc | 20 |
2 files changed, 40 insertions, 28 deletions
diff --git a/chromeos-dbus-bindings/proxy_generator.cc b/chromeos-dbus-bindings/proxy_generator.cc index bdf21d5..34d9e90 100644 --- a/chromeos-dbus-bindings/proxy_generator.cc +++ b/chromeos-dbus-bindings/proxy_generator.cc @@ -924,22 +924,38 @@ void ProxyGenerator::ObjectManager::AddInterfaceAccessors( string map_name = itf_name.MakeVariableName() + "_instances_"; // GetProxy(). - text->AddLine(StringPrintf("%s* Get%s(", - itf_name.MakeProxyName(true).c_str(), - itf_name.MakeProxyName(false).c_str())); - text->PushOffset(kLineContinuationOffset); - text->AddLine("const dbus::ObjectPath& object_path) {"); - text->PopOffset(); - text->PushOffset(kBlockOffset); - text->AddLine(StringPrintf("auto p = %s.find(object_path);", - map_name.c_str())); - text->AddLine(StringPrintf("if (p != %s.end())", map_name.c_str())); - text->PushOffset(kBlockOffset); - text->AddLine("return p->second.get();"); - text->PopOffset(); - text->AddLine("return nullptr;"); - text->PopOffset(); - text->AddLine("}"); + if (interface.path.empty()) { + // We have no fixed path, so there could be multiple instances of this itf. + text->AddLine(StringPrintf("%s* Get%s(", + itf_name.MakeProxyName(true).c_str(), + itf_name.MakeProxyName(false).c_str())); + text->PushOffset(kLineContinuationOffset); + text->AddLine("const dbus::ObjectPath& object_path) {"); + text->PopOffset(); + text->PushOffset(kBlockOffset); + text->AddLine(StringPrintf("auto p = %s.find(object_path);", + map_name.c_str())); + text->AddLine(StringPrintf("if (p != %s.end())", map_name.c_str())); + text->PushOffset(kBlockOffset); + text->AddLine("return p->second.get();"); + text->PopOffset(); + text->AddLine("return nullptr;"); + text->PopOffset(); + text->AddLine("}"); + } else { + // We have a fixed path, so the object could be considered a "singleton". + // Skip the object_path parameter and return the first available instance. + text->AddLine(StringPrintf("%s* Get%s() {", + itf_name.MakeProxyName(true).c_str(), + itf_name.MakeProxyName(false).c_str())); + text->PushOffset(kBlockOffset); + text->AddLine(StringPrintf("if (%s.empty())", map_name.c_str())); + text->AddLineWithOffset("return nullptr;", kBlockOffset); + text->AddLine(StringPrintf("return %s.begin()->second.get();", + map_name.c_str())); + text->PopOffset(); + text->AddLine("}"); + } // GetInstances(). text->AddLine(StringPrintf("std::vector<%s*> Get%sInstances() const {", diff --git a/chromeos-dbus-bindings/proxy_generator_unittest.cc b/chromeos-dbus-bindings/proxy_generator_unittest.cc index f0615a9..a157656 100644 --- a/chromeos-dbus-bindings/proxy_generator_unittest.cc +++ b/chromeos-dbus-bindings/proxy_generator_unittest.cc @@ -756,12 +756,10 @@ class ObjectManagerProxy : public dbus::ObjectManager::Interface { return dbus_object_manager_; } - org::chromium::Itf1Proxy* GetItf1Proxy( - const dbus::ObjectPath& object_path) { - auto p = itf1_instances_.find(object_path); - if (p != itf1_instances_.end()) - return p->second.get(); - return nullptr; + org::chromium::Itf1Proxy* GetItf1Proxy() { + if (itf1_instances_.empty()) + return nullptr; + return itf1_instances_.begin()->second.get(); } std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const { std::vector<org::chromium::Itf1Proxy*> values; @@ -1103,12 +1101,10 @@ class ObjectManagerProxy : public dbus::ObjectManager::Interface { return dbus_object_manager_; } - org::chromium::Itf1Proxy* GetItf1Proxy( - const dbus::ObjectPath& object_path) { - auto p = itf1_instances_.find(object_path); - if (p != itf1_instances_.end()) - return p->second.get(); - return nullptr; + org::chromium::Itf1Proxy* GetItf1Proxy() { + if (itf1_instances_.empty()) + return nullptr; + return itf1_instances_.begin()->second.get(); } std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const { std::vector<org::chromium::Itf1Proxy*> values; |