1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
1516use std::ops::{Deref, DerefMut};
1718use nautilus_common::ffi::{clock::TestClock_API, timer::TimeEventHandler};
19use nautilus_core::{
20 UnixNanos,
21 ffi::{cvec::CVec, parsing::u8_as_bool},
22};
2324use crate::accumulator::TimeEventAccumulator;
2526#[repr(C)]
27pub struct TimeEventAccumulatorAPI(Box<TimeEventAccumulator>);
2829impl Deref for TimeEventAccumulatorAPI {
30type Target = TimeEventAccumulator;
3132fn deref(&self) -> &Self::Target {
33&self.0
34}
35}
3637impl DerefMut for TimeEventAccumulatorAPI {
38fn deref_mut(&mut self) -> &mut Self::Target {
39&mut self.0
40}
41}
4243#[unsafe(no_mangle)]
44pub extern "C" fn time_event_accumulator_new() -> TimeEventAccumulatorAPI {
45 TimeEventAccumulatorAPI(Box::default())
46}
4748#[unsafe(no_mangle)]
49pub extern "C" fn time_event_accumulator_drop(accumulator: TimeEventAccumulatorAPI) {
50 drop(accumulator); // Memory freed here
51}
5253#[unsafe(no_mangle)]
54pub extern "C" fn time_event_accumulator_advance_clock(
55 accumulator: &mut TimeEventAccumulatorAPI,
56 clock: &mut TestClock_API,
57 to_time_ns: UnixNanos,
58 set_time: u8,
59) {
60 accumulator.advance_clock(clock, to_time_ns, u8_as_bool(set_time));
61}
6263#[unsafe(no_mangle)]
64pub extern "C" fn time_event_accumulator_drain(accumulator: &mut TimeEventAccumulatorAPI) -> CVec {
65let handlers: Vec<TimeEventHandler> = accumulator.drain().into_iter().map(Into::into).collect();
66 handlers.into()
67}