libextsupport: Add waitServiceDefault which polls

This commit is contained in:
Soo Hwan Na
2023-12-22 15:56:59 +09:00
parent 3763fc7447
commit 503d29fbb5

View File

@@ -1,3 +1,6 @@
#include <chrono>
#include <thread>
#include <type_traits>
#include <string>
@@ -17,3 +20,24 @@ static inline std::shared_ptr<T> getServiceDefault(void)
{
return getService<T>(std::string() + T::descriptor + "/default");
}
template <typename T>
static std::shared_ptr<T> waitServiceDefault(int retries = 5)
{
std::shared_ptr<T> kService = nullptr;
const auto kServiceDesc = std::string() + T::descriptor + "/default";
// If not declared, just return null
if (AServiceManager_isDeclared(kServiceDesc.c_str())) {
do {
kService = getService<T>(kServiceDesc);
if (kService == nullptr) {
std::this_thread::sleep_for(std::chrono::seconds(5));
--retries;
} else {
break;
}
} while (retries > 0);
}
return kService;
}