libextsupport: Add waitServiceDefault which polls
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user