cnstream_module_pri.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*************************************************************************
  2. * Copyright (C) [2021] by Cambricon, Inc. All rights reserved
  3. *
  4. * This source code is licensed under the Apache-2.0 license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * A part of this source code is referenced from Nebula project.
  8. * https://github.com/Bwar/Nebula/blob/master/src/actor/DynamicCreator.hpp
  9. * https://github.com/Bwar/Nebula/blob/master/src/actor/ActorFactory.hpp
  10. *
  11. * Copyright (C) Bwar.
  12. *
  13. * This source code is licensed under the Apache-2.0 license found in the
  14. * LICENSE file in the root directory of this source tree.
  15. *
  16. *************************************************************************/
  17. #ifndef CNSTREAM_MODULE_PRI_HPP_
  18. #define CNSTREAM_MODULE_PRI_HPP_
  19. /**
  20. * @file cnstream_module.hpp
  21. *
  22. * This file contains a declaration of the Module class and the ModuleFactory class.
  23. */
  24. #include <cxxabi.h>
  25. #include <unistd.h>
  26. #include <atomic>
  27. #include <bitset>
  28. #include <functional>
  29. #include <list>
  30. #include <memory>
  31. #include <string>
  32. #include <thread>
  33. #include <typeinfo>
  34. #include <unordered_map>
  35. #include <utility>
  36. #include <vector>
  37. namespace cnstream {
  38. /**
  39. * @class ModuleFactory
  40. *
  41. * @brief Provides functions to create instances with the ``ModuleClassName``and ``moduleName`` parameters.
  42. *
  43. * @note ModuleCreator, ModuleFactory, and ModuleCreatorWorker:
  44. * Implements reflection mechanism to create a module instance dynamically with the ``ModuleClassName`` and
  45. * ``moduleName`` parameters. See ActorFactory&DynamicCreator in https://github.com/Bwar/Nebula.
  46. */
  47. class ModuleFactory {
  48. public:
  49. /**
  50. * @brief Creates or gets the instance of the ModuleFactory class.
  51. *
  52. * @param None.
  53. *
  54. * @return Returns the instance of the ModuleFactory class.
  55. */
  56. static ModuleFactory *Instance() {
  57. if (nullptr == factory_) {
  58. factory_ = new (std::nothrow) ModuleFactory();
  59. LOGF_IF(CORE, nullptr == factory_) << "ModuleFactory::Instance() new ModuleFactory failed.";
  60. }
  61. return (factory_);
  62. }
  63. /**
  64. * @brief Destructor. A destructor to destruct ModuleFactory.
  65. *
  66. * @param None.
  67. *
  68. * @return None.
  69. */
  70. virtual ~ModuleFactory() {}
  71. /**
  72. * @brief Registers the pair of ``ModuleClassName`` and ``CreateFunction`` to module factory.
  73. *
  74. * @param[in] strTypeName The module class name.
  75. * @param[in] pFunc The ``CreateFunction`` of a Module object that has a parameter ``moduleName``.
  76. *
  77. * @return Returns true if this function has run successfully.
  78. */
  79. bool Regist(const std::string &strTypeName, std::function<Module *(const std::string &)> pFunc) {
  80. if (nullptr == pFunc) {
  81. return (false);
  82. }
  83. bool ret = map_.insert(std::make_pair(strTypeName, pFunc)).second;
  84. return ret;
  85. }
  86. /**
  87. * @brief Creates a module instance with ``ModuleClassName`` and ``moduleName``.
  88. *
  89. * @param[in] strTypeName The module class name.
  90. * @param[in] name The module name which is passed to ``CreateFunction`` to identify a module.
  91. *
  92. * @return Returns the module instance if this function has run successfully. Otherwise, returns nullptr if failed.
  93. */
  94. Module *Create(const std::string &strTypeName, const std::string &name) {
  95. auto iter = map_.find(strTypeName);
  96. if (iter == map_.end()) {
  97. return (nullptr);
  98. } else {
  99. return (iter->second(name));
  100. }
  101. }
  102. /**
  103. * @brief Gets all registered modules.
  104. *
  105. * @param None.
  106. *
  107. * @return All registered module class names.
  108. */
  109. std::vector<std::string> GetRegisted() {
  110. std::vector<std::string> registed_modules;
  111. for (auto &it : map_) {
  112. registed_modules.push_back(it.first);
  113. }
  114. return registed_modules;
  115. }
  116. private:
  117. ModuleFactory() {}
  118. static ModuleFactory *factory_;
  119. std::unordered_map<std::string, std::function<Module *(const std::string &)>> map_;
  120. };
  121. /**
  122. * @class ModuleCreatorWorker
  123. *
  124. * @brief ModuleCreatorWorker is class as a dynamic-creator helper.
  125. *
  126. * @note ModuleCreator, ModuleFactory, and ModuleCreatorWorker:
  127. * Implements reflection mechanism to create a module instance dynamically with the ``ModuleClassName`` and
  128. * ``moduleName`` parameters. See ActorFactory&DynamicCreator in https://github.com/Bwar/Nebula.
  129. */
  130. class ModuleCreatorWorker {
  131. public:
  132. /**
  133. * @brief Creates a module instance with ``ModuleClassName`` and ``moduleName``.
  134. *
  135. * @param[in] strTypeName The module class name.
  136. * @param[in] name The module name.
  137. *
  138. * @return Returns the module instance if the module instance is created successfully. Returns nullptr if failed.
  139. * @see ModuleFactory::Create
  140. */
  141. Module *Create(const std::string &strTypeName, const std::string &name) {
  142. Module *p = ModuleFactory::Instance()->Create(strTypeName, name);
  143. return (p);
  144. }
  145. };
  146. /**
  147. * @class ModuleCreator
  148. *
  149. * @brief A concrete ModuleClass needs to inherit ModuleCreator to enable reflection mechanism.
  150. * ModuleCreator provides ``CreateFunction``, and registers ``ModuleClassName`` and ``CreateFunction`` to
  151. * ModuleFactory().
  152. *
  153. * @note ModuleCreator, ModuleFactory, and ModuleCreatorWorker:
  154. * Implements reflection mechanism to create a module instance dynamically with the ``ModuleClassName`` and
  155. * ``moduleName`` parameters. See ActorFactory&DynamicCreator in https://github.com/Bwar/Nebula.
  156. */
  157. template <typename T>
  158. class ModuleCreator {
  159. public:
  160. struct Register {
  161. Register() {
  162. char *szDemangleName = nullptr;
  163. std::string strTypeName;
  164. #ifdef __GNUC__
  165. szDemangleName = abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, nullptr);
  166. #else
  167. // in this format?: szDemangleName = typeid(T).name();
  168. szDemangleName = abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, nullptr);
  169. #endif
  170. if (nullptr != szDemangleName) {
  171. strTypeName = szDemangleName;
  172. free(szDemangleName);
  173. }
  174. ModuleFactory::Instance()->Regist(strTypeName, CreateObject);
  175. }
  176. inline void do_nothing() const {}
  177. };
  178. /**
  179. * @brief Constructor. A constructor to construct module creator.
  180. *
  181. * @param None.
  182. *
  183. * @return None.
  184. */
  185. ModuleCreator() { register_.do_nothing(); }
  186. /**
  187. * @brief Destructor. A destructor to destruct module creator.
  188. *
  189. * @param None.
  190. *
  191. * @return None.
  192. */
  193. virtual ~ModuleCreator() { register_.do_nothing(); }
  194. /**
  195. * @brief Creates an instance of template (T) with specified instance name. This is a template function.
  196. *
  197. * @param[in] name The name of the instance.
  198. *
  199. * @return Returns the instance of template (T).
  200. */
  201. static T *CreateObject(const std::string &name) { return new (std::nothrow) T(name); }
  202. static Register register_;
  203. };
  204. template <typename T>
  205. typename ModuleCreator<T>::Register ModuleCreator<T>::register_;
  206. /**
  207. * @brief ModuleId&StreamIdx manager for pipeline. Allocates and deallocates id for Pipeline modules & streams.
  208. */
  209. class IdxManager {
  210. public:
  211. IdxManager() = default;
  212. IdxManager(const IdxManager&) = delete;
  213. IdxManager& operator=(const IdxManager&) = delete;
  214. uint32_t GetStreamIndex(const std::string& stream_id);
  215. void ReturnStreamIndex(const std::string& stream_id);
  216. size_t GetModuleIdx();
  217. void ReturnModuleIdx(size_t id_);
  218. private:
  219. std::mutex id_lock;
  220. std::unordered_map<std::string, uint32_t> stream_idx_map;
  221. std::bitset<MAX_STREAM_NUM> stream_bitset;
  222. uint64_t module_id_mask_ = 0;
  223. }; // class IdxManager
  224. } // namespace cnstream
  225. #endif // CNSTREAM_MODULE_PRI_HPP_