Refactor unnecessary shared_ptr usage

No need to use shared_ptr for a uint64_t. Instead just use optional to
check whether it was loaded from the JSON.

For devices without a Bus+Address specified, skip the deviceIsCreated()
check and 5x retry, since there is no point to that for non-I2C devices.

Tested: Confirmed EM still creates/probes an i2c device exposed in an
active config. For a test INA219 device exposed without an Address,
confirmed that the kernel error message (i2c i2c-x: new_device: Can't
parse I2C address) is only output once instead of 5 times.

Change-Id: Ib3778559235b19d9846b3bb973a795e82e37b123
Signed-off-by: Jonathan Doman <jonathan.doman@intel.com>
diff --git a/src/overlay.cpp b/src/overlay.cpp
index 4568699..3810c31 100644
--- a/src/overlay.cpp
+++ b/src/overlay.cpp
@@ -69,7 +69,7 @@
     return name.str();
 }
 
-void linkMux(const std::string& muxName, size_t busIndex, size_t address,
+void linkMux(const std::string& muxName, uint64_t busIndex, uint64_t address,
              const nlohmann::json::array_t& channelNames)
 {
     std::error_code ec;
@@ -119,14 +119,9 @@
     }
 }
 
-static int deleteDevice(const std::string& busPath,
-                        const std::shared_ptr<uint64_t>& address,
+static int deleteDevice(const std::string& busPath, uint64_t address,
                         const std::string& destructor)
 {
-    if (!address)
-    {
-        return -1;
-    }
     std::filesystem::path deviceDestructor(busPath);
     deviceDestructor /= destructor;
     std::ofstream deviceFile(deviceDestructor);
@@ -135,7 +130,7 @@
         std::cerr << "Error writing " << deviceDestructor << "\n";
         return -1;
     }
-    deviceFile << std::to_string(*address);
+    deviceFile << std::to_string(address);
     deviceFile.close();
     return 0;
 }
@@ -158,18 +153,12 @@
     return 0;
 }
 
-static bool deviceIsCreated(const std::string& busPath,
-                            const std::shared_ptr<uint64_t>& bus,
-                            const std::shared_ptr<uint64_t>& address,
+static bool deviceIsCreated(const std::string& busPath, uint64_t bus,
+                            uint64_t address,
                             const devices::createsHWMon hasHWMonDir)
 {
-    if (!bus || !address)
-    {
-        return false;
-    }
-
     std::filesystem::path dirPath = busPath;
-    dirPath /= deviceDirName(*bus, *address);
+    dirPath /= deviceDirName(bus, address);
     if (hasHWMonDir == devices::createsHWMon::hasHWMonDir)
     {
         dirPath /= "hwmon";
@@ -181,10 +170,8 @@
 }
 
 static int buildDevice(const std::string& busPath,
-                       const std::string& parameters,
-                       const std::shared_ptr<uint64_t>& bus,
-                       const std::shared_ptr<uint64_t>& address,
-                       const std::string& constructor,
+                       const std::string& parameters, uint64_t bus,
+                       uint64_t address, const std::string& constructor,
                        const std::string& destructor,
                        const devices::createsHWMon hasHWMonDir,
                        const size_t retries = 5)
@@ -237,8 +224,8 @@
     std::string destructor = exportTemplate.remove;
     devices::createsHWMon hasHWMonDir = exportTemplate.hasHWMonDir;
     std::string name = "unknown";
-    std::shared_ptr<uint64_t> bus = nullptr;
-    std::shared_ptr<uint64_t> address = nullptr;
+    std::optional<uint64_t> bus;
+    std::optional<uint64_t> address;
     const nlohmann::json::array_t* channels = nullptr;
 
     for (auto keyPair = configuration.begin(); keyPair != configuration.end();
@@ -260,13 +247,11 @@
 
         if (keyPair.key() == "Bus")
         {
-            bus = std::make_shared<uint64_t>(
-                *keyPair.value().get_ptr<const uint64_t*>());
+            bus = keyPair.value().get<uint64_t>();
         }
         else if (keyPair.key() == "Address")
         {
-            address = std::make_shared<uint64_t>(
-                *keyPair.value().get_ptr<const uint64_t*>());
+            address = keyPair.value().get<uint64_t>();
         }
         else if (keyPair.key() == "ChannelNames")
         {
@@ -279,14 +264,18 @@
                            subsituteString);
     }
 
-    int err = buildDevice(busPath, parameters, bus, address, constructor,
+    if (!bus || !address)
+    {
+        createDevice(busPath, parameters, constructor);
+        return;
+    }
+
+    int err = buildDevice(busPath, parameters, *bus, *address, constructor,
                           destructor, hasHWMonDir);
 
-    if ((err == 0) && boost::ends_with(type, "Mux") && bus && address &&
-        (channels != nullptr))
+    if ((err == 0) && boost::ends_with(type, "Mux") && (channels != nullptr))
     {
-        linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address),
-                *channels);
+        linkMux(name, *bus, *address, *channels);
     }
 }