cobia/
cape_array_boolean.rs

1use crate::{C,COBIAError,CapeArrayBooleanInFromProvider};
2use crate::cape_array::{ArrayInterface,CapeArrayIn,CapeArrayOut};
3use crate::{CapeArrayBooleanProviderIn,CapeArrayBooleanProviderOut};
4
5impl ArrayInterface<C::CapeBoolean> for C::ICapeArrayBoolean {
6
7	fn get(&mut self,data:&mut *mut C::CapeBoolean,size:&mut C::CapeSize) {
8		unsafe { (*self.vTbl).get.unwrap()(self.me,data as *mut *mut C::CapeBoolean,size) };
9	}
10
11	fn set(&mut self,data:&mut *mut C::CapeBoolean,size:C::CapeSize) -> C::CapeResult {
12		unsafe { (*self.vTbl).setsize.unwrap()(self.me,size,data as *mut *mut C::CapeBoolean) }
13	}
14
15	fn get_const(&self,data:&mut *const C::CapeBoolean,size:&mut C::CapeSize) {
16		unsafe { (*self.vTbl).get.unwrap()(self.me,data as *mut *const C::CapeBoolean as *mut *mut C::CapeBoolean,size) };
17	}
18}
19
20/// CapeArrayBooleanIn wraps an ICapeArrayBoolean interface pointer.
21///
22/// Given an ICapeArrayBoolean interface pointer, this allows setting
23///  and getting the elements.
24///
25/// This interface is typically used as arguments to rust methods
26/// on traits that are generated from CAPE-OPEN interfaces that have
27/// ICapeArrayBoolean input arguments.
28///
29/// # Examples
30///
31/// ```
32/// use cobia::*;
33///
34/// fn test_content(a: &CapeArrayBooleanIn) {
35///     assert_eq!(a.as_bool_vec(), vec![false,true,true,false]);
36/// }
37/// 
38/// let arr = cobia::CapeArrayBooleanVec::from_bool_slice(&[false,true,true,false]);
39/// test_content(&CapeArrayBooleanInFromProvider::from(&arr).as_cape_array_boolean_in())
40/// ```
41
42pub type CapeArrayBooleanIn<'a> = CapeArrayIn<'a,C::CapeBoolean,C::ICapeArrayBoolean>;
43
44impl<'a> CapeArrayBooleanIn<'a> {
45
46	/// Returns the elements of the array as a `Vec<bool>`.
47	///
48	/// # Examples
49	///
50	/// ```
51	/// use cobia::*;
52	///
53	/// fn test_content(a: &CapeArrayBooleanIn) {
54	///     assert_eq!(a.as_bool_vec(), vec![false,true,true,false]);
55	/// }
56	/// 
57	/// let arr = cobia::CapeArrayBooleanVec::from_bool_slice(&[false,true,true,false]);
58	/// test_content(&CapeArrayBooleanInFromProvider::from(&arr).as_cape_array_boolean_in())
59	/// ```
60
61	pub fn as_bool_vec(&self) -> Vec<bool> {
62		let vec=self.as_vec();
63		let mut v = Vec::with_capacity(vec.len());
64		for (i,_) in vec.iter().enumerate() {
65			v.push(vec[i]!=0);
66		}
67		v
68	}
69
70}
71
72impl<'a> CapeArrayBooleanProviderIn for CapeArrayBooleanIn<'a> {
73	fn as_cape_array_boolean_in(&self) -> C::ICapeArrayBoolean {
74		unsafe{**self.interface}
75	}
76}
77
78/// CapeArrayBooleanOut wraps an ICapeArrayBoolean interface pointer.
79///
80/// Given an ICapeArrayBoolean interface pointer, this allows setting
81///  and getting the elements.
82///
83/// This interface is typically used as arguments to rust methods
84/// on traits that are generated from CAPE-OPEN interfaces that have
85/// ICapeArrayBoolean output arguments.
86///
87/// # Examples
88///
89/// ```
90/// use cobia::*;
91///
92/// fn set_content(a: &mut CapeArrayBooleanOut) {
93///		a.put_array(&[false as CapeBoolean,true as CapeBoolean,true as CapeBoolean,false as CapeBoolean]).unwrap();
94/// }
95/// 
96/// let mut arr = cobia::CapeArrayBooleanVec::new();
97/// set_content(&mut CapeArrayBooleanOutFromProvider::from(&mut arr).as_cape_array_boolean_out());
98/// assert_eq!(arr.as_vec(), &vec![false as CapeBoolean,true as CapeBoolean,true as CapeBoolean,false as CapeBoolean]);
99/// ```
100
101pub type CapeArrayBooleanOut<'a> = CapeArrayOut<'a,C::CapeBoolean,C::ICapeArrayBoolean>;
102
103impl<'a> CapeArrayBooleanOut<'a> {
104
105	/// Returns the elements of the array as a `Vec<bool>`.
106	///
107	/// # Examples
108	///
109	/// ```
110	/// use cobia::*;
111	///
112	/// fn test_content(a: CapeArrayBooleanOut) {
113	///     assert_eq!(a.as_bool_vec(), vec![false,true,true,false]);
114	/// }
115	/// 
116	/// let mut arr = cobia::CapeArrayBooleanVec::from_bool_slice(&[false,true,true,false]);
117	/// test_content(CapeArrayBooleanOutFromProvider::from(&mut arr).as_cape_array_boolean_out())
118	/// ```
119
120	pub fn as_bool_vec(&self) -> Vec<bool> {
121		let vec=self.as_vec();
122		let mut v = Vec::with_capacity(vec.len());
123		for (i,_) in vec.iter().enumerate() {
124			v.push(vec[i]!=0);
125		}
126		v
127	}
128
129	/// Set the content of the boolean array from any object that implements CapeArrayBooleanProviderIn.
130	///
131	/// # Arguments
132	/// * `array` - An object that implements CapeArrayBooleanProviderIn
133	///
134	/// # Examples
135	///
136	/// ```
137	/// use cobia::*;
138	/// fn set_content(a: &mut CapeArrayBooleanOut, b: &CapeArrayBooleanIn) {
139	///		a.put_array(&[false as CapeBoolean,true as CapeBoolean,true as CapeBoolean,false as CapeBoolean]).unwrap();
140	/// }
141	/// 
142	/// let mut arr = cobia::CapeArrayBooleanVec::new();
143	/// let arr1 = cobia::CapeArrayBooleanVec::from_bool_slice(&[false,true,true,false]);
144	/// set_content(&mut CapeArrayBooleanOutFromProvider::from(&mut arr).as_cape_array_boolean_out(),&CapeArrayBooleanInFromProvider::from(&arr1).as_cape_array_boolean_in());
145	/// assert_eq!(arr.as_vec(), &[false as CapeBoolean,true as CapeBoolean,true as CapeBoolean,false as CapeBoolean]);
146	/// ```
147
148	pub fn set<T:CapeArrayBooleanProviderIn>(&mut self,array:&T) -> Result<(), COBIAError> {
149		let mut boolean_array_in_from_provider = CapeArrayBooleanInFromProvider::from(array);
150		let boolean_array=boolean_array_in_from_provider.as_cape_array_boolean_in();
151		self.resize(boolean_array.size())?;
152		for i in 0..boolean_array.size() {
153			self[i]=boolean_array[i];
154		}
155		Ok(())
156	}
157}
158
159impl<'a> CapeArrayBooleanProviderOut for CapeArrayBooleanOut<'a> {
160	fn as_cape_array_boolean_out(&mut self) -> C::ICapeArrayBoolean {
161		unsafe { **self.interface }
162	}
163}