regex-instance: HalInterface: add regex API and hide public fields

<regex-instance> is added under an <interface> tag,
which is represented by an HalInterface object. Add
API for looping over all <regex-instance>s / <instance>s
under it.

With this API, HalInterface can hide its public fields.

Bug: 73738616
Test: libvintf_test
Test: vintf_object_test
Test: vts_treble_vintf_test

Change-Id: Ia072dd7e946c28789797db8a46fd968bc7872dca
Merged-In: Ia072dd7e946c28789797db8a46fd968bc7872dca
diff --git a/HalInterface.cpp b/HalInterface.cpp
index 2dbf57c..cec33af 100644
--- a/HalInterface.cpp
+++ b/HalInterface.cpp
@@ -23,13 +23,37 @@
 namespace vintf {
 
 bool operator==(const HalInterface& lft, const HalInterface& rgt) {
-    if (lft.name != rgt.name)
-        return false;
-    if (lft.instances != rgt.instances)
-        return false;
+    if (lft.mName != rgt.mName) return false;
+    if (lft.mInstances != rgt.mInstances) return false;
     return true;
 }
 
+bool HalInterface::forEachInstance(
+    const std::function<bool(const std::string&, const std::string&, bool isRegex)>& func) const {
+    for (const auto& instance : mInstances) {
+        if (!func(mName, instance, false /* isRegex */)) {
+            return false;
+        }
+    }
+    return true;
+}
+
+bool HalInterface::hasAnyInstance() const {
+    bool found = false;
+    forEachInstance([&found](const auto&, const auto&, bool) {
+        found = true;
+        return false;  // break;
+    });
+    return found;
+}
+
+bool HalInterface::insertInstance(const std::string& instanceOrPattern, bool /*isRegex*/) {
+    return mInstances.insert(instanceOrPattern).second;
+}
+
+bool HalInterface::removeInstance(const std::string& instanceOrPattern, bool /*isRegex*/) {
+    return mInstances.erase(instanceOrPattern) > 0;
+}
+
 } // namespace vintf
 } // namespace android
-