Applied the dynamic single pattern for singleton classes
authorJiban Prakash <p.jiban@samsung.com>
Tue, 30 Apr 2013 09:08:26 +0000 (14:38 +0530)
committerGerrit Code Review <gerrit2@kim11>
Thu, 2 May 2013 03:08:15 +0000 (12:08 +0900)
Change-Id: I82f61efcf33e1a9e4cacb2889e98e71dc98a3025
Signed-off-by: Jiban Prakash <p.jiban@samsung.com>
src/FShellAppWidgetProviderManager.cpp
src/core/FShellShortcutManager.cpp
src/core/FShell_ShortcutManagerImpl.cpp

index f4af1d1..09d1a75 100644 (file)
@@ -53,6 +53,8 @@ using namespace Tizen::Graphics;
 namespace Tizen { namespace Shell
 {
 
+AppWidgetProviderManager* AppWidgetProviderManager::__pTheInstance = null;
+
 AppWidgetProviderManager::AppWidgetProviderManager()
        :__pAppWidgetProviderManagerImpl(null)
 {
@@ -64,21 +66,40 @@ AppWidgetProviderManager::~AppWidgetProviderManager()
        SysLog(NID_SHELL, "AppWidgetProviderManager is destroyed.");
 }
 
+void
+AppWidgetProviderManager::InitSingleton(void)
+{
+       std::unique_ptr<AppWidgetProviderManager> pInst(new (std::nothrow) AppWidgetProviderManager());
+       SysTryReturnVoidResult(NID_SHELL, pInst, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+       result r = pInst->Construct();
+       SysTryReturnVoidResult(NID_SHELL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
+
+       __pTheInstance = pInst.release();
+       std::atexit(DestroySingleton);
+}
+
+void
+AppWidgetProviderManager::DestroySingleton(void)
+{
+       delete __pTheInstance;
+}
+
 AppWidgetProviderManager*
 AppWidgetProviderManager::GetInstance()
 {
-       static AppWidgetProviderManager* pAppWidgetManager = null;
-       if( pAppWidgetManager == null)
+       static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
+       if (__pTheInstance == null)
        {
-               pAppWidgetManager = new (std::nothrow) AppWidgetProviderManager;
-               SysTryReturn(NID_SHELL, pAppWidgetManager, null, E_OUT_OF_MEMORY, "E_OUT_OF_MEMORY" );
-
-               result r = pAppWidgetManager->Construct();
-//             SysTryReturn(NID_SHELL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
-               SysAssertf(!IsFailed(r), "Failed to construct AppWidgetProviderManager!!");
+               ClearLastResult();
+               pthread_once(&onceBlock, InitSingleton);
+               result r = GetLastResult();
+               if (IsFailed(r))
+               {
+                       onceBlock = PTHREAD_ONCE_INIT;
+               }
        }
-
-       return pAppWidgetManager;
+       return __pTheInstance;
 }
 
 result
index dc46571..c061b11 100644 (file)
@@ -41,6 +41,8 @@ using namespace Tizen::Security;
 namespace Tizen { namespace Shell
 {
 
+ShortcutManager* ShortcutManager::__pTheInstance = null;
+
 ShortcutManager::ShortcutManager()
        :__pShortcutManagerImpl(null)
 {
@@ -50,21 +52,40 @@ ShortcutManager::~ShortcutManager()
 {
 }
 
+void
+ShortcutManager::InitSingleton(void)
+{
+       std::unique_ptr<ShortcutManager> pInst(new (std::nothrow) ShortcutManager());
+       SysTryReturnVoidResult(NID_SHELL, pInst, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+       result r = pInst->Construct();
+       SysTryReturnVoidResult(NID_SHELL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
+
+       __pTheInstance = pInst.release();
+       std::atexit(DestroySingleton);
+}
+
+void
+ShortcutManager::DestroySingleton(void)
+{
+       delete __pTheInstance;
+}
+
 ShortcutManager*
 ShortcutManager::GetInstance()
 {
-       static ShortcutManager* pShortcutManager = null;
-       if( pShortcutManager == null)
+       static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
+       if (__pTheInstance == null)
        {
-               pShortcutManager = new (std::nothrow) ShortcutManager;
-               SysTryReturn(NID_SHELL, pShortcutManager, null, E_OUT_OF_MEMORY, "E_OUT_OF_MEMORY" );
-
-               result r = pShortcutManager->Construct();
-//             SysTryReturn(NID_SHELL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
-               SysAssertf(!IsFailed(r), "Failed to construct ShortcutManager!!");
+               ClearLastResult();
+               pthread_once(&onceBlock, InitSingleton);
+               result r = GetLastResult();
+               if (IsFailed(r))
+               {
+                       onceBlock = PTHREAD_ONCE_INIT;
+               }
        }
-
-       return pShortcutManager;
+       return __pTheInstance;
 }
 
 result
@@ -80,18 +101,7 @@ ShortcutManager::AddShortcut(const AppId& appId, const String& displayName, cons
        result r = _AccessController::CheckUserPrivilege(_PRV_SHORTCUT_INSTALL);
        SysTryReturnResult(NID_SHELL, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
 
-       std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
-       std::unique_ptr<char[]> pIcon(_StringConverter::CopyToCharArrayN(iconFilePath));
-       std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(displayName));
-       std::unique_ptr<char[]> pUriData(_StringConverter::CopyToCharArrayN(uriData));
-       int type = (uriData.IsEmpty())? LAUNCH_BY_PACKAGE : LAUNCH_BY_URI;
-
-       int ret = add_to_home_shortcut(pAppId.get(), pName.get(), type, pUriData.get(), pIcon.get(), allowDuplication, NULL, this);
-       SysTryReturnResult(NID_SHELL, ret != SHORTCUT_ERROR_COMM, E_SUCCESS, "It's failed to add_to_home_shortcut, but regarded as success.");
-       SysTryReturnResult(NID_SHELL, ret != SHORTCUT_ERROR_MEMORY, E_OUT_OF_MEMORY, "It's failed to add_to_home_shortcut.");
-       SysTryReturnResult(NID_SHELL, ret == SHORTCUT_SUCCESS, E_SYSTEM, "Failed to add_to_home_shortcut");
-
-       return E_SUCCESS;
+       return __pShortcutManagerImpl->AddShortcut(appId, displayName, iconFilePath, uriData, allowDuplication);
 }
 
 result
@@ -100,15 +110,7 @@ ShortcutManager::RemoveShortcut(const Tizen::App::AppId& appId, const Tizen::Bas
        result r = _AccessController::CheckUserPrivilege(_PRV_SHORTCUT_INSTALL);
        SysTryReturnResult(NID_SHELL, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
 
-       std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
-       std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(displayName));
-
-       int ret = add_to_home_remove_shortcut(pAppId.get(), pName.get(), "", null, this);
-       SysTryReturnResult(NID_SHELL, ret != SHORTCUT_ERROR_COMM, E_SUCCESS, "It's failed to add_to_home_remove_shortcut, but regarded as success.");
-       SysTryReturnResult(NID_SHELL, ret != SHORTCUT_ERROR_MEMORY, E_OUT_OF_MEMORY, "It's failed to add_to_home_remove_shortcut.");
-       SysTryReturnResult(NID_SHELL, ret == SHORTCUT_SUCCESS, E_SYSTEM, "It's failed to add_to_home_remove_shortcut, with reason (%d)");
-
-       return E_SUCCESS;
+       return __pShortcutManagerImpl->RemoveShortcut(appId, displayName);
 }
 
 result
index fe72ae9..9e34ff8 100644 (file)
 #include <shortcut.h>
 #include <FBaseSysLog.h>
 #include <FBase.h>
+#include <FBase_StringConverter.h>
 #include <FApp.h>
 #include "FShell_ShortcutManagerImpl.h"
 
+
 namespace Tizen { namespace Shell
 {
 
@@ -139,6 +141,38 @@ _ShortcutManagerImpl::ConstructListener()
 }
 
 result
+_ShortcutManagerImpl::AddShortcut(const AppId& appId, const String& displayName, const String& iconFilePath, const String& uriData, bool allowDuplication)
+{
+       SysLog(NID_SHELL, "_ShortcutManagerImpl::AddShortcut enters.");
+       std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
+       std::unique_ptr<char[]> pIcon(_StringConverter::CopyToCharArrayN(iconFilePath));
+       std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(displayName));
+       std::unique_ptr<char[]> pUriData(_StringConverter::CopyToCharArrayN(uriData));
+       int type = (uriData.IsEmpty())? LAUNCH_BY_PACKAGE : LAUNCH_BY_URI;
+
+       int ret = add_to_home_shortcut(pAppId.get(), pName.get(), type, pUriData.get(), pIcon.get(), allowDuplication, NULL, this);
+       SysTryReturnResult(NID_SHELL, ret != SHORTCUT_ERROR_COMM, E_SUCCESS, "It's failed to add_to_home_shortcut, but regarded as success.");
+       SysTryReturnResult(NID_SHELL, ret != SHORTCUT_ERROR_MEMORY, E_OUT_OF_MEMORY, "It's failed to add_to_home_shortcut.");
+       SysTryReturnResult(NID_SHELL, ret == SHORTCUT_SUCCESS, E_SYSTEM, "Failed to add_to_home_shortcut");
+       SysLog(NID_SHELL, "_ShortcutManagerImpl::AddShortcut exits.");
+       return E_SUCCESS;
+}
+
+result
+_ShortcutManagerImpl::RemoveShortcut(const Tizen::App::AppId& appId, const Tizen::Base::String& displayName)
+{
+       std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
+       std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(displayName));
+
+       int ret = add_to_home_remove_shortcut(pAppId.get(), pName.get(), "", null, this);
+       SysTryReturnResult(NID_SHELL, ret != SHORTCUT_ERROR_COMM, E_SUCCESS, "It's failed to add_to_home_remove_shortcut, but regarded as success.");
+       SysTryReturnResult(NID_SHELL, ret != SHORTCUT_ERROR_MEMORY, E_OUT_OF_MEMORY, "It's failed to add_to_home_remove_shortcut.");
+       SysTryReturnResult(NID_SHELL, ret == SHORTCUT_SUCCESS, E_SYSTEM, "It's failed to add_to_home_remove_shortcut, with reason (%d)");
+
+       return E_SUCCESS;
+}
+
+result
 _ShortcutManagerImpl::AddShortcutRequestListener(IShortcutRequestListener& listener)
 {
        SysTryReturnResult(NID_SHELL, !__shortcutRequestListenerList.Contains(&listener), E_OBJ_ALREADY_EXIST, "The listener is already added.");