!1276 fix singleton container bug

Merge pull request !1276 from 邢亚楠/0729
This commit is contained in:
openharmony_ci
2022-07-30 00:44:47 +00:00
committed by Gitee
2 changed files with 6 additions and 3 deletions
+3
View File
@@ -43,6 +43,9 @@ public:
std::string nameT = __PRETTY_FUNCTION__;
nameT = nameT.substr(nameT.find("T = "));
nameT = nameT.substr(sizeof("T ="), nameT.length() - sizeof("T = "));
if (SingletonContainer::GetInstance().GetSingleton(nameT) == nullptr) {
return T::GetInstance();
}
return *(reinterpret_cast<T*>(SingletonContainer::GetInstance().GetSingleton(nameT)));
}
+3 -3
View File
@@ -58,7 +58,7 @@ void SingletonContainer::AddSingleton(const std::string& name, void* instance)
static int32_t nextId = 0;
singletonMap[nextId].value = instance;
singletonMap[nextId].refCount = 0;
WLOGFD("add %{public}s", name.c_str());
WLOGFI("add %{public}s", name.c_str());
stringMap[name] = nextId++;
} else {
WLOGFE("add failed: %{public}s", name.c_str());
@@ -70,7 +70,7 @@ void SingletonContainer::SetSingleton(const std::string& name, void* instance)
if (stringMap.find(name) == stringMap.end()) {
AddSingleton(name, instance);
} else {
WLOGFD("set %{public}s", name.c_str());
WLOGFI("set %{public}s", name.c_str());
singletonMap[stringMap[name]].value = instance;
}
}
@@ -78,7 +78,7 @@ void SingletonContainer::SetSingleton(const std::string& name, void* instance)
void* SingletonContainer::GetSingleton(const std::string& name)
{
if (stringMap.find(name) == stringMap.end()) {
WLOGFD("cannot get %{public}s", name.c_str());
WLOGFE("can not get %{public}s", name.c_str());
return nullptr;
}
return singletonMap[stringMap[name]].value;