nautilus_backtest/ffi/
engine.rs

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// -------------------------------------------------------------------------------------------------
15
16use std::ops::{Deref, DerefMut};
17
18use nautilus_common::ffi::{clock::TestClock_API, timer::TimeEventHandler};
19use nautilus_core::{
20    ffi::{cvec::CVec, parsing::u8_as_bool},
21    UnixNanos,
22};
23
24use crate::engine::TimeEventAccumulator;
25
26#[repr(C)]
27pub struct TimeEventAccumulatorAPI(Box<TimeEventAccumulator>);
28
29impl Deref for TimeEventAccumulatorAPI {
30    type Target = TimeEventAccumulator;
31
32    fn deref(&self) -> &Self::Target {
33        &self.0
34    }
35}
36
37impl DerefMut for TimeEventAccumulatorAPI {
38    fn deref_mut(&mut self) -> &mut Self::Target {
39        &mut self.0
40    }
41}
42
43#[no_mangle]
44pub extern "C" fn time_event_accumulator_new() -> TimeEventAccumulatorAPI {
45    TimeEventAccumulatorAPI(Box::new(TimeEventAccumulator::new()))
46}
47
48#[no_mangle]
49pub extern "C" fn time_event_accumulator_drop(accumulator: TimeEventAccumulatorAPI) {
50    drop(accumulator); // Memory freed here
51}
52
53#[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}
62
63#[no_mangle]
64pub extern "C" fn time_event_accumulator_drain(accumulator: &mut TimeEventAccumulatorAPI) -> CVec {
65    let handlers: Vec<TimeEventHandler> = accumulator.drain().into_iter().map(Into::into).collect();
66    handlers.into()
67}