Skip to content

Commit 9395485

Browse files
sherginfacebook-github-bot
authored andcommitted
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
1 parent 07a4a95 commit 9395485

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

ReactCommon/fabric/components/image/ImageComponentDescriptor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ImageComponentDescriptor final:
2424
public:
2525
ImageComponentDescriptor(SharedEventDispatcher eventDispatcher, const SharedContextContainer &contextContainer):
2626
ConcreteComponentDescriptor(eventDispatcher),
27-
imageManager_(contextContainer->getInstance<ImageManager>()) {}
27+
imageManager_(contextContainer->getInstance<SharedImageManager>()) {}
2828

2929
void adopt(UnsharedShadowNode shadowNode) const override {
3030
ConcreteComponentDescriptor::adopt(shadowNode);

ReactCommon/fabric/uimanager/ContextContainer.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ using SharedContextContainer = std::shared_ptr<ContextContainer>;
2020

2121
/*
2222
* General purpose dependecy injection container.
23+
* Instance types must be copyable.
2324
*/
2425
class ContextContainer final {
2526

@@ -30,11 +31,12 @@ class ContextContainer final {
3031
* by `{type, key}` pair.
3132
*/
3233
template<typename T>
33-
void registerInstance(std::shared_ptr<T> instance, const std::string &key = "") {
34+
void registerInstance(const T &instance, const std::string &key = "") {
3435
std::lock_guard<std::mutex> lock(mutex_);
36+
3537
instances_.insert({
3638
{std::type_index(typeid(T)), key},
37-
instance
39+
std::make_shared<T>(instance)
3840
});
3941
}
4042

@@ -44,9 +46,10 @@ class ContextContainer final {
4446
* by {type, key} pair.
4547
*/
4648
template<typename T>
47-
std::shared_ptr<T> getInstance(const std::string &key = "") const {
49+
T getInstance(const std::string &key = "") const {
4850
std::lock_guard<std::mutex> lock(mutex_);
49-
return std::static_pointer_cast<T>(instances_.at({std::type_index(typeid(T)), key}));
51+
52+
return *std::static_pointer_cast<T>(instances_.at({std::type_index(typeid(T)), key}));
5053
}
5154

5255
private:

0 commit comments

Comments
 (0)