1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef MAPLE_UTIL_INCLUDE_FACTORY_H
17 #define MAPLE_UTIL_INCLUDE_FACTORY_H
18 #include <map>
19 #include <memory>
20 #include <functional>
21 #include <mutex>
22 #include "thread_env.h"
23
24 namespace maple {
25 template <typename TKey, typename TObject, typename... TArgs>
26 class ObjectFactory final {
27 public:
28 using key_type = TKey;
29 using creator_type = std::function<std::unique_ptr<TObject>(TArgs...)>;
30
Register(const key_type & key,creator_type func)31 void Register(const key_type &key, creator_type func)
32 {
33 if (creator.find(key) == creator.end()) {
34 creator[key] = func;
35 }
36 }
37
Create(const key_type & key,TArgs...args)38 std::unique_ptr<TObject> Create(const key_type &key, TArgs... args) const
39 {
40 auto it = creator.find(key);
41 return it == creator.end() ? std::unique_ptr<TObject>() : (it->second)(std::forward<TArgs>(args)...);
42 }
43
44 template <typename TObjectImpl>
DefaultCreator(TArgs...args)45 static std::unique_ptr<TObject> DefaultCreator(TArgs... args)
46 {
47 return std::make_unique<TObjectImpl>(std::forward<TArgs>(args)...);
48 }
49
ins()50 static ObjectFactory &ins()
51 {
52 static ObjectFactory factory;
53 return factory;
54 }
55
56 ObjectFactory(const ObjectFactory &) = delete;
57 ObjectFactory &operator=(const ObjectFactory &) = delete;
58 ObjectFactory(const ObjectFactory &&) = delete;
59 ObjectFactory &operator=(const ObjectFactory &&) = delete;
60
61 private:
62 ObjectFactory() = default;
63 ~ObjectFactory() = default;
64
65 private:
66 using CreatorHolder = std::map<key_type, creator_type>;
67 CreatorHolder creator;
68 };
69
70 template <typename TFactory, typename TFactory::key_type Key, typename TObjectImpl>
RegisterFactoryObject()71 inline void RegisterFactoryObject()
72 {
73 TFactory::ins().Register(Key, TFactory::template DefaultCreator<TObjectImpl>);
74 }
75
76 template <typename TFactory, typename... TArgs>
CreateProductObject(const typename TFactory::key_type & key,TArgs &&...args)77 inline auto CreateProductObject(const typename TFactory::key_type &key, TArgs &&... args)
78 {
79 return TFactory::ins().Create(key, std::forward<TArgs>(args)...);
80 }
81
82 template <typename TKey, typename TRet, typename... TArgs>
83 class FunctionFactory final {
84 public:
85 using key_type = TKey;
86 using creator_type = std::function<TRet(TArgs...)>;
87
Register(const key_type & key,creator_type func)88 void Register(const key_type &key, creator_type func)
89 {
90 static std::mutex mtx;
91 ParallelGuard guard(mtx, ThreadEnv::IsMeParallel());
92 if (creator.find(key) == creator.end()) {
93 creator[key] = func;
94 }
95 }
96
Create(const key_type & key)97 creator_type Create(const key_type &key) const
98 {
99 static std::mutex mtx;
100 ParallelGuard guard(mtx, ThreadEnv::IsMeParallel());
101 auto it = creator.find(key);
102 return it == creator.end() ? nullptr : it->second;
103 }
104
ins()105 static FunctionFactory &ins()
106 {
107 static FunctionFactory factory;
108 return factory;
109 }
110
111 FunctionFactory(const FunctionFactory &) = delete;
112 FunctionFactory &operator=(const FunctionFactory &) = delete;
113 FunctionFactory(const FunctionFactory &&) = delete;
114 FunctionFactory &operator=(const FunctionFactory &&) = delete;
115
116 private:
117 FunctionFactory() = default;
118 ~FunctionFactory() = default;
119
120 private:
121 using CreatorHolder = std::map<key_type, creator_type>;
122 CreatorHolder creator;
123 };
124
125 template <typename TFactory>
RegisterFactoryFunction(const typename TFactory::key_type & key,typename TFactory::creator_type func)126 inline void RegisterFactoryFunction(const typename TFactory::key_type &key, typename TFactory::creator_type func)
127 {
128 TFactory::ins().Register(key, func);
129 }
130
131 template <typename TFactory>
CreateProductFunction(const typename TFactory::key_type & key)132 inline auto CreateProductFunction(const typename TFactory::key_type &key)
133 {
134 return TFactory::ins().Create(key);
135 }
136 } // namespace maple
137
138 #endif // MAPLE_UTIL_INCLUDE_FACTORY_H
139