EntityManager: Clean up use of probeTypes

Remove the duplication by introducing findProbeType() to hide the map
definition.

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I62704bac7aef6c6ea5d9f81aee429861b13cca6c
diff --git a/include/EntityManager.hpp b/include/EntityManager.hpp
index a79e7fe..a80b5f9 100644
--- a/include/EntityManager.hpp
+++ b/include/EntityManager.hpp
@@ -27,6 +27,7 @@
 
 #include <iostream>
 #include <list>
+#include <optional>
 #include <string>
 
 // paths - > interfaces -> properties
@@ -59,6 +60,11 @@
     MATCH_ONE
 };
 
+using FoundProbeTypeT =
+    std::optional<boost::container::flat_map<const char*, probe_type_codes,
+                                             CmpStr>::const_iterator>;
+FoundProbeTypeT findProbeType(const std::string& probe);
+
 struct PerformScan : std::enable_shared_from_this<PerformScan>
 {
 
diff --git a/src/EntityManager.cpp b/src/EntityManager.cpp
index 8bd97fd..a46806a 100644
--- a/src/EntityManager.cpp
+++ b/src/EntityManager.cpp
@@ -39,6 +39,7 @@
 #include <filesystem>
 #include <fstream>
 #include <iostream>
+#include <map>
 #include <regex>
 #include <variant>
 constexpr const char* configurationDirectory = PACKAGE_DIR "configurations";
@@ -49,7 +50,7 @@
 constexpr const char* globalSchema = "global.json";
 constexpr const bool debug = false;
 
-static const boost::container::flat_map<const char*, probe_type_codes, CmpStr>
+const boost::container::flat_map<const char*, probe_type_codes, CmpStr>
     probeTypes{{{"FALSE", probe_type_codes::FALSE_T},
                 {"TRUE", probe_type_codes::TRUE_T},
                 {"AND", probe_type_codes::AND},
@@ -84,6 +85,22 @@
 const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]");
 const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]");
 
+FoundProbeTypeT findProbeType(const std::string& probe)
+{
+    boost::container::flat_map<const char*, probe_type_codes,
+                               CmpStr>::const_iterator probeType;
+    for (probeType = probeTypes.begin(); probeType != probeTypes.end();
+         ++probeType)
+    {
+        if (probe.find(probeType->first) != std::string::npos)
+        {
+            return probeType;
+        }
+    }
+
+    return std::nullopt;
+}
+
 static std::shared_ptr<sdbusplus::asio::dbus_interface>
     createInterface(sdbusplus::asio::object_server& objServer,
                     const std::string& path, const std::string& interface,
diff --git a/src/PerformProbe.cpp b/src/PerformProbe.cpp
index 7024e47..bd3e1b3 100644
--- a/src/PerformProbe.cpp
+++ b/src/PerformProbe.cpp
@@ -22,15 +22,6 @@
 
 constexpr const bool debug = false;
 
-/* Keep this in sync with EntityManager.cpp */
-static const boost::container::flat_map<const char*, probe_type_codes, CmpStr>
-    probeTypes{{{"FALSE", probe_type_codes::FALSE_T},
-                {"TRUE", probe_type_codes::TRUE_T},
-                {"AND", probe_type_codes::AND},
-                {"OR", probe_type_codes::OR},
-                {"FOUND", probe_type_codes::FOUND},
-                {"MATCH_ONE", probe_type_codes::MATCH_ONE}}};
-
 // probes dbus interface dictionary for a key with a value that matches a regex
 // When an interface passes a probe, also save its D-Bus path with it.
 bool probeDbus(const std::string& interface,
@@ -99,22 +90,10 @@
 
     for (auto& probe : probeCommand)
     {
-        bool foundProbe = false;
-        boost::container::flat_map<const char*, probe_type_codes,
-                                   CmpStr>::const_iterator probeType;
-
-        for (probeType = probeTypes.begin(); probeType != probeTypes.end();
-             ++probeType)
+        FoundProbeTypeT probeType = findProbeType(probe);
+        if (probeType)
         {
-            if (probe.find(probeType->first) != std::string::npos)
-            {
-                foundProbe = true;
-                break;
-            }
-        }
-        if (foundProbe)
-        {
-            switch (probeType->second)
+            switch ((*probeType)->second)
             {
                 case probe_type_codes::FALSE_T:
                 {
@@ -188,6 +167,7 @@
             {
                 return false;
             }
+            bool foundProbe = !!probeType;
             std::string probeInterface = probe.substr(0, findStart);
             cur = probeDbus(probeInterface, dbusProbeMap, foundDevs, scan,
                             foundProbe);
@@ -209,8 +189,8 @@
             ret = cur;
             first = false;
         }
-        lastCommand = probeType != probeTypes.end() ? probeType->second
-                                                    : probe_type_codes::FALSE_T;
+        lastCommand =
+            probeType ? (*probeType)->second : probe_type_codes::FALSE_T;
     }
 
     // probe passed, but empty device
diff --git a/src/PerformScan.cpp b/src/PerformScan.cpp
index 70a4e47..5978710 100644
--- a/src/PerformScan.cpp
+++ b/src/PerformScan.cpp
@@ -30,15 +30,6 @@
     propertiesChangedCallback(nlohmann::json& systemConfiguration,
                               sdbusplus::asio::object_server& objServer);
 
-/* Keep this in sync with EntityManager.cpp */
-static const boost::container::flat_map<const char*, probe_type_codes, CmpStr>
-    probeTypes{{{"FALSE", probe_type_codes::FALSE_T},
-                {"TRUE", probe_type_codes::TRUE_T},
-                {"AND", probe_type_codes::AND},
-                {"OR", probe_type_codes::OR},
-                {"FOUND", probe_type_codes::FOUND},
-                {"MATCH_ONE", probe_type_codes::MATCH_ONE}}};
-
 using GetSubTreeType = std::vector<
     std::pair<std::string,
               std::vector<std::pair<std::string, std::vector<std::string>>>>>;
@@ -606,19 +597,7 @@
                 std::cerr << "Probe statement wasn't a string, can't parse";
                 continue;
             }
-            bool found = false;
-            boost::container::flat_map<const char*, probe_type_codes,
-                                       CmpStr>::const_iterator probeType;
-            for (probeType = probeTypes.begin(); probeType != probeTypes.end();
-                 ++probeType)
-            {
-                if (probe->find(probeType->first) != std::string::npos)
-                {
-                    found = true;
-                    break;
-                }
-            }
-            if (found)
+            if (findProbeType(probe->c_str()))
             {
                 continue;
             }