21 constexpr auto abortPollInterval = std::chrono::milliseconds(50);
24 GazeTargetWaiter::Result::operator bool()
const
26 return status.wasReached();
32 { handleStatus(
status); });
38 const std::scoped_lock lock(mutex);
41 expectations[targetName] = Expectation{};
47 const std::scoped_lock lock(mutex);
49 const auto it = expectations.find(targetName);
51 if (it == expectations.end())
60 if (it->second.status.gazeTargetID.hasEntityName() and
61 it->second.status.gazeTargetID.getEntitySnapshotID() != *it->second.gazeTargetID)
64 it->second.settled =
false;
71 const std::scoped_lock lock(mutex);
73 expectations.erase(targetName);
80 const std::scoped_lock lock(mutex);
82 const auto it = expectations.find(
status.gazeTargetID.entityName);
84 if (it == expectations.end())
91 if (it->second.gazeTargetID.has_value() and
92 status.gazeTargetID.getEntitySnapshotID() != *it->second.gazeTargetID)
97 it->second.status =
status;
98 it->second.settled =
status.wasReached() or
status.isTerminal();
101 condition.notify_all();
107 const std::function<
bool()>& shouldAbort)
122 <<
" is reached (no timeout).";
128 std::unique_lock lock(mutex);
130 const auto it = expectations.find(targetName);
132 if (it == expectations.end())
135 <<
". Did you forget to call expect() before committing it?";
136 return Result{.status = {}, .message =
"not armed"};
139 while (not it->second.settled)
141 if (shouldAbort and shouldAbort())
143 result.
message =
"aborted by caller";
149 result.
message =
"wait timeout expired";
153 condition.wait_for(lock, abortPollInterval);
157 expectations.erase(it);
162 ARMARX_INFO <<
"Finished waiting for gaze target " <<
QUOTED(targetName) <<
" after "
165 << (result.
message.empty() ? std::string() :
" (" + result.
message +
")")
166 <<
", reached: " << (result ?
"yes" :
"no") <<
".";
static DateTime Now()
Current time on the virtual clock.
static DateTime Invalid()
MemoryID getEntitySnapshotID() const
Represents a point in time.
double toMilliSecondsDouble() const
Returns the amount of milliseconds.
bool isPositive() const
Tests whether the duration is positive (value in µs > 0).
void expect(const std::string &targetName)
Start collecting updates for this target. Call before committing it.
void bind(const std::string &targetName, const armem::MemoryID &gazeTargetID)
Narrow the expectation to the exact request snapshot, once the commit returned its ID.
GazeTargetWaiter(StatusHandlerInterface &handler)
Result wait(const std::string &targetName, const armarx::Duration &timeout, const std::function< bool()> &shouldAbort)
Block until the target is reached, ends, the abort predicate fires or the timeout expires.
void forget(const std::string &targetName)
Drop an expectation without waiting for it.
Registration of callbacks for gaze target lifecycle updates.
virtual void onAny(const Callback &callback)=0
Called for every update, whatever its status.
Business Object (BO) class of GazeTargetStatus: the lifecycle of one gaze target request,...
#define ARMARX_INFO
The normal logging level.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
const simox::meta::EnumNames< TargetStatus > TargetStatusNames
gaze_targets::GazeTargetStatus status
std::string message
Why the wait ended, if it did not end on a status update.