forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fabric: Introducing
ContextContainer
Summary: @public `ContextContainer` is general purpose DI container for Fabric. We need this to communicate some enviroment-specific and/or platform-specific modules down to cross-platform C++ code. The first one will be ImageManager. Soon. Reviewed By: fkgozali Differential Revision: D8475636 fbshipit-source-id: 0afc65063f818d0bab736cd2c55c6fdd21b629ac
- Loading branch information
1 parent
0a20f47
commit c674303
Showing
7 changed files
with
79 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <mutex> | ||
#include <typeindex> | ||
#include <typeinfo> | ||
#include <unordered_map> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
class ContextContainer; | ||
|
||
using SharedContextContainer = std::shared_ptr<ContextContainer>; | ||
|
||
/* | ||
* General purpose dependecy injection container. | ||
*/ | ||
class ContextContainer final { | ||
|
||
public: | ||
using ClassHandle = std::type_index; | ||
using SharedInstance = std::shared_ptr<void>; | ||
|
||
void registerInstance(const ClassHandle &handle, SharedInstance instance); | ||
|
||
const SharedInstance &at(const ClassHandle &handle) const; | ||
|
||
private: | ||
std::unordered_map<ClassHandle, SharedInstance> instances_; | ||
mutable std::mutex mutex_; | ||
}; | ||
|
||
} // namespace react | ||
} // namespace facebook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters