Switch to a thread safe single ton

In the current state the single ton pattern used in the
repository is not thread safe.

Switching the pattern to Meyer's single ton over the trusty
leaky single ton.

Ref : https://jinyu.li/notes/meyers-singleton/

Tested By :
1. Patched post-code-manager deamon & was able to get the post
   codes with the GetPostCodesWithTimeStamp method.

Signed-off-by: Manojkiran Eda <manojkiran.eda@gmail.com>
Change-Id: I0ee5c21f15fc93e1d8c5e62a0fed33622a33c894
diff --git a/inc/post_code.hpp b/inc/post_code.hpp
index be800f2..3e82ab5 100644
--- a/inc/post_code.hpp
+++ b/inc/post_code.hpp
@@ -42,17 +42,15 @@
 // Singleton holder to store host/node and other path information
 class PostCodeDataHolder
 {
-    static PostCodeDataHolder *instance;
 
     PostCodeDataHolder()
     {
     }
 
   public:
-    static PostCodeDataHolder *getInstance()
+    static PostCodeDataHolder &getInstance()
     {
-        if (!instance)
-            instance = new PostCodeDataHolder;
+        static PostCodeDataHolder instance;
         return instance;
     }
 
@@ -89,8 +87,8 @@
 
 struct PostCode : sdbusplus::server::object_t<post_code, delete_all>
 {
-    PostCodeDataHolder *postcodeDataHolderObj =
-        postcodeDataHolderObj->getInstance();
+    PostCodeDataHolder postcodeDataHolderObj =
+        PostCodeDataHolder::getInstance();
 
     PostCode(sdbusplus::bus::bus &bus, const char *path, EventPtr & /*event*/) :
         sdbusplus::server::object_t<post_code, delete_all>(bus, path), bus(bus),
@@ -99,10 +97,10 @@
             sdbusplus::bus::match::rules::type::signal() +
                 sdbusplus::bus::match::rules::member("PropertiesChanged") +
                 sdbusplus::bus::match::rules::path(
-                    postcodeDataHolderObj->PostCodePath +
-                    std::to_string(postcodeDataHolderObj->node)) +
+                    postcodeDataHolderObj.PostCodePath +
+                    std::to_string(postcodeDataHolderObj.node)) +
                 sdbusplus::bus::match::rules::interface(
-                    postcodeDataHolderObj->PropertiesIntf),
+                    postcodeDataHolderObj.PropertiesIntf),
             [this](sdbusplus::message::message &msg) {
                 std::string objectName;
                 std::map<std::string, std::variant<postcode_t>> msgData;
@@ -122,10 +120,10 @@
             sdbusplus::bus::match::rules::type::signal() +
                 sdbusplus::bus::match::rules::member("PropertiesChanged") +
                 sdbusplus::bus::match::rules::path(
-                    postcodeDataHolderObj->HostStatePathPrefix +
-                    std::to_string(postcodeDataHolderObj->node)) +
+                    postcodeDataHolderObj.HostStatePathPrefix +
+                    std::to_string(postcodeDataHolderObj.node)) +
                 sdbusplus::bus::match::rules::interface(
-                    postcodeDataHolderObj->PropertiesIntf),
+                    postcodeDataHolderObj.PropertiesIntf),
             [this](sdbusplus::message::message &msg) {
                 std::string objectName;
                 std::map<std::string, std::variant<std::string>> msgData;
@@ -159,11 +157,11 @@
     {
         phosphor::logging::log<phosphor::logging::level::INFO>(
             "PostCode is created");
-        auto dir = fs::path(postcodeDataHolderObj->PostCodeListPathPrefix +
-                            std::to_string(postcodeDataHolderObj->node));
+        auto dir = fs::path(postcodeDataHolderObj.PostCodeListPathPrefix +
+                            std::to_string(postcodeDataHolderObj.node));
         fs::create_directories(dir);
-        strPostCodeListPath = postcodeDataHolderObj->PostCodeListPathPrefix +
-                              std::to_string(postcodeDataHolderObj->node) + "/";
+        strPostCodeListPath = postcodeDataHolderObj.PostCodeListPathPrefix +
+                              std::to_string(postcodeDataHolderObj.node) + "/";
         strCurrentBootCycleIndexName = CurrentBootCycleIndexName;
         uint16_t index = 0;
         deserialize(
diff --git a/src/main.cpp b/src/main.cpp
index 98cae3e..d808498 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -19,8 +19,8 @@
 
 int main(int argc, char* argv[])
 {
-    PostCodeDataHolder* postcodeDataHolderObj =
-        postcodeDataHolderObj->getInstance();
+    PostCodeDataHolder postcodeDataHolderObj =
+        PostCodeDataHolder::getInstance();
 
     int arg;
     int optIndex = 0;
@@ -36,7 +36,7 @@
         switch (arg)
         {
             case 'h':
-                postcodeDataHolderObj->node = std::stoi(optarg);
+                postcodeDataHolderObj.node = std::stoi(optarg);
                 break;
             default:
                 break;
@@ -60,7 +60,7 @@
     sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
     sdbusplus::server::manager_t m{bus, DBUS_OBJECT_NAME};
 
-    intfName = DBUS_INTF_NAME + std::to_string(postcodeDataHolderObj->node);
+    intfName = DBUS_INTF_NAME + std::to_string(postcodeDataHolderObj.node);
 
     bus.request_name(intfName.c_str());
 
diff --git a/src/post_code.cpp b/src/post_code.cpp
index 1fcbe55..b1ffab8 100644
--- a/src/post_code.cpp
+++ b/src/post_code.cpp
@@ -17,16 +17,14 @@
 
 #include "iomanip"
 
-PostCodeDataHolder* PostCodeDataHolder::instance = 0;
-
 void PostCode::deleteAll()
 {
-    auto dir = fs::path(postcodeDataHolderObj->PostCodeListPathPrefix +
-                        std::to_string(postcodeDataHolderObj->node));
+    auto dir = fs::path(postcodeDataHolderObj.PostCodeListPathPrefix +
+                        std::to_string(postcodeDataHolderObj.node));
     std::uintmax_t n = fs::remove_all(dir);
     std::cerr << "clearPostCodes deleted " << n << " files in "
-              << postcodeDataHolderObj->PostCodeListPathPrefix +
-                     std::to_string(postcodeDataHolderObj->node)
+              << postcodeDataHolderObj.PostCodeListPathPrefix +
+                     std::to_string(postcodeDataHolderObj.node)
               << std::endl;
     fs::create_directories(dir);
     postCodes.clear();