diff --git a/ReactCommon/fabric/uimanager/ContextContainer.h b/ReactCommon/fabric/uimanager/ContextContainer.h index 30e055c85..cb8209bb2 100644 --- a/ReactCommon/fabric/uimanager/ContextContainer.h +++ b/ReactCommon/fabric/uimanager/ContextContainer.h @@ -24,20 +24,37 @@ using SharedContextContainer = std::shared_ptr; class ContextContainer final { public: + /* + * Registers an instance of the particular type `T` in the container. + * If `key` parameter is specified, the instance is registered + * by `{type, key}` pair. + */ template - void registerInstance(std::shared_ptr instance) { + void registerInstance(std::shared_ptr instance, const std::string &key = "") { std::lock_guard lock(mutex_); - instances_.insert({std::type_index(typeid(T)), instance}); + instances_.insert({ + {std::type_index(typeid(T)), key}, + instance + }); } + /* + * Returns a previously registered instance of the particular type `T`. + * If `key` parameter is specified, the lookup will be performed + * by {type, key} pair. + */ template - std::shared_ptr getInstance() const { + std::shared_ptr getInstance(const std::string &key = "") const { std::lock_guard lock(mutex_); - return std::static_pointer_cast(instances_.at(std::type_index(typeid(T)))); + return std::static_pointer_cast(instances_.at({std::type_index(typeid(T)), key})); } private: - std::unordered_map> instances_; + std::unordered_map< + std::pair, + std::shared_ptr + > instances_; + mutable std::mutex mutex_; };