#pragma once #ifndef META_RUNTIME_META_REGISTRY_H #define META_RUNTIME_META_REGISTRY_H #include "meta_object.h" #include #include namespace meta { /** * Registry **/ template class MetaRegistry { public: static MetaRegistry *Instance(); static bool Register(const std::string &obj_name, std::function creator); static bool Unregister(const std::string &obj_name); private: bool RegisterImpl(const std::string &obj_name, std::function creator); bool UnregisterImpl(const std::string &obj_name); std::unordered_map> creators_; }; template MetaRegistry * MetaRegistry::Instance() { static MetaRegistry instance; return &instance; } template bool MetaRegistry::Register(const std::string &obj_name, std::function creator) { return Instance()->RegisterImpl(obj_name, creator); } template bool MetaRegistry::Unregister(const std::string &obj_name) { return Instance()->UnregisterImpl(obj_name); } template bool MetaRegistry::RegisterImpl(const std::string &obj_name, std::function creator) { return creators_.insert({obj_name, creator}).second; } template bool MetaRegistry::UnregisterImpl(const std::string &obj_name) { return creators_.erase(obj_name) > 0; } }// namespace meta #endif// META_RUNTIME_META_REGISTRY_H