fmi_export/fmi3/instance/
context.rs1use std::{collections::BTreeMap, path::PathBuf};
2
3use fmi::fmi3::Fmi3Status;
4
5use crate::fmi3::{
6 UserModel,
7 instance::{IntermediateUpdateClosure, LogMessageClosure},
8 traits::{Context, ModelLoggingCategory},
9};
10
11pub struct BasicContext<M: UserModel> {
13 logging_on: BTreeMap<M::LoggingCategory, bool>,
16 log_message: LogMessageClosure,
18 resource_path: PathBuf,
20 stop_time: Option<f64>,
22 time: f64,
24 early_return_allowed: bool,
26 intermediate_update: Option<IntermediateUpdateClosure>,
28}
29
30impl<M: UserModel> BasicContext<M> {
31 pub fn new(
32 logging_on: bool,
33 log_message: LogMessageClosure,
34 resource_path: PathBuf,
35 early_return_allowed: bool,
36 intermediate_update: Option<IntermediateUpdateClosure>,
37 ) -> Self {
38 let logging_on = <M as UserModel>::LoggingCategory::all_categories()
39 .map(|category| (category, logging_on))
40 .collect();
41 Self {
42 logging_on,
43 log_message,
44 resource_path,
45 stop_time: None,
46 time: 0.0,
47 early_return_allowed,
48 intermediate_update,
49 }
50 }
51
52 pub fn intermediate_update(&self) -> Option<&IntermediateUpdateClosure> {
53 self.intermediate_update.as_ref()
54 }
55}
56
57impl<M> Context<M> for BasicContext<M>
58where
59 M: UserModel + 'static,
60{
61 fn logging_on(&self, category: <M as UserModel>::LoggingCategory) -> bool {
62 matches!(self.logging_on.get(&category), Some(true))
63 }
64
65 fn set_logging(&mut self, category: <M as UserModel>::LoggingCategory, enabled: bool) {
66 self.logging_on.insert(category, enabled);
67 }
68
69 fn log(&self, status: Fmi3Status, category: M::LoggingCategory, args: std::fmt::Arguments<'_>) {
71 if self.logging_on(category) {
72 (self.log_message)(status, &category.to_string(), args);
74 }
75 }
76
77 fn resource_path(&self) -> &PathBuf {
79 &self.resource_path
80 }
81
82 fn initialize(&mut self, start_time: f64, stop_time: Option<f64>) {
83 self.time = start_time;
84 self.stop_time = stop_time;
85 }
86
87 fn time(&self) -> f64 {
88 self.time
89 }
90
91 fn set_time(&mut self, time: f64) {
92 self.time = time;
93 }
94
95 fn stop_time(&self) -> Option<f64> {
96 self.stop_time
97 }
98
99 fn early_return_allowed(&self) -> bool {
100 self.early_return_allowed
101 }
102
103 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
104 self
105 }
106}