|
| 1 | +#include "score/hm/deadline/deadline_monitor.h" |
| 2 | +#include "ffi_helpers.h" |
| 3 | + |
| 4 | +extern "C" { |
| 5 | +using namespace score::hm; |
| 6 | +using namespace score::hm::internal; |
| 7 | +using namespace score::hm::deadline; |
| 8 | + |
| 9 | +internal::FFIHandle deadline_monitor_builder_create(); |
| 10 | +void deadline_monitor_builder_destroy(internal::FFIHandle handle); |
| 11 | +void deadline_monitor_builder_add_deadline(internal::FFIHandle handler, |
| 12 | + const IdentTag* tag, |
| 13 | + uint32_t min, |
| 14 | + uint32_t max); |
| 15 | +int deadline_monitor_cpp_get_deadline(FFIHandle handler, const IdentTag* tag, FFIHandle* out); |
| 16 | +void deadline_monitor_cpp_destroy(FFIHandle handler); |
| 17 | +void deadline_destroy(FFIHandle deadline_handle); |
| 18 | +int deadline_start(FFIHandle deadline_handle); |
| 19 | +void deadline_stop(FFIHandle deadline_handle); |
| 20 | +} |
| 21 | + |
| 22 | +namespace score::hm::deadline |
| 23 | +{ |
| 24 | +DeadlineMonitorBuilder::DeadlineMonitorBuilder() |
| 25 | + : monitor_builder_handler_(deadline_monitor_builder_create(), &deadline_monitor_builder_destroy) |
| 26 | +{ |
| 27 | +} |
| 28 | + |
| 29 | +DeadlineMonitorBuilder DeadlineMonitorBuilder::add_deadline(const IdentTag& tag, const TimeRange& range) && |
| 30 | +{ |
| 31 | + auto handle = monitor_builder_handler_.as_rust_handle(); |
| 32 | + SCORE_LANGUAGE_FUTURECPP_PRECONDITION(handle.has_value()); |
| 33 | + |
| 34 | + deadline_monitor_builder_add_deadline(handle.value(), &tag, range.min_as_u32(), range.max_as_u32()); |
| 35 | + |
| 36 | + return std::move(*this); |
| 37 | +} |
| 38 | + |
| 39 | +DeadlineMonitor::DeadlineMonitor(FFIHandle handle) : monitor_handle_(handle, &deadline_monitor_cpp_destroy) {} |
| 40 | + |
| 41 | +score::cpp::expected<Deadline, score::hm::Error> DeadlineMonitor::get_deadline(const IdentTag& tag) |
| 42 | +{ |
| 43 | + auto handle = monitor_handle_.as_rust_handle(); |
| 44 | + SCORE_LANGUAGE_FUTURECPP_PRECONDITION(handle.has_value()); |
| 45 | + |
| 46 | + internal::FFIHandle ret = nullptr; |
| 47 | + auto result = deadline_monitor_cpp_get_deadline(handle.value(), &tag, &ret); |
| 48 | + |
| 49 | + if (result != kSuccess) |
| 50 | + { |
| 51 | + return score::cpp::unexpected(::score::hm::ffi::fromRustError(result)); |
| 52 | + } |
| 53 | + |
| 54 | + return score::cpp::expected<Deadline, score::hm::Error>(Deadline{ret}); |
| 55 | +} |
| 56 | + |
| 57 | +Deadline::Deadline(internal::FFIHandle handle) : deadline_handle_(handle, &deadline_destroy), has_handle_(false) {} |
| 58 | + |
| 59 | +Deadline::~Deadline() |
| 60 | +{ |
| 61 | + SCORE_LANGUAGE_FUTURECPP_PRECONDITION(!has_handle_); |
| 62 | +} |
| 63 | + |
| 64 | +score::cpp::expected<DeadlineHandle, score::hm::Error> Deadline::start() |
| 65 | +{ |
| 66 | + if (has_handle_) |
| 67 | + { |
| 68 | + return score::cpp::unexpected(::score::hm::Error::WrongState); |
| 69 | + } |
| 70 | + |
| 71 | + auto handle = deadline_handle_.as_rust_handle(); |
| 72 | + SCORE_LANGUAGE_FUTURECPP_PRECONDITION(handle.has_value()); |
| 73 | + |
| 74 | + auto result = deadline_start(handle.value()); |
| 75 | + if (result != kSuccess) |
| 76 | + { |
| 77 | + return score::cpp::unexpected(::score::hm::ffi::fromRustError(result)); |
| 78 | + } |
| 79 | + |
| 80 | + has_handle_ = true; |
| 81 | + return score::cpp::expected<DeadlineHandle, score::hm::Error>(DeadlineHandle{*this}); |
| 82 | +} |
| 83 | + |
| 84 | +DeadlineHandle::DeadlineHandle(Deadline& deadline) : was_stopped_(false), deadline_(deadline) {} |
| 85 | + |
| 86 | +void DeadlineHandle::stop() |
| 87 | +{ |
| 88 | + SCORE_LANGUAGE_FUTURECPP_PRECONDITION(deadline_.has_value()); |
| 89 | + |
| 90 | + if (!was_stopped_) |
| 91 | + { |
| 92 | + was_stopped_ = true; |
| 93 | + auto handle = deadline_.value().get().deadline_handle_.as_rust_handle(); |
| 94 | + SCORE_LANGUAGE_FUTURECPP_PRECONDITION(handle.has_value()); |
| 95 | + |
| 96 | + deadline_stop(handle.value()); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +DeadlineHandle::DeadlineHandle(DeadlineHandle&& other) |
| 101 | + : was_stopped_(other.was_stopped_), deadline_(std::move(other.deadline_)) |
| 102 | +{ |
| 103 | + other.was_stopped_ = true; |
| 104 | + other.deadline_ = ::score::cpp::optional<std::reference_wrapper<Deadline>>{}; // None |
| 105 | +} |
| 106 | + |
| 107 | +DeadlineHandle::~DeadlineHandle() |
| 108 | +{ |
| 109 | + if (!deadline_.has_value()) |
| 110 | + { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + stop(); |
| 115 | + deadline_.value().get().has_handle_ = false; |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace score::hm::deadline |
0 commit comments