cobia/cape_array_value.rs
1pub use crate::*;
2use std::fmt;
3use std::marker::PhantomData;
4use crate::C;
5use crate::{CapeArrayValueProviderIn,CapeArrayValueProviderOut};
6
7/// CapeArrayValueIn wraps an ICapeArrayValue interface pointer in a read-only manner
8///
9/// Given a reference to an ICapeArrayValue interface pointer, this allows getting,
10/// but not setting the elements.
11///
12/// This interface is typically used as arguments to rust methods
13/// on traits that are generated from CAPE-OPEN interfaces that have
14/// ICapeArrayValue input arguments.
15///
16/// A NULL interface pointer is treated as an empty array.
17///
18/// # Examples
19///
20/// ```
21/// use cobia::*;
22///
23/// fn test_content(a: &CapeArrayValueIn) {
24/// assert_eq!(a.as_value_vec().unwrap(), vec![cobia::CapeValueContent::Boolean(true),cobia::CapeValueContent::Real(1.2),cobia::CapeValueContent::Empty]);
25/// }
26///
27/// let arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::Boolean(true),cobia::CapeValueContent::Real(1.2),cobia::CapeValueContent::Empty]);
28/// test_content(&CapeArrayValueInFromProvider::from(&arr).as_cape_array_value_in());
29/// ```
30
31pub struct CapeArrayValueIn<'a> {
32 data: *mut *mut C::ICapeValue,
33 size: C::CapeSize,
34 interface: &'a *mut C::ICapeArrayValue,
35 _lifetime: PhantomData<&'a ()> //even though we do not refer to the interace after contruction, life time is bound to the interface, as each of the elements are
36}
37
38impl<'a> CapeArrayValueIn<'a> {
39 /// Create a new CapeValueIn from an ICapeArrayValue interface pointer.
40 ///
41 /// # Arguments
42 ///
43 /// * `interface` - A pointer to an ICapeArrayValue interface
44 ///
45 /// # Examples
46 ///
47 /// ```
48 /// use cobia::*;
49 /// use cobia::prelude::*;
50 /// let arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::Boolean(true),cobia::CapeValueContent::Real(1.2),cobia::CapeValueContent::Empty]);
51 /// let i_cape_array_value=arr.as_cape_array_value_in();
52 /// let mut i_cape_array_value_ptr=(&i_cape_array_value as *const C::ICapeArrayValue).cast_mut(); //normally a pointer to the interface is received
53 /// let va = cobia::CapeArrayValueIn::new(&mut i_cape_array_value_ptr); //CapeArrayValueIn from *mut C::ICapeArrayValue
54 /// assert_eq!(va.as_value_vec().unwrap(), vec![cobia::CapeValueContent::Boolean(true),cobia::CapeValueContent::Real(1.2),cobia::CapeValueContent::Empty]);
55 /// ```
56
57 pub fn new(interface: &'a *mut C::ICapeArrayValue) -> CapeArrayValueIn<'a> {
58 if interface.is_null() {
59 CapeArrayValueIn {
60 data : std::ptr::null_mut(),
61 size : 0,
62 interface,
63 _lifetime : std::default::Default::default()
64 }
65 } else {
66 let mut data: *mut *mut C::ICapeValue = std::ptr::null_mut();
67 let mut size: C::CapeSize = 0;
68 unsafe { (*(**interface).vTbl).get.unwrap()((**interface).me, &mut data, &mut size) };
69 CapeArrayValueIn {
70 data,
71 size,
72 interface,
73 _lifetime : std::default::Default::default()
74 }
75 }
76 }
77
78 /// Return the size of the array
79 ///
80 /// # Examples
81 ///
82 /// ```
83 /// use cobia::*;
84 ///
85 /// fn test_size(a: &CapeArrayValueIn) {
86 /// assert_eq!(a.size(), 3);
87 /// }
88 ///
89 /// let arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::Boolean(true),cobia::CapeValueContent::Real(1.2),cobia::CapeValueContent::Empty]);
90 /// test_size(&CapeArrayValueInFromProvider::from(&arr).as_cape_array_value_in());
91 /// ```
92 pub fn size(&self) -> usize {
93 self.size as usize
94 }
95
96 /// Check if the array is empty
97 ///
98 /// # Examples
99 ///
100 /// ```
101 /// use cobia::*;
102 ///
103 /// fn test_empty(a: &CapeArrayValueIn) {
104 /// assert!(a.is_empty());
105 /// }
106 ///
107 /// let arr = cobia::CapeArrayValueVec::new();
108 /// test_empty(&CapeArrayValueInFromProvider::from(&arr).as_cape_array_value_in());
109 /// ```
110 pub fn is_empty(&self) -> bool {
111 self.size == 0
112 }
113
114 /// Get an element
115 ///
116 /// # Arguments
117 ///
118 /// * `index` - The index of the element to get
119 ///
120 /// Note that neither Index and IndexMut is
121 /// provided for CapeArrayValueIn, because these interfaces
122 /// yield a reference to the element, whereas the elements
123 /// of CapeArrayValueIn are represented by an interface,
124 /// which is conveniently wrapped into CapeValueIn.
125 ///
126 /// Note that the life time of the CapeValueIn is tied to the
127 /// life time of the CapeArrayValueIn.
128 ///
129 /// # Examples
130 ///
131 /// ```
132 /// use cobia::*;
133 ///
134 /// fn test_size(a: &CapeArrayValueIn) {
135 /// assert_eq!(a.at(1).unwrap().get_boolean().unwrap(), true);
136 /// }
137 ///
138 /// let arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::Real(2.4),cobia::CapeValueContent::Boolean(true)]);
139 /// test_size(&CapeArrayValueInFromProvider::from(&arr).as_cape_array_value_in());
140 /// ```
141
142 pub fn at(&self, index: usize) -> Result<CapeValueIn<'a>, COBIAError> {
143 if index >= self.size as usize {
144 return Err(COBIAError::Code(COBIAERR_NOSUCHITEM));
145 }
146 let p=unsafe { self.data.add(index) };
147 if unsafe{*p}.is_null() {
148 //this provided by the implementor of ICapeArrayValue and should not be null
149 Err(COBIAError::Code(COBIAERR_NULLPOINTER))
150 } else {
151 Ok(CapeValueIn::new(unsafe { &mut *p }))
152 }
153 }
154
155 /// Return the content of the value array as a vector of CapeValueContent
156 ///
157 /// # Examples
158 ///
159 /// ```
160 /// use cobia::*;
161 ///
162 /// fn test_content(a: &CapeArrayValueIn) {
163 /// assert_eq!(a.as_value_vec().unwrap(), vec![cobia::CapeValueContent::Real(2.4),cobia::CapeValueContent::Boolean(true)]);
164 /// }
165 ///
166 /// let arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::Real(2.4),cobia::CapeValueContent::Boolean(true)]);
167 /// test_content(&CapeArrayValueInFromProvider::from(&arr).as_cape_array_value_in());
168 /// ```
169
170 pub fn as_value_vec(&self) -> Result<Vec<CapeValueContent>, COBIAError> {
171 let mut vec= Vec::new();
172 vec.reserve(self.size as usize);
173 for i in 0..self.size {
174 let p=unsafe { *self.data.add(i as usize) };
175 if p.is_null() {
176 //this provided by the implementor of ICapeArrayValue and should not be null
177 vec.push(CapeValueContent::Empty);
178 } else {
179 let val=CapeValueIn::new(&p);
180 match val.get_type() {
181 Ok(tp)=>
182 match tp {
183 CapeValueType::String => vec.push(CapeValueContent::String(
184 match val.get_string() {
185 Ok(s)=>s,
186 Err(e)=> {return Err(e);}
187 })),
188 CapeValueType::Integer => vec.push(CapeValueContent::Integer(
189 match val.get_integer() {
190 Ok(i)=>i,
191 Err(e)=> {return Err(e);}
192 })),
193 CapeValueType::Boolean => vec.push(CapeValueContent::Boolean(
194 match val.get_boolean() {
195 Ok(b)=>b,
196 Err(e)=> {return Err(e);}
197 })),
198 CapeValueType::Real => vec.push(CapeValueContent::Real(
199 match val.get_real() {
200 Ok(r)=>r,
201 Err(e)=> {return Err(e);}
202 })),
203 CapeValueType::Empty => vec.push(CapeValueContent::Empty),
204 },
205 Err(e)=> {return Err(e);}
206 }
207 }
208 }
209 Ok(vec)
210 }
211
212}
213
214pub struct CapeArrayValueInIterator<'a> {
215 arr: &'a CapeArrayValueIn<'a>,
216 index: usize,
217}
218
219impl<'a> Iterator for CapeArrayValueInIterator<'a> {
220 type Item = CapeValueIn<'a>;
221
222 fn next(&mut self) -> Option<Self::Item> {
223 if self.index < self.arr.size as usize {
224 let res = self.arr.at(self.index);
225 self.index += 1;
226 Some(res.unwrap())
227 } else {
228 None
229 }
230 }
231}
232
233impl<'a> CapeArrayValueIn<'a> {
234 /// Return an iterator over the value array.
235 ///
236 /// # Examples
237 ///
238 /// ```
239 /// use cobia::*;
240 ///
241 /// fn test_iter(a: &CapeArrayValueIn) {
242 /// let mut iter = a.iter();
243 /// assert_eq!(iter.next().unwrap().get_integer().unwrap(), 4);
244 /// assert_eq!(iter.next().unwrap().get_boolean().unwrap(), true);
245 /// assert!(!iter.next().is_some());
246 /// }
247 ///
248 /// let arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::Integer(4),cobia::CapeValueContent::Boolean(true)]);
249 /// test_iter(&CapeArrayValueInFromProvider::from(&arr).as_cape_array_value_in());
250 /// ```
251
252 pub fn iter(&self) -> CapeArrayValueInIterator<'_> {
253 CapeArrayValueInIterator {
254 arr: &self,
255 index: 0,
256 }
257 }
258}
259
260impl<'a> fmt::Display for CapeArrayValueIn<'a> {
261 /// Display the content of the value array as a vector.
262 ///
263 /// # Examples
264 ///
265 /// ```
266 /// use cobia::*;
267 ///
268 /// fn test_format(a: &CapeArrayValueIn) {
269 /// assert_eq!(format!("{}", a), "[4, true, <empty>, \"H2O\"]");
270 /// }
271 ///
272 /// let arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::Integer(4),cobia::CapeValueContent::Boolean(true),cobia::CapeValueContent::Empty,cobia::CapeValueContent::String("H2O".to_string())]);
273 /// test_format(&CapeArrayValueInFromProvider::from(&arr).as_cape_array_value_in());
274 /// ```
275
276 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
277 write!(f, "[")?;
278 for (count, v) in self.iter().enumerate() {
279 if count != 0 {
280 write!(f, ", ")?;
281 }
282 write!(f, "{}", v)?;
283 }
284 write!(f, "]")
285 }
286}
287
288impl<'a> CapeArrayValueProviderIn for CapeArrayValueIn<'a> {
289 fn as_cape_array_value_in(&self) -> C::ICapeArrayValue {
290 unsafe { **self.interface }
291 }
292}
293
294/// CapeArrayValueOut wraps an ICapeArrayValue interface pointer.
295///
296/// Given a reference to an ICapeArrayValue interface pointer, this allows setting
297/// and getting the elements.
298///
299/// This interface is typically used as arguments to rust methods
300/// on traits that are generated from CAPE-OPEN interfaces that have
301/// ICapeArrayValue output arguments.
302///
303/// NULL interface pointers are not allowed.
304///
305/// # Examples
306///
307/// ```
308/// use cobia::*;
309///
310/// fn set_content(a: &mut CapeArrayValueOut) {
311/// a.put_array(&[cobia::CapeValueContent::Boolean(true),cobia::CapeValueContent::Real(1.2),cobia::CapeValueContent::Empty]).unwrap();
312/// }
313///
314/// let mut arr = cobia::CapeArrayValueVec::new();
315/// set_content(&mut CapeArrayValueOutFromProvider::from(&mut arr).as_cape_array_value_out());
316/// assert_eq!(arr.as_value_vec(), vec![cobia::CapeValueContent::Boolean(true),cobia::CapeValueContent::Real(1.2),cobia::CapeValueContent::Empty]);
317/// ```
318
319pub struct CapeArrayValueOut<'a> {
320 interface: &'a mut *mut C::ICapeArrayValue,
321 data: *mut *mut C::ICapeValue,
322 size: C::CapeSize,
323}
324
325impl<'a> CapeArrayValueOut<'a> {
326 /// Create a new CapeValueOut from an ICapeArrayValue interface pointer.
327 ///
328 /// # Arguments
329 ///
330 /// * `interface` - A pointer to an ICapeArrayValue interface
331 ///
332 /// # Examples
333 ///
334 /// ```
335 /// use cobia::*;
336 /// use cobia::prelude::*;
337 /// let mut arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::Boolean(true),cobia::CapeValueContent::Real(1.2),cobia::CapeValueContent::Empty]);
338 /// let i_cape_array_value=arr.as_cape_array_value_out();
339 /// let mut i_cape_array_value_ptr=(&i_cape_array_value as *const C::ICapeArrayValue).cast_mut(); //normally a pointer to the interface is received
340 /// let va = cobia::CapeArrayValueOut::new(&mut i_cape_array_value_ptr); //CapeArrayValueOut from *mut C::ICapeArrayValue
341 /// assert_eq!(va.as_value_vec().unwrap(), vec![cobia::CapeValueContent::Boolean(true),cobia::CapeValueContent::Real(1.2),cobia::CapeValueContent::Empty]);
342 /// ```
343
344 pub fn new(interface: &'a mut *mut C::ICapeArrayValue) -> CapeArrayValueOut<'a> {
345 let mut data: *mut *mut C::ICapeValue = std::ptr::null_mut();
346 let mut size: C::CapeSize = 0;
347 unsafe { (*(**interface).vTbl).get.unwrap()((**interface).me, &mut data, &mut size) };
348 CapeArrayValueOut {
349 interface,
350 data,
351 size,
352 }
353 }
354
355 /// Return the size of the array
356 ///
357 /// # Examples
358 ///
359 /// ```
360 /// use cobia::*;
361 ///
362 /// fn check_size(a: &mut CapeArrayValueOut) {
363 /// assert_eq!(a.size(), 3);
364 /// }
365 ///
366 /// let mut arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::Boolean(true),cobia::CapeValueContent::Real(1.2),cobia::CapeValueContent::Empty]);
367 /// check_size(&mut CapeArrayValueOutFromProvider::from(&mut arr).as_cape_array_value_out());
368 /// ```
369 pub fn size(&self) -> usize {
370 self.size as usize
371 }
372
373 /// Check if the array is empty
374 ///
375 /// # Examples
376 ///
377 /// ```
378 /// use cobia::*;
379 ///
380 /// fn check_empty(a: &mut CapeArrayValueOut) {
381 /// assert!(a.is_empty());
382 /// }
383 ///
384 /// let mut arr = cobia::CapeArrayValueVec::new();
385 /// check_empty(&mut CapeArrayValueOutFromProvider::from(&mut arr).as_cape_array_value_out());
386 /// ```
387 pub fn is_empty(&self) -> bool {
388 self.size == 0
389 }
390
391 /// Set the content from a slice of CapeValueContent
392 ///
393 /// # Arguments
394 ///
395 /// * `arr` - A slice of CapeValueContent
396 ///
397 /// # Examples
398 ///
399 /// ```
400 /// use cobia::*;
401 ///
402 /// fn set_content(a: &mut CapeArrayValueOut) {
403 /// a.put_array(&[cobia::CapeValueContent::String("methane".to_string()),cobia::CapeValueContent::Empty,cobia::CapeValueContent::Boolean(true)]).unwrap();
404 /// }
405 ///
406 /// let mut arr = cobia::CapeArrayValueVec::new();
407 /// set_content(&mut CapeArrayValueOutFromProvider::from(&mut arr).as_cape_array_value_out());
408 /// assert_eq!(arr.as_value_vec(), [cobia::CapeValueContent::String("methane".to_string()),cobia::CapeValueContent::Empty,cobia::CapeValueContent::Boolean(true)]); //the values have been stored on the object that implements ICapeArrayValue
409 /// ```
410
411 pub fn put_array(&mut self, array: &[CapeValueContent]) -> Result<(), COBIAError> {
412 let mut data: *mut *mut C::ICapeValue=std::ptr::null_mut();
413 let res = unsafe {
414 (*(**self.interface).vTbl).setsize.unwrap()(
415 (**self.interface).me,
416 array.len() as C::CapeSize,
417 &mut data,
418 )
419 };
420 if res == COBIAERR_NOERROR {
421 self.size = array.len() as C::CapeSize;
422 self.data=data;
423 for (i, s) in array.iter().enumerate() {
424 let p=unsafe { self.data.add(i as usize) };
425 if p.is_null() {
426 //this provided by the implementor of ICapeArrayValue and should not be null
427 return Err(COBIAError::Code(COBIAERR_NULLPOINTER));
428 }
429 let el = CapeValueOut::new(unsafe { &mut *p });
430 match s {
431 CapeValueContent::Empty => el.set_empty()?,
432 CapeValueContent::String(s) => el.set_string(s)?,
433 CapeValueContent::Integer(i) => el.set_integer(*i)?,
434 CapeValueContent::Boolean(b) => el.set_boolean(*b)?,
435 CapeValueContent::Real(r) => el.set_real(*r)?,
436 }
437 }
438 Ok(())
439 } else {
440 Err(COBIAError::Code(res))
441 }
442 }
443
444 /// Get an element
445 ///
446 /// # Arguments
447 ///
448 /// * `index` - The index of the element to get
449 ///
450 /// Note that neither Index and IndexMut is
451 /// provided for CapeArrayValueOut, because these interfaces
452 /// yield a reference to the element, whereas the elements
453 /// of CapeArrayValueOut are represented by an interface,
454 /// which is conveniently wrapped into CapeValueOut.
455 ///
456 /// Note that the life time of the CapeValueOut is tied to the
457 /// life time of the CapeArrayValueOut.
458 ///
459 /// # Examples
460 ///
461 /// ```
462 /// use cobia::*;
463 ///
464 /// fn test_element(a: &mut CapeArrayValueOut) {
465 /// assert_eq!(a.at(2).unwrap().get_boolean().unwrap(), true);
466 /// }
467 ///
468 /// let mut arr = cobia::CapeArrayValueVec::from_slice(&vec![cobia::CapeValueContent::String("methane".to_string()),cobia::CapeValueContent::Empty,cobia::CapeValueContent::Boolean(true)]);
469 /// test_element(&mut CapeArrayValueOutFromProvider::from(&mut arr).as_cape_array_value_out());
470 /// assert_eq!(arr.as_value_vec(), [cobia::CapeValueContent::String("methane".to_string()),cobia::CapeValueContent::Empty,cobia::CapeValueContent::Boolean(true)]); //the values have been stored on the object that implements ICapeArrayValue
471 /// ```
472
473 pub fn at(&self, index: usize) -> Result<CapeValueOut<'a>, COBIAError> {
474 if index >= self.size as usize {
475 return Err(COBIAError::Code(COBIAERR_NOSUCHITEM));
476 }
477 let p=unsafe { self.data.add(index) };
478 if p.is_null() {
479 //this provided by the implementor of ICapeArrayValue and should not be null
480 Err(COBIAError::Code(COBIAERR_NULLPOINTER))
481 } else {
482 Ok(CapeValueOut::new(unsafe { &mut *p} ))
483 }
484 }
485
486 /// Return the content of the value array as a vector of CapeValueContent
487 ///
488 /// # Examples
489 ///
490 /// ```
491 /// use cobia::*;
492 ///
493 /// fn test_content(a: &mut CapeArrayValueOut) {
494 /// assert_eq!(a.as_value_vec().unwrap(), vec![cobia::CapeValueContent::Real(2.4),cobia::CapeValueContent::Boolean(true)]);
495 /// }
496 ///
497 /// let mut arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::Real(2.4),cobia::CapeValueContent::Boolean(true)]);
498 /// test_content(&mut CapeArrayValueOutFromProvider::from(&mut arr).as_cape_array_value_out());
499 /// ```
500
501 pub fn as_value_vec(&self) -> Result<Vec<CapeValueContent>, COBIAError> {
502 let mut vec= Vec::new();
503 vec.reserve(self.size as usize);
504 for i in 0..self.size {
505 let p=unsafe { *self.data.add(i as usize) };
506 if p.is_null() {
507 //this provided by the implementor of ICapeArrayValue and should not be null
508 vec.push(CapeValueContent::Empty);
509 } else {
510 let val=CapeValueIn::new(&p);
511 match val.get_type() {
512 Ok(tp)=>
513 match tp {
514 CapeValueType::String => vec.push(CapeValueContent::String(
515 match val.get_string() {
516 Ok(s)=>s,
517 Err(e)=> {return Err(e);}
518 })),
519 CapeValueType::Integer => vec.push(CapeValueContent::Integer(
520 match val.get_integer() {
521 Ok(i)=>i,
522 Err(e)=> {return Err(e);}
523 })),
524 CapeValueType::Boolean => vec.push(CapeValueContent::Boolean(
525 match val.get_boolean() {
526 Ok(b)=>b,
527 Err(e)=> {return Err(e);}
528 })),
529 CapeValueType::Real => vec.push(CapeValueContent::Real(
530 match val.get_real() {
531 Ok(r)=>r,
532 Err(e)=> {return Err(e);}
533 })),
534 CapeValueType::Empty => vec.push(CapeValueContent::Empty),
535 },
536 Err(e)=> {return Err(e);}
537 }
538 }
539 }
540 Ok(vec)
541 }
542
543 /// Resize
544 ///
545 /// # Arguments
546 ///
547 /// * `size` - The new size of the array
548 ///
549 /// # Examples
550 ///
551 /// ```
552 /// use cobia::*;
553 ///
554 /// fn set_content(a: &mut CapeArrayValueOut) {
555 /// a.resize(3).unwrap();
556 /// a.at(0).unwrap().set_string("idealGasEnthalpy").unwrap();
557 /// a.at(1).unwrap().set_real(2.4).unwrap();
558 /// a.at(2).unwrap().set_integer(0).unwrap();
559 /// }
560 ///
561 /// let mut arr = cobia::CapeArrayValueVec::new();
562 /// set_content(&mut CapeArrayValueOutFromProvider::from(&mut arr).as_cape_array_value_out());
563 /// assert_eq!(arr.as_value_vec(), vec![cobia::CapeValueContent::String("idealGasEnthalpy".to_string()),cobia::CapeValueContent::Real(2.4),cobia::CapeValueContent::Integer(0)]); //the values have been stored on the object that implements ICapeArrayString
564 /// ```
565 pub fn resize(&mut self, size: usize) -> Result<(), COBIAError> {
566 let mut data:*mut *mut C::ICapeValue=std::ptr::null_mut();
567 let res = unsafe {
568 (*(**self.interface).vTbl).setsize.unwrap()(
569 (**self.interface).me,
570 size as C::CapeSize,
571 &mut data,
572 )
573 };
574 if res == COBIAERR_NOERROR {
575 self.size = size as C::CapeSize;
576 self.data=data;
577 Ok(())
578 } else {
579 Err(COBIAError::Code(res))
580 }
581 }
582
583 /// Set an element
584 ///
585 /// # Arguments
586 ///
587 /// * `index` - The index of the element to set
588 /// * `value` - The value to set
589 ///
590 /// # Examples
591 ///
592 /// ```
593 /// use cobia::*;
594 ///
595 /// fn set_element(a: &mut CapeArrayValueOut) {
596 /// a.put_value(1, cobia::CapeValueContent::String("density".to_string())).unwrap();
597 /// }
598 ///
599 /// let mut arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::String("methane".to_string()),cobia::CapeValueContent::Empty,cobia::CapeValueContent::Boolean(true)]);
600 /// set_element(&mut CapeArrayValueOutFromProvider::from(&mut arr).as_cape_array_value_out());
601 /// assert_eq!(arr.as_value_vec(), [cobia::CapeValueContent::String("methane".to_string()),cobia::CapeValueContent::String("density".to_string()),cobia::CapeValueContent::Boolean(true)]);
602 /// ```
603 pub fn put_value(&mut self, index: usize, value: CapeValueContent) -> Result<(), COBIAError> {
604 if index >= self.size as usize {
605 return Err(COBIAError::Code(COBIAERR_NOSUCHITEM));
606 }
607 let p=unsafe { self.data.add(index) };
608 if p.is_null() {
609 //this provided by the implementor of ICapeArrayValue and should not be null
610 return Err(COBIAError::Code(COBIAERR_NULLPOINTER));
611 }
612 let el = CapeValueOut::new(unsafe { &mut *p });
613 match value {
614 CapeValueContent::Empty => el.set_empty(),
615 CapeValueContent::String(value) => el.set_string(&value),
616 CapeValueContent::Integer(value) => el.set_integer(value),
617 CapeValueContent::Boolean(value) => el.set_boolean(value),
618 CapeValueContent::Real(value) => el.set_real(value),
619 }
620 }
621
622 /// Set the content of the value array from any object that implements CapeArrayValueProviderIn.
623 ///
624 /// # Arguments
625 /// * `array` - An object that implements CapeArrayValueProviderIn
626 ///
627 /// # Examples
628 ///
629 /// ```
630 /// use cobia::*;
631 /// let mut arr = cobia::CapeArrayValueVec::new();
632 /// let mut arr1 = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::Real(2.4),cobia::CapeValueContent::Boolean(true)]);
633 /// CapeArrayValueOutFromProvider::from(&mut arr).as_cape_array_value_out().set(&arr1);
634 /// assert_eq!(arr.as_value_vec(), vec![cobia::CapeValueContent::Real(2.4),cobia::CapeValueContent::Boolean(true)]);
635 /// ```
636
637 pub fn set<T:CapeArrayValueProviderIn>(&mut self,array:&T) -> Result<(), COBIAError> {
638 let mut value_array_in_from_provider = CapeArrayValueInFromProvider::from(array);
639 let value_array=value_array_in_from_provider.as_cape_array_value_in();
640 self.resize(value_array.size())?;
641 for i in 0..value_array.size() {
642 self.at(i)?.set(&value_array.at(i)?)?;
643 }
644 Ok(())
645 }
646
647}
648
649pub struct CapeArrayValueIterator<'a> {
650 arr: &'a CapeArrayValueOut<'a>,
651 index: usize,
652}
653
654impl<'a> Iterator for CapeArrayValueIterator<'a> {
655 type Item = CapeValueOut<'a>;
656
657 fn next(&mut self) -> Option<Self::Item> {
658 if self.index < self.arr.size as usize {
659 let res = self.arr.at(self.index);
660 self.index += 1;
661 Some(res.unwrap())
662 } else {
663 None
664 }
665 }
666}
667
668impl<'a> CapeArrayValueOut<'a> {
669 /// Return an iterator over the value array.
670 ///
671 /// # Examples
672 ///
673 /// ```
674 /// use cobia::*;
675 ///
676 /// fn check_iter(a: &mut CapeArrayValueOut) {
677 /// let mut iter = a.iter();
678 /// assert_eq!(iter.next().unwrap().get_string().unwrap(), "methane".to_string());
679 /// assert_eq!(iter.next().unwrap().get_type().unwrap(), cobia::CapeValueType::Empty);
680 /// assert_eq!(iter.next().unwrap().get_boolean().unwrap(), true);
681 /// assert!(!iter.next().is_some());
682 /// }
683 ///
684 /// let mut arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::String("methane".to_string()),cobia::CapeValueContent::Empty,cobia::CapeValueContent::Boolean(true)]);
685 /// check_iter(&mut CapeArrayValueOutFromProvider::from(&mut arr).as_cape_array_value_out());
686 /// ```
687
688 pub fn iter(&self) -> CapeArrayValueIterator<'_> {
689 CapeArrayValueIterator {
690 arr: &self,
691 index: 0,
692 }
693 }
694}
695
696impl<'a> fmt::Display for CapeArrayValueOut<'a> {
697 /// Display the content of the value array as a value vector.
698 ///
699 /// # Examples
700 ///
701 /// ```
702 /// use cobia::*;
703 ///
704 /// fn check_format(a: &mut CapeArrayValueOut) {
705 /// assert_eq!(format!("{}", a), "[\"methane\", <empty>, true]");
706 /// }
707 ///
708 /// let mut arr = cobia::CapeArrayValueVec::from_slice(&[cobia::CapeValueContent::String("methane".to_string()),cobia::CapeValueContent::Empty,cobia::CapeValueContent::Boolean(true)]);
709 /// check_format(&mut CapeArrayValueOutFromProvider::from(&mut arr).as_cape_array_value_out());
710 /// ```
711
712 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
713 write!(f, "[")?;
714 for (count, v) in self.iter().enumerate() {
715 if count != 0 {
716 write!(f, ", ")?;
717 }
718 write!(f, "{}", v)?;
719 }
720 write!(f, "]")
721 }
722}
723
724impl<'a> CapeArrayValueProviderOut for CapeArrayValueOut<'a> {
725 fn as_cape_array_value_out(&mut self) -> C::ICapeArrayValue {
726 unsafe { **self.interface }
727 }
728}