• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_AUTO_REGISTRATION_FACTORY_H_
17 #define MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_AUTO_REGISTRATION_FACTORY_H_
18 
19 #include <unordered_map>
20 
21 namespace mindspore::lite {
22 template <typename KeyType, typename CreatorType>
23 class AutoRegistrationFactory {
24  public:
25   struct AutoRegister {
AutoRegisterAutoRegister26     AutoRegister(KeyType k, CreatorType creator) {
27       AutoRegistrationFactory<KeyType, CreatorType>::Get().Insert(k, creator);
28     }
29   };
30   static AutoRegistrationFactory<KeyType, CreatorType> &Get();
HasKey(KeyType k)31   bool HasKey(KeyType k) const { return key2creator_.find(k) != key2creator_.end(); }
GetCreator(KeyType k)32   CreatorType GetCreator(KeyType k) { return key2creator_[k]; }
33 
34  private:
Insert(KeyType k,CreatorType creator)35   bool Insert(KeyType k, CreatorType creator) {
36     if (HasKey(k)) {
37       return false;
38     }
39     return key2creator_.emplace(k, creator).second;
40   }
41   std::unordered_map<KeyType, CreatorType> key2creator_;
42 };
43 
44 #define AUTO_REGISTRATION_FACTORY_JOIN(a, b) a##b
45 
46 #define AUTO_REGISTRATION_FACTORY_UNIQUE_NAME_JOIN(a, b) AUTO_REGISTRATION_FACTORY_JOIN(a, b)
47 
48 #define AUTO_REGISTRATION_FACTORY_UNIQUE_NAME AUTO_REGISTRATION_FACTORY_UNIQUE_NAME_JOIN(g_, __COUNTER__)
49 
50 #define REGISTER_CLASS_CREATOR(KeyType, k, CreatorType, creator) \
51   static AutoRegistrationFactory<KeyType, CreatorType>::AutoRegister AUTO_REGISTRATION_FACTORY_UNIQUE_NAME(k, creator);
52 }  // namespace mindspore::lite
53 
54 #endif  // MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_AUTO_REGISTRATION_FACTORY_H_
55