mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 01:25:39 +00:00
0532e01d69
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
46 lines
1.0 KiB
C++
46 lines
1.0 KiB
C++
// 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:
|
|
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});
|
|
}
|
|
|
|
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<std::type_index, std::shared_ptr<void>> instances_;
|
|
mutable std::mutex mutex_;
|
|
};
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|