Fabric: ContextContainer is now able to store any copyable values
Summary: @public Previously, ContextContainer could store only `shared_ptr`s, but now it wraps all values in own `shared_ptr` container. I wish we can use `unique_ptr` here, but apparently we cannot because `unique_ptr` does not support type-erasure (`std::unique_ptr<void>` is illigal). Becasue ContextContainer is not supposed to be used in hot paths, the performance aspect of that does not actually matter. Reviewed By: mdvacca Differential Revision: D8853446 fbshipit-source-id: e5d0a5595fe44c59f1395d6ffccf9d3fed923c83
This commit is contained in:
parent
07a4a959a7
commit
9395485822
|
@ -24,7 +24,7 @@ class ImageComponentDescriptor final:
|
|||
public:
|
||||
ImageComponentDescriptor(SharedEventDispatcher eventDispatcher, const SharedContextContainer &contextContainer):
|
||||
ConcreteComponentDescriptor(eventDispatcher),
|
||||
imageManager_(contextContainer->getInstance<ImageManager>()) {}
|
||||
imageManager_(contextContainer->getInstance<SharedImageManager>()) {}
|
||||
|
||||
void adopt(UnsharedShadowNode shadowNode) const override {
|
||||
ConcreteComponentDescriptor::adopt(shadowNode);
|
||||
|
|
|
@ -20,6 +20,7 @@ using SharedContextContainer = std::shared_ptr<ContextContainer>;
|
|||
|
||||
/*
|
||||
* General purpose dependecy injection container.
|
||||
* Instance types must be copyable.
|
||||
*/
|
||||
class ContextContainer final {
|
||||
|
||||
|
@ -30,11 +31,12 @@ public:
|
|||
* by `{type, key}` pair.
|
||||
*/
|
||||
template<typename T>
|
||||
void registerInstance(std::shared_ptr<T> instance, const std::string &key = "") {
|
||||
void registerInstance(const T &instance, const std::string &key = "") {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
instances_.insert({
|
||||
{std::type_index(typeid(T)), key},
|
||||
instance
|
||||
std::make_shared<T>(instance)
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -44,9 +46,10 @@ public:
|
|||
* by {type, key} pair.
|
||||
*/
|
||||
template<typename T>
|
||||
std::shared_ptr<T> getInstance(const std::string &key = "") const {
|
||||
T getInstance(const std::string &key = "") const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return std::static_pointer_cast<T>(instances_.at({std::type_index(typeid(T)), key}));
|
||||
|
||||
return *std::static_pointer_cast<T>(instances_.at({std::type_index(typeid(T)), key}));
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
Loading…
Reference in New Issue