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:
parent
7a7f9601bc
commit
0532e01d69
|
@ -49,7 +49,7 @@ private:
|
||||||
SharedContextContainer contextContainer = std::make_shared<ContextContainer>();
|
SharedContextContainer contextContainer = std::make_shared<ContextContainer>();
|
||||||
|
|
||||||
void *imageLoader = (__bridge void *)[[RCTBridge currentBridge] imageLoader];
|
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 = std::make_shared<Scheduler>(contextContainer);
|
||||||
_scheduler->setDelegate(_delegateProxy.get());
|
_scheduler->setDelegate(_delegateProxy.get());
|
||||||
|
|
|
@ -24,7 +24,7 @@ class ImageComponentDescriptor final:
|
||||||
public:
|
public:
|
||||||
ImageComponentDescriptor(SharedEventDispatcher eventDispatcher, const SharedContextContainer &contextContainer):
|
ImageComponentDescriptor(SharedEventDispatcher eventDispatcher, const SharedContextContainer &contextContainer):
|
||||||
ConcreteComponentDescriptor(eventDispatcher),
|
ConcreteComponentDescriptor(eventDispatcher),
|
||||||
imageManager_(std::static_pointer_cast<ImageManager>(contextContainer->at(typeid(ImageManager)))) {}
|
imageManager_(contextContainer->getInstance<ImageManager>()) {}
|
||||||
|
|
||||||
void adopt(UnsharedShadowNode shadowNode) const override {
|
void adopt(UnsharedShadowNode shadowNode) const override {
|
||||||
ConcreteComponentDescriptor::adopt(shadowNode);
|
ConcreteComponentDescriptor::adopt(shadowNode);
|
||||||
|
|
|
@ -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
|
|
|
@ -24,15 +24,20 @@ using SharedContextContainer = std::shared_ptr<ContextContainer>;
|
||||||
class ContextContainer final {
|
class ContextContainer final {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using ClassHandle = std::type_index;
|
template<typename T>
|
||||||
using SharedInstance = std::shared_ptr<void>;
|
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);
|
template<typename T>
|
||||||
|
std::shared_ptr<T> getInstance() const {
|
||||||
const SharedInstance &at(const ClassHandle &handle) const;
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
return std::static_pointer_cast<T>(instances_.at(std::type_index(typeid(T))));
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<ClassHandle, SharedInstance> instances_;
|
std::unordered_map<std::type_index, std::shared_ptr<void>> instances_;
|
||||||
mutable std::mutex mutex_;
|
mutable std::mutex mutex_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue