Add Named<T> in place of std::pair<std::string, T>
... for better readability.
Test: libvintf_test
Change-Id: I50c76ad696c043a89e8ba3168f9bfc77a16924e6
diff --git a/AssembleVintf.cpp b/AssembleVintf.cpp
index 4174e93..8807da0 100644
--- a/AssembleVintf.cpp
+++ b/AssembleVintf.cpp
@@ -267,23 +267,23 @@
std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
template <typename S>
- using Schemas = std::vector<std::pair<std::string, S>>;
+ using Schemas = std::vector<Named<S>>;
using HalManifests = Schemas<HalManifest>;
using CompatibilityMatrices = Schemas<CompatibilityMatrix>;
bool assembleHalManifest(HalManifests* halManifests) {
std::string error;
- HalManifest* halManifest = &halManifests->front().second;
+ HalManifest* halManifest = &halManifests->front().object;
for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
- const std::string& path = it->first;
- HalManifest& halToAdd = it->second;
+ const std::string& path = it->name;
+ HalManifest& halToAdd = it->object;
if (halToAdd.level() != Level::UNSPECIFIED) {
if (halManifest->level() == Level::UNSPECIFIED) {
halManifest->mLevel = halToAdd.level();
} else if (halManifest->level() != halToAdd.level()) {
std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
- << " File '" << halManifests->front().first << "' has level "
+ << " File '" << halManifests->front().name << "' has level "
<< halManifest->level() << std::endl
<< " File '" << path << "' has level " << halToAdd.level()
<< std::endl;
@@ -401,8 +401,8 @@
Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
Level ret = Level::UNSPECIFIED;
for (const auto& e : matrices) {
- if (ret == Level::UNSPECIFIED || ret > e.second.level()) {
- ret = e.second.level();
+ if (ret == Level::UNSPECIFIED || ret > e.object.level()) {
+ ret = e.object.level();
}
}
return ret;
@@ -414,11 +414,11 @@
KernelSepolicyVersion kernelSepolicyVers;
Version sepolicyVers;
std::unique_ptr<HalManifest> checkManifest;
- if (matrices->front().second.mType == SchemaType::DEVICE) {
- matrix = &matrices->front().second;
+ if (matrices->front().object.mType == SchemaType::DEVICE) {
+ matrix = &matrices->front().object;
}
- if (matrices->front().second.mType == SchemaType::FRAMEWORK) {
+ if (matrices->front().object.mType == SchemaType::FRAMEWORK) {
Level deviceLevel = Level::UNSPECIFIED;
if (mCheckFile != nullptr) {
checkManifest = std::make_unique<HalManifest>();
@@ -438,7 +438,7 @@
if (deviceLevel == Level::UNSPECIFIED) {
// building empty.xml
- matrix = &matrices->front().second;
+ matrix = &matrices->front().object;
} else {
matrix = CompatibilityMatrix::combine(deviceLevel, matrices, &error);
if (matrix == nullptr) {
@@ -470,8 +470,8 @@
out() << "<!--" << std::endl;
out() << " Input:" << std::endl;
for (const auto& e : *matrices) {
- if (!e.first.empty()) {
- out() << " " << getFileNameFromPath(e.first) << std::endl;
+ if (!e.name.empty()) {
+ out() << " " << getFileNameFromPath(e.name) << std::endl;
}
}
out() << "-->" << std::endl;
diff --git a/CompatibilityMatrix.cpp b/CompatibilityMatrix.cpp
index 98d4414..076055c 100644
--- a/CompatibilityMatrix.cpp
+++ b/CompatibilityMatrix.cpp
@@ -172,23 +172,22 @@
// Find compatibility_matrix.empty.xml (which has unspecified level) and use it
// as a base matrix.
CompatibilityMatrix* CompatibilityMatrix::findOrInsertBaseMatrix(
- std::vector<std::pair<std::string, CompatibilityMatrix>>* matrices, std::string* error) {
+ std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) {
bool multipleFound = false;
CompatibilityMatrix* matrix = nullptr;
for (auto& e : *matrices) {
- if (e.second.level() == Level::UNSPECIFIED) {
- if (!e.second.mHals.empty()) {
+ if (e.object.level() == Level::UNSPECIFIED) {
+ if (!e.object.mHals.empty()) {
if (error) {
- *error =
- "Error: File \"" + e.first + "\" should not contain " + "HAL elements.";
+ *error = "Error: File \"" + e.name + "\" should not contain " + "HAL elements.";
}
return nullptr;
}
- if (!e.second.mXmlFiles.empty()) {
+ if (!e.object.mXmlFiles.empty()) {
if (error) {
- *error = "Error: File \"" + e.first + "\" should not contain " +
- "XML File elements.";
+ *error =
+ "Error: File \"" + e.name + "\" should not contain " + "XML File elements.";
}
return nullptr;
}
@@ -197,7 +196,7 @@
multipleFound = true;
}
- matrix = &e.second;
+ matrix = &e.object;
// continue to detect multiple files with "unspecified" levels
}
}
@@ -208,8 +207,8 @@
"Error: multiple framework compatibility matrix files have "
"unspecified level; there should only be one such file.\n";
for (auto& e : *matrices) {
- if (e.second.level() == Level::UNSPECIFIED) {
- *error += " " + e.first + "\n";
+ if (e.object.level() == Level::UNSPECIFIED) {
+ *error += " " + e.name + "\n";
}
}
}
@@ -217,7 +216,7 @@
}
if (matrix == nullptr) {
- matrix = &matrices->emplace(matrices->end())->second;
+ matrix = &matrices->emplace(matrices->end())->object;
matrix->mType = SchemaType::FRAMEWORK;
matrix->mLevel = Level::UNSPECIFIED;
}
@@ -225,9 +224,9 @@
return matrix;
}
-CompatibilityMatrix* CompatibilityMatrix::combine(
- Level deviceLevel, std::vector<std::pair<std::string, CompatibilityMatrix>>* matrices,
- std::string* error) {
+CompatibilityMatrix* CompatibilityMatrix::combine(Level deviceLevel,
+ std::vector<Named<CompatibilityMatrix>>* matrices,
+ std::string* error) {
if (deviceLevel == Level::UNSPECIFIED) {
if (error) {
*error = "Error: device level is unspecified.";
@@ -243,18 +242,18 @@
matrix->mLevel = deviceLevel;
for (auto& e : *matrices) {
- if (&e.second != matrix && e.second.level() == deviceLevel) {
- if (!matrix->addAllHals(&e.second, error)) {
+ if (&e.object != matrix && e.object.level() == deviceLevel) {
+ if (!matrix->addAllHals(&e.object, error)) {
if (error) {
- *error = "File \"" + e.first + "\" cannot be added: HAL " + *error +
+ *error = "File \"" + e.name + "\" cannot be added: HAL " + *error +
" has a conflict.";
}
return nullptr;
}
- if (!matrix->addAllXmlFiles(&e.second, error)) {
+ if (!matrix->addAllXmlFiles(&e.object, error)) {
if (error) {
- *error = "File \"" + e.first + "\" cannot be added: XML File entry " + *error +
+ *error = "File \"" + e.name + "\" cannot be added: XML File entry " + *error +
" has a conflict.";
}
return nullptr;
@@ -263,20 +262,20 @@
}
for (auto& e : *matrices) {
- if (&e.second != matrix && e.second.level() != Level::UNSPECIFIED &&
- e.second.level() > deviceLevel) {
- if (!matrix->addAllHalsAsOptional(&e.second, error)) {
+ if (&e.object != matrix && e.object.level() != Level::UNSPECIFIED &&
+ e.object.level() > deviceLevel) {
+ if (!matrix->addAllHalsAsOptional(&e.object, error)) {
if (error) {
- *error = "File \"" + e.first + "\" cannot be added: " + *error +
+ *error = "File \"" + e.name + "\" cannot be added: " + *error +
". See <hal> with the same name " +
"in previously parsed files or previously declared in this file.";
}
return nullptr;
}
- if (!matrix->addAllXmlFilesAsOptional(&e.second, error)) {
+ if (!matrix->addAllXmlFilesAsOptional(&e.object, error)) {
if (error) {
- *error = "File \"" + e.first + "\" cannot be added: XML File entry " + *error +
+ *error = "File \"" + e.name + "\" cannot be added: XML File entry " + *error +
" has a conflict.";
}
return nullptr;
diff --git a/include/vintf/CompatibilityMatrix.h b/include/vintf/CompatibilityMatrix.h
index 9be80ed..62b505a 100644
--- a/include/vintf/CompatibilityMatrix.h
+++ b/include/vintf/CompatibilityMatrix.h
@@ -27,6 +27,7 @@
#include "MapValueIterator.h"
#include "MatrixHal.h"
#include "MatrixKernel.h"
+#include "Named.h"
#include "SchemaType.h"
#include "Sepolicy.h"
#include "Vndk.h"
@@ -80,11 +81,11 @@
// - If level() == deviceLevel, all HAL versions and XML files are added as is
// (optionality is kept)
// - If level() > deviceLevel, all HAL versions and XML files are added as optional.
- static CompatibilityMatrix* combine(
- Level deviceLevel, std::vector<std::pair<std::string, CompatibilityMatrix>>* matrices,
- std::string* error);
+ static CompatibilityMatrix* combine(Level deviceLevel,
+ std::vector<Named<CompatibilityMatrix>>* matrices,
+ std::string* error);
static CompatibilityMatrix* findOrInsertBaseMatrix(
- std::vector<std::pair<std::string, CompatibilityMatrix>>* matrices, std::string* error);
+ std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error);
friend struct HalManifest;
friend struct RuntimeInfo;
diff --git a/include/vintf/Named.h b/include/vintf/Named.h
new file mode 100644
index 0000000..eacb70b
--- /dev/null
+++ b/include/vintf/Named.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_VINTF_NAMED_H
+#define ANDROID_VINTF_NAMED_H
+
+#include <string>
+
+template <typename T>
+struct Named {
+ std::string name;
+ T object;
+
+ Named() = default;
+ Named(const std::string& n, const T& o) : name(n), object(o) {}
+ Named(std::string&& n, T&& o) : name(std::move(n)), object(std::move(o)) {}
+};
+
+#endif // ANDROID_VINTF_NAMED_H