From 081714c7d12b6dbdf9e433d891cdbb825765feec Mon Sep 17 00:00:00 2001 From: Jiban Prakash Date: Tue, 30 Apr 2013 14:38:26 +0530 Subject: [PATCH] Applied the dynamic single pattern for singleton classes Change-Id: I82f61efcf33e1a9e4cacb2889e98e71dc98a3025 Signed-off-by: Jiban Prakash --- src/FShellAppWidgetProviderManager.cpp | 41 +++++++++++++++------ src/core/FShellShortcutManager.cpp | 64 +++++++++++++++++---------------- src/core/FShell_ShortcutManagerImpl.cpp | 34 ++++++++++++++++++ 3 files changed, 98 insertions(+), 41 deletions(-) diff --git a/src/FShellAppWidgetProviderManager.cpp b/src/FShellAppWidgetProviderManager.cpp index f4af1d1..09d1a75 100644 --- a/src/FShellAppWidgetProviderManager.cpp +++ b/src/FShellAppWidgetProviderManager.cpp @@ -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 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 diff --git a/src/core/FShellShortcutManager.cpp b/src/core/FShellShortcutManager.cpp index dc46571..c061b11 100644 --- a/src/core/FShellShortcutManager.cpp +++ b/src/core/FShellShortcutManager.cpp @@ -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 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 pAppId(_StringConverter::CopyToCharArrayN(appId)); - std::unique_ptr pIcon(_StringConverter::CopyToCharArrayN(iconFilePath)); - std::unique_ptr pName(_StringConverter::CopyToCharArrayN(displayName)); - std::unique_ptr 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 pAppId(_StringConverter::CopyToCharArrayN(appId)); - std::unique_ptr 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 diff --git a/src/core/FShell_ShortcutManagerImpl.cpp b/src/core/FShell_ShortcutManagerImpl.cpp index fe72ae9..9e34ff8 100644 --- a/src/core/FShell_ShortcutManagerImpl.cpp +++ b/src/core/FShell_ShortcutManagerImpl.cpp @@ -24,9 +24,11 @@ #include #include #include +#include #include #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 pAppId(_StringConverter::CopyToCharArrayN(appId)); + std::unique_ptr pIcon(_StringConverter::CopyToCharArrayN(iconFilePath)); + std::unique_ptr pName(_StringConverter::CopyToCharArrayN(displayName)); + std::unique_ptr 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 pAppId(_StringConverter::CopyToCharArrayN(appId)); + std::unique_ptr 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."); -- 2.7.4