Skip to content

Commit

Permalink
fix: Add nullptr check to HybridObjectRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Aug 8, 2024
1 parent 7a914cf commit ec910a7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ std::unordered_map<std::string, HybridObjectRegistry::HybridObjectConstructorFn>
return _constructorsMap;
}

void HybridObjectRegistry::registerHybridObjectConstructor(std::string hybridObjectName, HybridObjectConstructorFn&& constructorFn) {
void HybridObjectRegistry::registerHybridObjectConstructor(const std::string& hybridObjectName, HybridObjectConstructorFn&& constructorFn) {
Logger::log(TAG, "Registering HybridObject \"%s\"...", hybridObjectName);
auto& map = HybridObjectRegistry::getRegistry();
#if DEBUG
if (map.contains(hybridObjectName)) [[unlikely]] {
auto message =
"HybridObject \"" + std::string(hybridObjectName) +
Expand All @@ -27,11 +28,12 @@ void HybridObjectRegistry::registerHybridObjectConstructor(std::string hybridObj
"- If you just registered your own HybridObject, maybe you accidentally called `registerHybridObjectConstructor(...)` twice?";
throw std::runtime_error(message);
}
#endif
map.insert({hybridObjectName, std::move(constructorFn)});
Logger::log(TAG, "Successfully registered HybridObject \"%s\"!", hybridObjectName);
}

std::shared_ptr<HybridObject> HybridObjectRegistry::createHybridObject(std::string hybridObjectName) {
std::shared_ptr<HybridObject> HybridObjectRegistry::createHybridObject(const std::string& hybridObjectName) {
auto& map = HybridObjectRegistry::getRegistry();
auto fn = map.find(hybridObjectName);
if (fn == map.end()) [[unlikely]] {
Expand All @@ -43,6 +45,11 @@ std::shared_ptr<HybridObject> HybridObjectRegistry::createHybridObject(std::stri
std::shared_ptr<HybridObject> instance = fn->second();

#if DEBUG
if (instance == nullptr) [[unlikely]] {
throw std::runtime_error("Failed to create HybridObject \"" + hybridObjectName +
"\" - "
"The constructor returned a nullptr!");
}
if (instance->getName() != hybridObjectName) [[unlikely]] {
throw std::runtime_error("HybridObject's name (\"" + instance->getName() +
"\") does not match"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class HybridObjectRegistry {
* It will be uniquely identified via it's `hybridObjectName`, and can be initialized from
* JS using `NitroModules.get<T>(name)` - which will call the `constructorFn` here.
*/
static void registerHybridObjectConstructor(std::string hybridObjectName, HybridObjectConstructorFn&& constructorFn);
static void registerHybridObjectConstructor(const std::string& hybridObjectName, HybridObjectConstructorFn&& constructorFn);

static std::shared_ptr<HybridObject> createHybridObject(std::string hybridObjectName);
static std::shared_ptr<HybridObject> createHybridObject(const std::string& hybridObjectName);

private:
static std::unordered_map<std::string, HybridObjectConstructorFn>& getRegistry();
Expand Down

0 comments on commit ec910a7

Please sign in to comment.