Fabric: Enhancements in ContextContainer

Summary:
@public
Everything is better with C++ templates.
In this cases templates allow us to remove additional parameters and casts on the callsite.

Reviewed By: mdvacca

Differential Revision: D8754523

fbshipit-source-id: 2340b2cd96ab0a60d54d9aa30dea3c072b951a8a
This commit is contained in:
Valentin Shergin 2018-07-15 16:46:22 -07:00 committed by Facebook Github Bot
parent 7a7f9601bc
commit 0532e01d69
4 changed files with 13 additions and 30 deletions

View File

@ -49,7 +49,7 @@ private:
SharedContextContainer contextContainer = std::make_shared<ContextContainer>();
void *imageLoader = (__bridge void *)[[RCTBridge currentBridge] imageLoader];
contextContainer->registerInstance(typeid(ImageManager), std::make_shared<ImageManager>(imageLoader));
contextContainer->registerInstance(std::make_shared<ImageManager>(imageLoader));
_scheduler = std::make_shared<Scheduler>(contextContainer);
_scheduler->setDelegate(_delegateProxy.get());

View File

@ -24,7 +24,7 @@ class ImageComponentDescriptor final:
public:
ImageComponentDescriptor(SharedEventDispatcher eventDispatcher, const SharedContextContainer &contextContainer):
ConcreteComponentDescriptor(eventDispatcher),
imageManager_(std::static_pointer_cast<ImageManager>(contextContainer->at(typeid(ImageManager)))) {}
imageManager_(contextContainer->getInstance<ImageManager>()) {}
void adopt(UnsharedShadowNode shadowNode) const override {
ConcreteComponentDescriptor::adopt(shadowNode);

View File

@ -1,22 +0,0 @@
// Copyright (c) 2004-present, Facebook, Inc.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#include "ContextContainer.h"
namespace facebook {
namespace react {
void ContextContainer::registerInstance(const ClassHandle &handle, SharedInstance instance) {
std::lock_guard<std::mutex> lock(mutex_);
instances_.insert({handle, instance});
}
const ContextContainer::SharedInstance &ContextContainer::at(const ClassHandle &handle) const {
std::lock_guard<std::mutex> lock(mutex_);
return instances_.at(handle);
}
} // namespace react
} // namespace facebook

View File

@ -24,15 +24,20 @@ using SharedContextContainer = std::shared_ptr<ContextContainer>;
class ContextContainer final {
public:
using ClassHandle = std::type_index;
using SharedInstance = std::shared_ptr<void>;
template<typename T>
void registerInstance(std::shared_ptr<T> instance) {
std::lock_guard<std::mutex> lock(mutex_);
instances_.insert({std::type_index(typeid(T)), instance});
}
void registerInstance(const ClassHandle &handle, SharedInstance instance);
const SharedInstance &at(const ClassHandle &handle) const;
template<typename T>
std::shared_ptr<T> getInstance() const {
std::lock_guard<std::mutex> lock(mutex_);
return std::static_pointer_cast<T>(instances_.at(std::type_index(typeid(T))));
}
private:
std::unordered_map<ClassHandle, SharedInstance> instances_;
std::unordered_map<std::type_index, std::shared_ptr<void>> instances_;
mutable std::mutex mutex_;
};