service: code refactoring to ServiceFactory class 00/312900/5
authorInki Dae <inki.dae@samsung.com>
Mon, 17 Jun 2024 06:16:47 +0000 (15:16 +0900)
committerInki Dae <inki.dae@samsung.com>
Wed, 19 Jun 2024 00:19:28 +0000 (09:19 +0900)
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 <inki.dae@samsung.com>
services/auto_zoom/include/AutoZoom.h
services/auto_zoom/src/AutoZoom.cpp
services/common/include/ServiceFactory.h
services/common/src/ServiceFactory.cpp [deleted file]
services/singleo_native_capi.cpp

index ccf8b25fd8efc2f89f1b01176a6b545c1f336007..1defe0dcb982d8782d21e65171dd8b6791ef26c5 100644 (file)
@@ -38,6 +38,7 @@ namespace autozoom
 class AutoZoom : public IService, public input::IInputObserver
 {
 private:
+       static bool _registered;
        std::unique_ptr<TaskManager> _taskManager;
        std::unique_ptr<singleo::input::IInputService> _input_service;
        std::unique_ptr<IPostprocessor> _postprocessor;
index ce35e4010d2f4bd1209b73053610b65e73450b72..13430ad0c6b5dab13f7c099fee6e59c10bbbfef1 100644 (file)
@@ -39,7 +39,7 @@ namespace services
 {
 namespace autozoom
 {
-REGISTER_SERVICE(AutoZoom)
+bool AutoZoom::_registered = registerService<AutoZoom>("AutoZoom");
 
 void BridgeNodeCallback(INode *node)
 {
index 77216ae1df5a369c84d2a3f62ab15fe23b8ed81e..62f868556ea896fc6506f2951fd2af68019649ab 100644 (file)
@@ -20,6 +20,7 @@
 #include <map>
 #include <vector>
 #include <stdexcept>
+#include <unordered_map>
 #include "IService.h"
 #include "InputTypes.h"
 
@@ -30,24 +31,38 @@ namespace services
 class ServiceFactory
 {
 public:
-       using createFunc = IService *(*) ();
-       static std::map<std::string, createFunc> __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<std::string, createFunc> __service_table;
 };
 
-#define REGISTER_SERVICE(name)                                  \
-       const bool is##nameRegistered = [] {                        \
-               ServiceFactory::__service_table[#name] = &name::create; \
-               return true;                                            \
-       }();
+template<typename ServiceType> bool registerService(const std::string &name)
+{
+       ServiceFactory::instance().registerService(name, []() { return dynamic_cast<IService *>(new ServiceType()); });
+
+       return true;
+}
 
 }
 }
diff --git a/services/common/src/ServiceFactory.cpp b/services/common/src/ServiceFactory.cpp
deleted file mode 100644 (file)
index 85e6af7..0000000
+++ /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<string, ServiceFactory::createFunc> ServiceFactory::__service_table;
-
-}
-}
\ No newline at end of file
index d353842353cd1cb4174af433e50fcb06276f81c8..6774a0efaef4dd22774dc429fd965773e098b629 100644 (file)
@@ -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.");