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;
{
namespace autozoom
{
-REGISTER_SERVICE(AutoZoom)
+bool AutoZoom::_registered = registerService<AutoZoom>("AutoZoom");
void BridgeNodeCallback(INode *node)
{
#include <map>
#include <vector>
#include <stdexcept>
+#include <unordered_map>
#include "IService.h"
#include "InputTypes.h"
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;
+}
}
}
+++ /dev/null
-/**
- * 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
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.");