123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- #ifndef CNSTREAM_MODULE_PRI_HPP_
- #define CNSTREAM_MODULE_PRI_HPP_
- #include <cxxabi.h>
- #include <unistd.h>
- #include <atomic>
- #include <bitset>
- #include <functional>
- #include <list>
- #include <memory>
- #include <string>
- #include <thread>
- #include <typeinfo>
- #include <unordered_map>
- #include <utility>
- #include <vector>
- namespace cnstream {
- class ModuleFactory {
- public:
-
- static ModuleFactory *Instance() {
- if (nullptr == factory_) {
- factory_ = new (std::nothrow) ModuleFactory();
- LOGF_IF(CORE, nullptr == factory_) << "ModuleFactory::Instance() new ModuleFactory failed.";
- }
- return (factory_);
- }
-
- virtual ~ModuleFactory() {}
-
- bool Regist(const std::string &strTypeName, std::function<Module *(const std::string &)> pFunc) {
- if (nullptr == pFunc) {
- return (false);
- }
- bool ret = map_.insert(std::make_pair(strTypeName, pFunc)).second;
- return ret;
- }
-
- Module *Create(const std::string &strTypeName, const std::string &name) {
- auto iter = map_.find(strTypeName);
- if (iter == map_.end()) {
- return (nullptr);
- } else {
- return (iter->second(name));
- }
- }
-
- std::vector<std::string> GetRegisted() {
- std::vector<std::string> registed_modules;
- for (auto &it : map_) {
- registed_modules.push_back(it.first);
- }
- return registed_modules;
- }
- private:
- ModuleFactory() {}
- static ModuleFactory *factory_;
- std::unordered_map<std::string, std::function<Module *(const std::string &)>> map_;
- };
- class ModuleCreatorWorker {
- public:
-
- Module *Create(const std::string &strTypeName, const std::string &name) {
- Module *p = ModuleFactory::Instance()->Create(strTypeName, name);
- return (p);
- }
- };
- template <typename T>
- class ModuleCreator {
- public:
- struct Register {
- Register() {
- char *szDemangleName = nullptr;
- std::string strTypeName;
- #ifdef __GNUC__
- szDemangleName = abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, nullptr);
- #else
-
- szDemangleName = abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, nullptr);
- #endif
- if (nullptr != szDemangleName) {
- strTypeName = szDemangleName;
- free(szDemangleName);
- }
- ModuleFactory::Instance()->Regist(strTypeName, CreateObject);
- }
- inline void do_nothing() const {}
- };
-
- ModuleCreator() { register_.do_nothing(); }
-
- virtual ~ModuleCreator() { register_.do_nothing(); }
-
- static T *CreateObject(const std::string &name) { return new (std::nothrow) T(name); }
- static Register register_;
- };
- template <typename T>
- typename ModuleCreator<T>::Register ModuleCreator<T>::register_;
- class IdxManager {
- public:
- IdxManager() = default;
- IdxManager(const IdxManager&) = delete;
- IdxManager& operator=(const IdxManager&) = delete;
- uint32_t GetStreamIndex(const std::string& stream_id);
- void ReturnStreamIndex(const std::string& stream_id);
- size_t GetModuleIdx();
- void ReturnModuleIdx(size_t id_);
- private:
- std::mutex id_lock;
- std::unordered_map<std::string, uint32_t> stream_idx_map;
- std::bitset<MAX_STREAM_NUM> stream_bitset;
- uint64_t module_id_mask_ = 0;
- };
- }
- #endif
|