Refine BridgeGpio object creation logic

This commit refines BridgeGpio object creation logic by using explicit
std::move to prevent object copy. Also, for cases of rescanning of
ADCSensors, it adjusts timing of destroying for the previous ADCSensor
to avoid double request of a gpio line.

Tested: adcsensor service was started without making a below error
even in case of a recreation sensors caused by rescanning:
  Error requesting gpio: P3VBAT_BRIDGE_EN
  terminate called after throwing an instance of 'std::system_error'
  what():  error setting GPIO line values: Operation not permitted

Change-Id: I26ca059f8fe6eea2c60605f19ec49a3e365dc66f
Signed-off-by: Jae Hyun Yoo <jae.hyun.yoo@linux.intel.com>
diff --git a/include/ADCSensor.hpp b/include/ADCSensor.hpp
index e061138..331a229 100644
--- a/include/ADCSensor.hpp
+++ b/include/ADCSensor.hpp
@@ -55,7 +55,7 @@
               std::vector<thresholds::Threshold>&& thresholds,
               const double scaleFactor, PowerState readState,
               const std::string& sensorConfiguration,
-              std::optional<BridgeGpio> bridgeGpio);
+              std::optional<BridgeGpio>&& bridgeGpio);
     ~ADCSensor();
 
   private:
diff --git a/src/ADCSensor.cpp b/src/ADCSensor.cpp
index ab1481b..ec3efdb 100644
--- a/src/ADCSensor.cpp
+++ b/src/ADCSensor.cpp
@@ -45,13 +45,14 @@
                      std::vector<thresholds::Threshold>&& _thresholds,
                      const double scaleFactor, PowerState readState,
                      const std::string& sensorConfiguration,
-                     std::optional<BridgeGpio> bridgeGpio) :
+                     std::optional<BridgeGpio>&& bridgeGpio) :
     Sensor(boost::replace_all_copy(sensorName, " ", "_"),
            std::move(_thresholds), sensorConfiguration,
            "xyz.openbmc_project.Configuration.ADC", maxReading, minReading),
     objServer(objectServer), scaleFactor(scaleFactor), path(path),
     readState(std::move(readState)), inputDev(io, open(path.c_str(), O_RDONLY)),
-    waitTimer(io), errCount(0), thresholdTimer(io, this), bridgeGpio(bridgeGpio)
+    waitTimer(io), errCount(0), thresholdTimer(io, this),
+    bridgeGpio(std::move(bridgeGpio))
 {
     sensorInterface = objectServer.add_interface(
         "/xyz/openbmc_project/sensors/voltage/" + name,
diff --git a/src/ADCSensorMain.cpp b/src/ADCSensorMain.cpp
index 8a88244..4fdca29 100644
--- a/src/ADCSensorMain.cpp
+++ b/src/ADCSensorMain.cpp
@@ -230,6 +230,9 @@
             }
         }
 
+        auto& sensor = sensors[sensorName];
+        sensor = nullptr;
+
         std::optional<BridgeGpio> bridgeGpio;
         for (const SensorBaseConfiguration& suppConfig : *sensorData)
         {
@@ -259,12 +262,10 @@
             }
         }
 
-        auto& sensor = sensors[sensorName];
-        sensor = nullptr;
         sensor = std::make_unique<ADCSensor>(
             path.string(), objectServer, dbusConnection, io, sensorName,
             std::move(sensorThresholds), scaleFactor, readState, *interfacePath,
-            bridgeGpio);
+            std::move(bridgeGpio));
     }
 }