| 19 | using Task = aimrt::util::Function<aimrt_function_executor_task_ops_t>; |
| 20 | |
| 21 | class ExecutorRef { |
| 22 | public: |
| 23 | ExecutorRef() = default; |
| 24 | explicit ExecutorRef(const aimrt_executor_base_t* base_ptr) |
| 25 | : base_ptr_(base_ptr) {} |
| 26 | ~ExecutorRef() = default; |
| 27 | |
| 28 | explicit operator bool() const { return (base_ptr_ != nullptr); } |
| 29 | |
| 30 | const aimrt_executor_base_t* NativeHandle() const { return base_ptr_; } |
| 31 | |
| 32 | std::string_view Type() const { |
| 33 | AIMRT_ASSERT(base_ptr_, "Reference is null."); |
| 34 | return aimrt::util::ToStdStringView(base_ptr_->type(base_ptr_->impl)); |
| 35 | } |
| 36 | |
| 37 | std::string_view Name() const { |
| 38 | AIMRT_ASSERT(base_ptr_, "Reference is null."); |
| 39 | return aimrt::util::ToStdStringView(base_ptr_->name(base_ptr_->impl)); |
| 40 | } |
| 41 | |
| 42 | bool ThreadSafe() const { |
| 43 | AIMRT_ASSERT(base_ptr_, "Reference is null."); |
| 44 | return base_ptr_->is_thread_safe(base_ptr_->impl); |
| 45 | } |
| 46 | |
| 47 | bool IsInCurrentExecutor() const { |
| 48 | AIMRT_ASSERT(base_ptr_, "Reference is null."); |
| 49 | return base_ptr_->is_in_current_executor(base_ptr_->impl); |
| 50 | } |
| 51 | |
| 52 | bool SupportTimerSchedule() const { |
| 53 | AIMRT_ASSERT(base_ptr_, "Reference is null."); |
| 54 | return base_ptr_->is_support_timer_schedule(base_ptr_->impl); |
| 55 | } |
| 56 | |
| 57 | void Execute(Task&& task) { |
| 58 | AIMRT_ASSERT(base_ptr_, "Reference is null."); |
| 59 | base_ptr_->execute(base_ptr_->impl, task.NativeHandle()); |
| 60 | } |
| 61 | |
| 62 | void Dispatch(Task&& task) { |
| 63 | AIMRT_ASSERT(base_ptr_, "Reference is null."); |
| 64 | if (base_ptr_->is_in_current_executor(base_ptr_->impl)) { |
| 65 | task(); |
| 66 | } else { |
| 67 | base_ptr_->execute(base_ptr_->impl, task.NativeHandle()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | std::chrono::system_clock::time_point Now() const { |
| 72 | AIMRT_ASSERT(base_ptr_, "Reference is null."); |
| 73 | return common::util::GetTimePointFromTimestampNs(base_ptr_->now(base_ptr_->impl)); |
| 74 | } |
| 75 | |
| 76 | void ExecuteAt(std::chrono::system_clock::time_point tp, Task&& task) { |
| 77 | AIMRT_ASSERT(base_ptr_, "Reference is null."); |
| 78 |
no outgoing calls