You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

module_registry.h 2.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Copyright 2019 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 PREDICT_COMMON_MODULE_REGISTRY_H_
  17. #define PREDICT_COMMON_MODULE_REGISTRY_H_
  18. #include <memory>
  19. #include <string>
  20. #include <unordered_map>
  21. #include "common/mslog.h"
  22. #define MSPREDICT_API __attribute__((visibility("default")))
  23. namespace mindspore {
  24. namespace predict {
  25. class ModuleBase {
  26. public:
  27. virtual ~ModuleBase() = default;
  28. };
  29. template <typename T>
  30. class Module;
  31. class ModuleRegistry {
  32. public:
  33. ModuleRegistry() = default;
  34. virtual ~ModuleRegistry() = default;
  35. template <class T>
  36. bool Register(const std::string &name, const T &t) {
  37. modules[name] = &t;
  38. return true;
  39. }
  40. template <class T>
  41. std::shared_ptr<T> Create(const std::string &name) {
  42. auto it = modules.find(name);
  43. if (it == modules.end()) {
  44. return nullptr;
  45. }
  46. auto *module = (Module<T> *)it->second;
  47. if (module == nullptr) {
  48. return nullptr;
  49. } else {
  50. return module->Create();
  51. }
  52. }
  53. template <class T>
  54. T *GetInstance(const std::string &name) {
  55. auto it = modules.find(name);
  56. if (it == modules.end()) {
  57. return nullptr;
  58. }
  59. auto *module = (Module<T> *)it->second;
  60. if (module == nullptr) {
  61. return nullptr;
  62. } else {
  63. return module->GetInstance();
  64. }
  65. }
  66. protected:
  67. std::unordered_map<std::string, const ModuleBase *> modules;
  68. };
  69. ModuleRegistry *GetRegistryInstance() MSPREDICT_API;
  70. template <class T>
  71. class ModuleRegistrar {
  72. public:
  73. ModuleRegistrar(const std::string &name, const T &module) {
  74. auto registryInstance = GetRegistryInstance();
  75. if (registryInstance == nullptr) {
  76. MS_LOGW("registryInstance is nullptr.");
  77. } else {
  78. registryInstance->Register(name, module);
  79. }
  80. }
  81. };
  82. } // namespace predict
  83. } // namespace mindspore
  84. #endif // PREDICT_COMMON_MODULE_REGISTRY_H_