From 84fe6f3266f48088d98c8332c41172d6e92250be Mon Sep 17 00:00:00 2001 From: Inki Dae Date: Mon, 17 Jun 2024 15:16:47 +0900 Subject: [PATCH] service: code refactoring to ServiceFactory class Do code refactoring to ServiceFactory class by changing the use of macro - REGISTER_BACKDEND() - to the use of template. Change-Id: Idecb462c67150cfdbb0794b14e93124033fa9693 Signed-off-by: Inki Dae --- services/auto_zoom/include/AutoZoom.h | 1 + services/auto_zoom/src/AutoZoom.cpp | 2 +- services/common/include/ServiceFactory.h | 35 +++++++++++++++++------- services/common/src/ServiceFactory.cpp | 28 ------------------- services/singleo_native_capi.cpp | 2 +- 5 files changed, 28 insertions(+), 40 deletions(-) delete mode 100644 services/common/src/ServiceFactory.cpp diff --git a/services/auto_zoom/include/AutoZoom.h b/services/auto_zoom/include/AutoZoom.h index ccf8b25..1defe0d 100644 --- a/services/auto_zoom/include/AutoZoom.h +++ b/services/auto_zoom/include/AutoZoom.h @@ -38,6 +38,7 @@ namespace autozoom class AutoZoom : public IService, public input::IInputObserver { private: + static bool _registered; std::unique_ptr _taskManager; std::unique_ptr _input_service; std::unique_ptr _postprocessor; diff --git a/services/auto_zoom/src/AutoZoom.cpp b/services/auto_zoom/src/AutoZoom.cpp index ce35e40..13430ad 100644 --- a/services/auto_zoom/src/AutoZoom.cpp +++ b/services/auto_zoom/src/AutoZoom.cpp @@ -39,7 +39,7 @@ namespace services { namespace autozoom { -REGISTER_SERVICE(AutoZoom) +bool AutoZoom::_registered = registerService("AutoZoom"); void BridgeNodeCallback(INode *node) { diff --git a/services/common/include/ServiceFactory.h b/services/common/include/ServiceFactory.h index 77216ae..62f8685 100644 --- a/services/common/include/ServiceFactory.h +++ b/services/common/include/ServiceFactory.h @@ -20,6 +20,7 @@ #include #include #include +#include #include "IService.h" #include "InputTypes.h" @@ -30,24 +31,38 @@ namespace services class ServiceFactory { public: - using createFunc = IService *(*) (); - static std::map __service_table; + using createFunc = IService *(*)(); - static IService *create(const std::string &className) + static ServiceFactory &instance() { - auto it = __service_table.find(className); + static ServiceFactory factory; + return factory; + } + + IService *create(const std::string &name) + { + auto it = __service_table.find(name); if (it != __service_table.end()) return it->second(); - return nullptr; + throw singleo::exception::InvalidParameter("Invalid service."); + } + + void registerService(const std::string &name, const createFunc &func) + { + __service_table[name] = func; } + +private: + std::unordered_map __service_table; }; -#define REGISTER_SERVICE(name) \ - const bool is##nameRegistered = [] { \ - ServiceFactory::__service_table[#name] = &name::create; \ - return true; \ - }(); +template bool registerService(const std::string &name) +{ + ServiceFactory::instance().registerService(name, []() { return dynamic_cast(new ServiceType()); }); + + return true; +} } } diff --git a/services/common/src/ServiceFactory.cpp b/services/common/src/ServiceFactory.cpp deleted file mode 100644 index 85e6af7..0000000 --- a/services/common/src/ServiceFactory.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "ServiceFactory.h" - -using namespace std; - -namespace singleo -{ -namespace services -{ -map ServiceFactory::__service_table; - -} -} \ No newline at end of file diff --git a/services/singleo_native_capi.cpp b/services/singleo_native_capi.cpp index d353842..6774a0e 100644 --- a/services/singleo_native_capi.cpp +++ b/services/singleo_native_capi.cpp @@ -28,7 +28,7 @@ int singleo_service_create(const char *option, singleo_service_h *handle) async_mode = parser.getAsyncMode(); if (ServiceType::AUTO_ZOOM == parser.getServiceType()) { - service_handle = ServiceFactory::create("AutoZoom"); + service_handle = ServiceFactory::instance().create("AutoZoom"); if (!service_handle) throw InvalidOperation("Failed to create AutoZoom service."); -- 2.34.1