[REFACTOR] embed file name into manifest/matrix object

Instead of attaching name to the object as a new type Named<T>,
embed the file name or file path of the object into the object
directly.

The type Named<T> is re-written so that its subclass extends its
setFileName() and fileName() methods.

Test: libvintf_test, vintf_object_test
Bug: 118757553

Change-Id: Ib3356cf56632b00c77c26df1cfa1a1aaf8878876
diff --git a/CompatibilityMatrix.cpp b/CompatibilityMatrix.cpp
index a2ef873..ae13d99 100644
--- a/CompatibilityMatrix.cpp
+++ b/CompatibilityMatrix.cpp
@@ -303,6 +303,7 @@
 }
 
 bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) {
+    // ignore fileName().
     return lft.mType == rgt.mType && lft.mLevel == rgt.mLevel && lft.mHals == rgt.mHals &&
            lft.mXmlFiles == rgt.mXmlFiles &&
            (lft.mType != SchemaType::DEVICE ||
@@ -320,12 +321,12 @@
 }
 
 std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combine(
-    Level deviceLevel, std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) {
+    Level deviceLevel, std::vector<CompatibilityMatrix>* matrices, std::string* error) {
     // Check type.
     for (const auto& e : *matrices) {
-        if (e.object.type() != SchemaType::FRAMEWORK) {
+        if (e.type() != SchemaType::FRAMEWORK) {
             if (error) {
-                *error = "File \"" + e.name + "\" is not a framework compatibility matrix.";
+                *error = "File \"" + e.fileName() + "\" is not a framework compatibility matrix.";
                 return nullptr;
             }
         }
@@ -333,15 +334,15 @@
 
     // Matrices with unspecified (empty) level are auto-filled with deviceLevel.
     for (auto& e : *matrices) {
-        if (e.object.level() == Level::UNSPECIFIED) {
-            e.object.mLevel = deviceLevel;
+        if (e.level() == Level::UNSPECIFIED) {
+            e.mLevel = deviceLevel;
         }
     }
 
     // Add from low to high FCM version so that optional <kernel> requirements are added correctly.
     // See comment in addAllAsOptional.
     std::sort(matrices->begin(), matrices->end(),
-              [](const auto& x, const auto& y) { return x.object.level() < y.object.level(); });
+              [](const auto& x, const auto& y) { return x.level() < y.level(); });
 
     auto baseMatrix = std::make_unique<CompatibilityMatrix>();
     baseMatrix->mLevel = deviceLevel;
@@ -349,31 +350,31 @@
 
     std::vector<std::string> parsedFiles;
     for (auto& e : *matrices) {
-        if (e.object.level() < deviceLevel) {
+        if (e.level() < deviceLevel) {
             continue;
         }
 
         bool success = false;
-        if (e.object.level() == deviceLevel) {
+        if (e.level() == deviceLevel) {
             success = baseMatrix->addAll(&e, error);
         } else {
             success = baseMatrix->addAllAsOptional(&e, error);
         }
         if (!success) {
             if (error) {
-                *error = "Conflict when merging \"" + e.name + "\": " + *error + "\n" +
+                *error = "Conflict when merging \"" + e.fileName() + "\": " + *error + "\n" +
                          "Previous files:\n" + base::Join(parsedFiles, "\n");
             }
             return nullptr;
         }
-        parsedFiles.push_back(e.name);
+        parsedFiles.push_back(e.fileName());
     }
 
     return baseMatrix;
 }
 
 std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combineDeviceMatrices(
-    std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) {
+    std::vector<CompatibilityMatrix>* matrices, std::string* error) {
     auto baseMatrix = std::make_unique<CompatibilityMatrix>();
     baseMatrix->mType = SchemaType::DEVICE;
 
@@ -382,36 +383,34 @@
         bool success = baseMatrix->addAll(&e, error);
         if (!success) {
             if (error) {
-                *error = "Conflict when merging \"" + e.name + "\": " + *error + "\n" +
+                *error = "Conflict when merging \"" + e.fileName() + "\": " + *error + "\n" +
                          "Previous files:\n" + base::Join(parsedFiles, "\n");
             }
             return nullptr;
         }
-        parsedFiles.push_back(e.name);
+        parsedFiles.push_back(e.fileName());
     }
     return baseMatrix;
 }
 
-bool CompatibilityMatrix::addAll(Named<CompatibilityMatrix>* inputMatrix, std::string* error) {
-    if (!addAllHals(&inputMatrix->object, error) || !addAllXmlFiles(&inputMatrix->object, error) ||
-        !addAllKernels(&inputMatrix->object, error) || !addSepolicy(&inputMatrix->object, error) ||
-        !addAvbMetaVersion(&inputMatrix->object, error) || !addVndk(&inputMatrix->object, error) ||
-        !addVendorNdk(&inputMatrix->object, error) || !addSystemSdk(&inputMatrix->object, error)) {
+bool CompatibilityMatrix::addAll(CompatibilityMatrix* inputMatrix, std::string* error) {
+    if (!addAllHals(inputMatrix, error) || !addAllXmlFiles(inputMatrix, error) ||
+        !addAllKernels(inputMatrix, error) || !addSepolicy(inputMatrix, error) ||
+        !addAvbMetaVersion(inputMatrix, error) || !addVndk(inputMatrix, error) ||
+        !addVendorNdk(inputMatrix, error) || !addSystemSdk(inputMatrix, error)) {
         if (error) {
-            *error = "File \"" + inputMatrix->name + "\" cannot be added: " + *error + ".";
+            *error = "File \"" + inputMatrix->fileName() + "\" cannot be added: " + *error + ".";
         }
         return false;
     }
     return true;
 }
 
-bool CompatibilityMatrix::addAllAsOptional(Named<CompatibilityMatrix>* inputMatrix,
-                                           std::string* error) {
-    if (!addAllHalsAsOptional(&inputMatrix->object, error) ||
-        !addAllXmlFilesAsOptional(&inputMatrix->object, error) ||
-        !addAllKernels(&inputMatrix->object, error)) {
+bool CompatibilityMatrix::addAllAsOptional(CompatibilityMatrix* inputMatrix, std::string* error) {
+    if (!addAllHalsAsOptional(inputMatrix, error) ||
+        !addAllXmlFilesAsOptional(inputMatrix, error) || !addAllKernels(inputMatrix, error)) {
         if (error) {
-            *error = "File \"" + inputMatrix->name + "\" cannot be added: " + *error;
+            *error = "File \"" + inputMatrix->fileName() + "\" cannot be added: " + *error;
         }
         return false;
     }