|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Apache License Version 2.0 which is available at |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: Apache-2.0 |
| 12 | + ********************************************************************************/ |
| 13 | +#include "score/hm/logic/logic_monitor.h" |
| 14 | +#include <score/assert.hpp> |
| 15 | + |
| 16 | +namespace |
| 17 | +{ |
| 18 | +extern "C" { |
| 19 | +using namespace score::hm; |
| 20 | +using namespace score::hm::internal; |
| 21 | +using namespace score::hm::logic; |
| 22 | + |
| 23 | +FFICode logic_monitor_builder_create(const StateTag* initial_state, FFIHandle* logic_monitor_builder_handle_out); |
| 24 | +FFICode logic_monitor_builder_destroy(FFIHandle logic_monitor_builder_handle); |
| 25 | +FFICode logic_monitor_builder_add_state(FFIHandle logic_monitor_builder_handle, |
| 26 | + const StateTag* state, |
| 27 | + const StateTag* allowed_states, |
| 28 | + size_t num_allowed_states); |
| 29 | +FFICode logic_monitor_destroy(FFIHandle logic_monitor_handle); |
| 30 | +FFICode logic_monitor_transition(FFIHandle logic_monitor_handle, const StateTag* target_state); |
| 31 | +FFICode logic_monitor_state(FFIHandle logic_monitor_handle, StateTag* state_out); |
| 32 | +} |
| 33 | + |
| 34 | +FFIHandle logic_monitor_builder_create_wrapper(const StateTag& initial_state) |
| 35 | +{ |
| 36 | + FFIHandle handle{nullptr}; |
| 37 | + auto result{logic_monitor_builder_create(&initial_state, &handle)}; |
| 38 | + SCORE_LANGUAGE_FUTURECPP_ASSERT(result == kSuccess); |
| 39 | + return handle; |
| 40 | +} |
| 41 | +} // namespace |
| 42 | + |
| 43 | +namespace score::hm::logic |
| 44 | +{ |
| 45 | +LogicMonitorBuilder::LogicMonitorBuilder(const StateTag& initial_state) |
| 46 | + : monitor_builder_handle_{logic_monitor_builder_create_wrapper(initial_state), &logic_monitor_builder_destroy} |
| 47 | +{ |
| 48 | +} |
| 49 | + |
| 50 | +LogicMonitorBuilder LogicMonitorBuilder::add_state(const StateTag& state, |
| 51 | + const std::vector<StateTag>& allowed_states) && |
| 52 | +{ |
| 53 | + auto monitor_builder_handle{monitor_builder_handle_.as_rust_handle()}; |
| 54 | + SCORE_LANGUAGE_FUTURECPP_PRECONDITION(monitor_builder_handle.has_value()); |
| 55 | + |
| 56 | + auto result{logic_monitor_builder_add_state( |
| 57 | + monitor_builder_handle.value(), &state, allowed_states.data(), allowed_states.size())}; |
| 58 | + SCORE_LANGUAGE_FUTURECPP_ASSERT(result == kSuccess); |
| 59 | + |
| 60 | + return std::move(*this); |
| 61 | +} |
| 62 | + |
| 63 | +LogicMonitor::LogicMonitor(FFIHandle monitor_handle) : monitor_handle_{monitor_handle, &logic_monitor_destroy} {} |
| 64 | + |
| 65 | +score::cpp::expected<StateTag, Error> LogicMonitor::transition(const StateTag& state) |
| 66 | +{ |
| 67 | + auto monitor_handle{monitor_handle_.as_rust_handle()}; |
| 68 | + SCORE_LANGUAGE_FUTURECPP_PRECONDITION(monitor_handle.has_value()); |
| 69 | + |
| 70 | + auto result{logic_monitor_transition(monitor_handle.value(), &state)}; |
| 71 | + if (result != kSuccess) |
| 72 | + { |
| 73 | + return score::cpp::unexpected(static_cast<Error>(result)); |
| 74 | + } |
| 75 | + |
| 76 | + return score::cpp::expected<StateTag, Error>(state); |
| 77 | +} |
| 78 | + |
| 79 | +score::cpp::expected<StateTag, Error> LogicMonitor::state() |
| 80 | +{ |
| 81 | + auto monitor_handle{monitor_handle_.as_rust_handle()}; |
| 82 | + SCORE_LANGUAGE_FUTURECPP_PRECONDITION(monitor_handle.has_value()); |
| 83 | + |
| 84 | + StateTag state_tag{""}; |
| 85 | + auto result{logic_monitor_state(monitor_handle.value(), &state_tag)}; |
| 86 | + if (result != kSuccess) |
| 87 | + { |
| 88 | + return score::cpp::unexpected(static_cast<Error>(result)); |
| 89 | + } |
| 90 | + |
| 91 | + return score::cpp::expected<StateTag, Error>(state_tag); |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace score::hm::logic |
0 commit comments