diff options
author | 2018-06-25 16:32:01 -0700 | |
---|---|---|
committer | 2018-06-28 13:23:15 -0700 | |
commit | db730530656f717cdb925c058461a4485db4b32b (patch) | |
tree | 5da28453fede9d25cd65b12cf52b643d41905736 | |
parent | 20f4ee8c46a35f6ec4bf6c634bee866a2a92be0a (diff) |
lshal: add ListCommand::tableForType
... to avoid duplicating switch/case logic.
Test: lshal_test
Change-Id: I9096534d607839ccc34dc115e76e890688a25c61
-rw-r--r-- | cmds/lshal/ListCommand.cpp | 54 | ||||
-rw-r--r-- | cmds/lshal/ListCommand.h | 2 |
2 files changed, 22 insertions, 34 deletions
diff --git a/cmds/lshal/ListCommand.cpp b/cmds/lshal/ListCommand.cpp index fae96d3176..c68d1972de 100644 --- a/cmds/lshal/ListCommand.cpp +++ b/cmds/lshal/ListCommand.cpp @@ -287,32 +287,31 @@ bool ListCommand::shouldReportHalType(const HalType &type) const { return (std::find(mListTypes.begin(), mListTypes.end(), type) != mListTypes.end()); } +Table* ListCommand::tableForType(HalType type) { + switch (type) { + case HalType::BINDERIZED_SERVICES: + return &mServicesTable; + case HalType::PASSTHROUGH_CLIENTS: + return &mPassthroughRefTable; + case HalType::PASSTHROUGH_LIBRARIES: + return &mImplementationsTable; + default: + LOG(FATAL) << "Unknown HAL type " << static_cast<int64_t>(type); + return nullptr; + } +} +const Table* ListCommand::tableForType(HalType type) const { + return const_cast<ListCommand*>(this)->tableForType(type); +} + void ListCommand::forEachTable(const std::function<void(Table &)> &f) { for (const auto& type : mListTypes) { - switch (type) { - case HalType::BINDERIZED_SERVICES: - f(mServicesTable); break; - case HalType::PASSTHROUGH_CLIENTS: - f(mPassthroughRefTable); break; - case HalType::PASSTHROUGH_LIBRARIES: - f(mImplementationsTable); break; - default: - LOG(FATAL) << __func__ << "Unknown HAL type."; - } + f(*tableForType(type)); } } void ListCommand::forEachTable(const std::function<void(const Table &)> &f) const { for (const auto& type : mListTypes) { - switch (type) { - case HalType::BINDERIZED_SERVICES: - f(mServicesTable); break; - case HalType::PASSTHROUGH_CLIENTS: - f(mPassthroughRefTable); break; - case HalType::PASSTHROUGH_LIBRARIES: - f(mImplementationsTable); break; - default: - LOG(FATAL) << __func__ << "Unknown HAL type."; - } + f(*tableForType(type)); } } @@ -553,20 +552,7 @@ Status ListCommand::dump() { } void ListCommand::putEntry(HalType type, TableEntry &&entry) { - Table *table = nullptr; - switch (type) { - case HalType::BINDERIZED_SERVICES : - table = &mServicesTable; break; - case HalType::PASSTHROUGH_CLIENTS : - table = &mPassthroughRefTable; break; - case HalType::PASSTHROUGH_LIBRARIES : - table = &mImplementationsTable; break; - default: - err() << "Error: Unknown type of entry " << static_cast<int64_t>(type) << std::endl; - } - if (table) { - table->add(std::forward<TableEntry>(entry)); - } + tableForType(type)->add(std::forward<TableEntry>(entry)); } Status ListCommand::fetchAllLibraries(const sp<IServiceManager> &manager) { diff --git a/cmds/lshal/ListCommand.h b/cmds/lshal/ListCommand.h index 45f63146b0..b10901c01e 100644 --- a/cmds/lshal/ListCommand.h +++ b/cmds/lshal/ListCommand.h @@ -134,6 +134,8 @@ protected: void forEachTable(const std::function<void(Table &)> &f); void forEachTable(const std::function<void(const Table &)> &f) const; + Table* tableForType(HalType type); + const Table* tableForType(HalType type) const; NullableOStream<std::ostream> err() const; NullableOStream<std::ostream> out() const; |