cobia/cape_array_real_scalar.rs
1use crate::C;
2use crate::*;
3
4/// Scalar based CapeArrayRealOut implementation
5///
6/// ICapeArrayReal is passed as data container between CAPE-OPEN functions.
7/// It is up to the caller to provide the interface, and its implementation.
8/// This class provides a default impementation using a scalar CapeReal.
9///
10/// This class can be used as input and output argument. However, it should
11/// only be used as an output argument in case a scalar value is expected, such
12/// as for getting the density from a material object, as any attempt to
13/// resize the content to anything else but 1 will result in an error.
14///
15/// # Examples
16///
17/// ```
18/// use cobia::*;
19///
20/// fn set_content(a: &mut CapeArrayRealOut) {
21/// a.resize(1);
22/// a[0]=2.5;
23/// }
24///
25/// let mut arr = cobia::CapeArrayRealScalar::new();
26/// set_content(&mut CapeArrayRealOutFromProvider::from(&mut arr).as_cape_array_real_out());
27/// assert_eq!(arr.value(), 2.5);
28/// ```
29
30#[derive (Clone)]
31pub struct CapeArrayRealScalar {
32 value: CapeReal,
33}
34
35impl CapeArrayRealScalar {
36
37 /// Create a new CapeArrayRealScalar
38 ///
39 /// Creates a new empty CapeArrayRealScalar
40 ///
41 /// # Examples
42 ///
43 /// ```
44 /// use cobia;
45 /// let arr = cobia::CapeArrayRealScalar::new();
46 /// assert!(cobia::CapeReal::is_nan(arr.value()));
47 /// ```
48 pub fn new() -> Self {
49 Self {
50 value: CapeReal::NAN,
51 }
52 }
53
54 /// Create a new CapeArrayRealScalar with initial value
55 ///
56 /// Creates a new empty CapeArrayRealScalar and sets the value
57 ///
58 /// # Arguments
59 ///
60 /// * `value` - The initial value
61 ///
62 /// # Examples
63 ///
64 /// ```
65 /// use cobia;
66 /// let arr = cobia::CapeArrayRealScalar::from(3.14);
67 /// assert_eq!(arr.value(),3.14);
68 /// ```
69 pub fn from(value:CapeReal) -> Self {
70 Self {
71 value,
72 }
73 }
74
75 /// Return value
76 ///
77 /// Returns the value.
78 ///
79 /// # Example
80 ///
81 /// ```
82 /// use cobia;
83 /// let arr = cobia::CapeArrayRealScalar::from(10.0);
84 /// assert_eq!(arr.value(),10.0);
85 /// ```
86
87 pub fn value(&self) -> CapeReal {
88 self.value
89 }
90
91 /// Set the value
92 ///
93 /// Sets the value
94 ///
95 /// # Arguments
96 ///
97 /// * `value` - The value to be set
98 ///
99 /// # Example
100 ///
101 /// ```
102 /// use cobia;
103 /// let mut arr = cobia::CapeArrayRealScalar::from(10.0);
104 /// arr.set(11.0);
105 /// assert_eq!(arr.value(),11.0);
106 /// ```
107
108 pub fn set(&mut self,value:CapeReal) {
109 self.value=value;
110 }
111
112 /// Interface member function
113
114 extern "C" fn get(
115 me: *mut ::std::os::raw::c_void,
116 data: *mut *mut CapeReal,
117 size: *mut C::CapeSize,
118 ) {
119 let p = me as *mut Self;
120 let arr: &mut Self = unsafe { &mut *p };
121 unsafe {
122 *data = &mut arr.value as *mut CapeReal;
123 *size = 1;
124 }
125 }
126
127 /// Interface member function
128
129 extern "C" fn setsize(
130 me: *mut ::std::os::raw::c_void,
131 size: C::CapeSize,
132 data: *mut *mut CapeReal,
133 ) -> CapeResult {
134 if size!=1 {
135 COBIAERR_INVALIDARGUMENT //only a size of 1 is allowed
136 } else {
137 let p = me as *mut Self;
138 let arr: &mut Self = unsafe { &mut *p };
139 unsafe {
140 *data = &mut arr.value as *mut CapeReal;
141 }
142 COBIAERR_NOERROR
143 }
144 }
145
146 /// Interface v-table
147
148 const VTABLE: C::ICapeArrayReal_VTable = C::ICapeArrayReal_VTable {
149 get: Some(Self::get),
150 setsize: Some(Self::setsize),
151 };
152
153}
154
155impl AsMut<CapeReal> for CapeArrayRealScalar {
156
157 /// Return mutable reference to the scalar value
158 ///
159 /// Returns a mutable reference to the scalar value.
160 ///
161 /// # Example
162 ///
163 /// ```
164 /// use cobia;
165 /// let mut arr = cobia::CapeArrayRealScalar::from(10.0);
166 /// *arr.as_mut()=11.0;
167 /// assert_eq!(arr.value(),11.0);
168 /// ```
169
170 fn as_mut(&mut self) -> &mut CapeReal {
171 &mut self.value
172 }
173
174}
175
176impl CapeArrayRealProviderIn for CapeArrayRealScalar {
177 /// Convert to ICapeArrayReal
178 ///
179 /// Returns a reference to the ICapeArrayReal interface.
180 ///
181 /// # Examples
182 ///
183 /// ```
184 /// use cobia::*;
185 ///
186 /// fn test_content(a: &CapeArrayRealIn) {
187 /// assert_eq!(a[0], 0.0);
188 /// }
189 ///
190 /// let arr = cobia::CapeArrayRealScalar::from(0.0);
191 /// test_content(&CapeArrayRealInFromProvider::from(&arr).as_cape_array_real_in());
192 /// ```
193
194 fn as_cape_array_real_in(&self) -> C::ICapeArrayReal {
195 C::ICapeArrayReal {
196 me: (self as *const Self).cast_mut() as *mut ::std::os::raw::c_void,
197 vTbl: (&Self::VTABLE as *const C::ICapeArrayReal_VTable).cast_mut()
198 }
199 }
200}
201
202impl CapeArrayRealProviderOut for CapeArrayRealScalar {
203 /// Convert to ICapeArrayReal
204 ///
205 /// Returns a mutable reference to the ICapeArrayReal interface.
206 ///
207 /// # Examples
208 ///
209 /// ```
210 /// use cobia::*;
211 ///
212 /// fn set_content(a: &mut CapeArrayRealOut) {
213 /// a.resize(1);
214 /// a[0]=2.5;
215 /// }
216 ///
217 /// let mut arr = cobia::CapeArrayRealScalar::new();
218 /// set_content(&mut CapeArrayRealOutFromProvider::from(&mut arr).as_cape_array_real_out());
219 /// assert_eq!(arr.value(), 2.5);
220 /// ```
221
222 fn as_cape_array_real_out(&mut self) -> C::ICapeArrayReal {
223 C::ICapeArrayReal {
224 me: (self as *const Self).cast_mut() as *mut ::std::os::raw::c_void,
225 vTbl: (&Self::VTABLE as *const C::ICapeArrayReal_VTable).cast_mut()
226 }
227 }
228}