distillation_shortcut_unit/parameter_collection.rs
1use crate::shared_unit_data::*;
2use crate::distillation_shortcut_unit::DistillationShortcutUnit;
3use cobia::*;
4
5/// A collection of parameters for the distillation shortcut unit.
6///
7/// This collection implements the `ICapeIdentification` and `ICapeCollection<ICapeParameter>` interfaces
8/// and provides methods to manage parameters associated with the unit.
9
10#[cape_object_implementation(
11 interfaces={
12 cape_open_1_2::ICapeIdentification,
13 cape_open_1_2::ICapeCollection<cape_open_1_2::CapeParameter>,
14 },
15 new_arguments={
16 shared_unit_data
17 }
18 )]
19pub(crate) struct ParameterCollection {
20 /// The name of the parameter collection.
21 name: CapeStringImpl,
22 /// The description of the parameter collection.
23 description: CapeStringImpl,
24 /// The parameters in the collection.
25 parameters : Vec<cape_open_1_2::CapeParameter>,
26 /// Shared data for the unit, allowing access to common properties.
27 shared_unit_data: SharedUnitDataRef,
28 /// A map to quickly access parameters by their names.
29 parameter_name_map : cobia::CapeOpenMap<usize>,
30}
31
32impl ParameterCollection {
33
34 /// Creates a new `ParameterCollection` for the distillation shortcut unit.
35 ///
36 /// # Arguments
37 /// * `shared_unit_data` - A reference to shared data for the unit, which contains common properties.
38 ///
39 /// # Returns
40 /// A new instance of `ParameterCollection` initialized with the provided shared data.
41
42 fn new(shared_unit_data: SharedUnitDataRef) -> Self {
43 Self {
44 name : CapeStringImpl::from(format!("{} parameter collection",DistillationShortcutUnit::NAME)),
45 description : CapeStringImpl::from(format!("Parameter collection of {}",DistillationShortcutUnit::NAME)),
46 parameters: Vec::new(),
47 shared_unit_data,
48 parameter_name_map : cobia::CapeOpenMap::new(),
49 cobia_object_data : std::default::Default::default(), //this member is generated by cape_object_implementation and can be set to default()
50 }
51 }
52
53 /// Adds a parameter to the collection.
54 ///
55 /// The parameter's name is cached for quick look-ups by name.
56 ///
57 /// # Arguments
58 /// * `parameter` - A smart pointer to the parameter to be added.
59 ///
60
61 pub(crate) fn add_parameter<T:CapeSmartPointer>(&mut self,parameter:T) {
62 //cache the name for look-ups by name
63 let index=self.parameters.len();
64 self.parameters.push(cape_open_1_2::CapeParameter::from_object(¶meter).unwrap());
65 let iden=cape_open_1_2::CapeIdentification::from_object(¶meter).unwrap();
66 let mut name=cobia::CapeStringImpl::new();
67 iden.get_component_name(&mut name).unwrap();
68 self.parameter_name_map.insert_from_cape_string_constant(name,index);
69 }
70
71 /// Obtain an iterator to iterate over the parameters in the collection.
72 ///
73 /// # Returns
74 /// An iterator that yields references to the parameters in the collection.
75
76 pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut cape_open_1_2::CapeParameter> {
77 self.parameters.iter_mut()
78 }
79}
80
81impl std::fmt::Display for ParameterCollection {
82
83 /// Formats the parameter collection for display.
84 ///
85 /// The std::fmt::Display interface is used when generating the
86 /// source name of the object that raises an error.
87 ///
88 /// # Arguments
89 /// * `f` - A mutable reference to the formatter where the output will be written.
90 ///
91 /// # Returns
92 /// A result indicating whether the formatting was successful or not.
93
94 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95 write!(f,"Parameter collection of {} unit '{}'",DistillationShortcutUnit::NAME, self.shared_unit_data.borrow().name)
96 }
97
98}
99
100impl cape_open_1_2::ICapeIdentification for ParameterCollection {
101
102 /// Gets the name of the component.
103 ///
104 /// # Arguments
105 /// * `name` - A mutable reference to a `CapeStringOut` where the name will be set.
106 ///
107 /// # Returns
108 /// A result indicating success or failure. If successful, the name is set in `name`.
109
110 fn get_component_name(&mut self,name:&mut CapeStringOut) -> Result<(), COBIAError> {
111 name.set(&self.name)?;
112 Ok(())
113 }
114
115 /// Gets the description of the component.
116 ///
117 /// # Arguments
118 /// * `description` - A mutable reference to a `CapeStringOut` where the description will be set.
119 ///
120 /// # Returns
121 /// A result indicating success or failure. If successful, the description is set in `description`.
122
123 fn get_component_description(&mut self,description:&mut CapeStringOut) -> Result<(), COBIAError> {
124 description.set(&self.description)?;
125 Ok(())
126 }
127
128 /// Sets the name of the component.
129 ///
130 /// This method is not allowed for this parameter collection implementation and will return an error.
131
132 fn set_component_name(&mut self, _name: &CapeStringIn) -> Result<(), COBIAError> {
133 Err(cobia::COBIAError::Code(cobia::COBIAERR_DENIED))
134 }
135
136 /// Sets the description of the component.
137 ///
138 /// This method is not allowed for this parameter collection implementation and will return an error.
139
140 fn set_component_description(&mut self, _desc: &CapeStringIn) -> Result<(), COBIAError> {
141 Err(cobia::COBIAError::Code(cobia::COBIAERR_DENIED))
142 }
143}
144
145impl cape_open_1_2::ICapeCollection<cape_open_1_2::CapeParameter> for ParameterCollection {
146
147 /// Retrieves a parameter by its index.
148 ///
149 /// # Arguments
150 /// * `index` - The index of the parameter to retrieve.
151 ///
152 /// # Returns
153 /// A result containing the parameter if found, or an error if the index is out of bounds.
154
155 fn item_by_index(&mut self,index:CapeInteger) -> Result<cape_open_1_2::CapeParameter,COBIAError> {
156 if index<0 || index>=self.parameters.len() as CapeInteger {
157 Err(cobia::COBIAError::Code(cobia::COBIAERR_NOSUCHITEM))
158 } else {
159 Ok(self.parameters[index as usize].clone())
160 }
161 }
162
163 /// Retrieves a parameter by its name.
164 ///
165 /// # Arguments
166 /// * `name` - A reference to the name of the parameter to retrieve.
167 ///
168 /// # Returns
169 /// A result containing the parameter if found, or an error if the name does not exist in the collection.
170
171 fn item_by_name(&mut self,name:&CapeStringIn) -> Result<cape_open_1_2::CapeParameter,COBIAError> {
172 let parameter_index=self.parameter_name_map.get(name).ok_or(cobia::COBIAError::Code(cobia::COBIAERR_NOSUCHITEM))?;
173 Ok(self.parameters[*parameter_index].clone())
174 }
175
176 /// Gets the number of parameters in the collection.
177 ///
178 /// # Returns
179 /// A result containing the number of parameters as a `CapeInteger`.
180
181 fn get_count(&mut self) -> Result<CapeInteger,COBIAError> {
182 Ok(self.parameters.len() as CapeInteger)
183 }
184}