Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit db730530 authored by Yifan Hong's avatar Yifan Hong
Browse files

lshal: add ListCommand::tableForType

... to avoid duplicating switch/case logic.
Test: lshal_test

Change-Id: I9096534d607839ccc34dc115e76e890688a25c61
parent 20f4ee8c
Loading
Loading
Loading
Loading
+20 −34
Original line number Diff line number Diff line
@@ -287,33 +287,32 @@ bool ListCommand::shouldReportHalType(const HalType &type) const {
    return (std::find(mListTypes.begin(), mListTypes.end(), type) != mListTypes.end());
}

void ListCommand::forEachTable(const std::function<void(Table &)> &f) {
    for (const auto& type : mListTypes) {
Table* ListCommand::tableForType(HalType type) {
    switch (type) {
        case HalType::BINDERIZED_SERVICES:
                f(mServicesTable); break;
            return &mServicesTable;
        case HalType::PASSTHROUGH_CLIENTS:
                f(mPassthroughRefTable); break;
            return &mPassthroughRefTable;
        case HalType::PASSTHROUGH_LIBRARIES:
                f(mImplementationsTable); break;
            return &mImplementationsTable;
        default:
                LOG(FATAL) << __func__ << "Unknown HAL type.";
            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(const Table &)> &f) const {

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) {
        f(*tableForType(type));
    }
}

void ListCommand::postprocess() {
@@ -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) {
+2 −0
Original line number Diff line number Diff line
@@ -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;