cobia/cape_array_slice.rs
1use crate::C;
2use crate::*;
3
4/// Slice based implementation of ICapeArray
5///
6/// Base class for several slice based ICapeArray implementations.
7///
8/// Any ICapeArray (e.g. ICapeArrayReal) is passed as data container
9/// between CAPE-OPEN functions. It is up to the caller to provide the
10/// interface, and its implementation. This class provides a default
11/// impementation, that uses a reference to the slice its data
12///
13/// These data types can only be used as input argument, not as output
14/// argument, as the reference to the slice is non mutable.
15///
16/// # Examples
17///
18/// ```
19/// use cobia::*;
20///
21/// fn test_content(a: &CapeArrayRealIn) {
22/// assert_eq!(a.as_slice(), &[2.5,4.5]);
23/// }
24///
25/// let arr = cobia::CapeArrayRealSlice::new(&[2.5,4.5]);
26/// test_content(&CapeArrayRealInFromProvider::from(&arr).as_cape_array_real_in());
27/// ```
28
29#[allow(private_bounds)]
30pub struct CapeArraySlice<'a,Element:Copy+Clone> {
31 slice: &'a [Element],
32}
33
34#[allow(private_bounds)]
35impl<'a,Element:Copy+Clone> CapeArraySlice<'a,Element> {
36
37 /// New from slice
38 ///
39 /// Creates a new CapeArraySlice from a slice.
40 ///
41 /// # Arguments
42 ///
43 /// * `slice` - A vector or slice or slice of values to be used as CapeArraySlice - values are referenced
44 ///
45 /// # Examples
46 ///
47 /// ```
48 /// use cobia;
49 /// use cobia::prelude::*;
50 /// let arr = cobia::CapeArrayRealSlice::new(&[2.5,4.5]);
51 /// assert_eq!(arr.as_slice(), &vec![2.5,4.5]);
52 /// ```
53
54 pub fn new(slice: &'a [Element]) -> Self {
55 CapeArraySlice {
56 slice,
57 }
58 }
59
60 /// Return a slice
61 ///
62 /// Returns a reference to the slice of type T.
63 ///
64 /// # Example
65 ///
66 /// ```
67 /// use cobia;
68 /// use cobia::prelude::*;
69 /// let arr = cobia::CapeArrayRealSlice::new(&[2.5,4.5]);
70 /// assert_eq!(arr.as_slice(), &vec![2.5,4.5]);
71 /// ```
72
73 pub fn as_slice(&self) -> &'a [Element] {
74 &self.slice
75 }
76
77 /// Get size
78 ///
79 /// Returns the size of the slice.
80 ///
81 /// # Examples
82 ///
83 /// ```
84 /// use cobia;
85 /// use cobia::prelude::*;
86 /// let arr = cobia::CapeArrayRealSlice::new(&[2.5,4.5]);
87 /// assert_eq!(arr.size(), 2);
88 /// ```
89
90 pub fn size(&self) -> usize {
91 self.as_slice().len()
92 }
93
94}
95
96impl<'a,Element:Copy+Clone> AsRef<[Element]> for CapeArraySlice<'a,Element> {
97
98 /// Return a reference to the slice
99 ///
100 /// Returns a reference to the slice `[Element]`.
101 fn as_ref(&self) -> &[Element] {
102 self.slice
103 }
104}
105
106
107/// Slice based CapeArrayRealIn implementation
108///
109/// ICapeArrayReal is passed as data container between CAPE-OPEN functions.
110/// It is up to the caller to provide the interface, and its implementation.
111/// This class provides a default impementation using a `&[CapeReal]`.
112///
113/// # Examples
114///
115/// ```
116/// use cobia::*;
117///
118/// fn test_content(a: &CapeArrayRealIn) {
119/// assert_eq!(a.as_slice(), &[2.5,4.5]);
120/// }
121///
122/// let arr = cobia::CapeArrayRealSlice::new(&[2.5,4.5]);
123/// test_content(&CapeArrayRealInFromProvider::from(&arr).as_cape_array_real_in());
124/// ```
125pub type CapeArrayRealSlice<'a> = CapeArraySlice<'a,CapeReal>;
126
127impl<'a> CapeArrayRealSlice<'a> {
128
129 /// Interface member function
130
131 extern "C" fn get(
132 me: *mut ::std::os::raw::c_void,
133 data: *mut *mut CapeReal,
134 size: *mut C::CapeSize,
135 ) {
136 let p = me as *mut Self;
137 let arr: &mut Self = unsafe { &mut *p };
138 unsafe {
139 *data = arr.slice.as_ptr() as *mut CapeReal;
140 *size = arr.slice.len() as C::CapeSize;
141 }
142 }
143
144 /// Interface member function
145
146 extern "C" fn setsize(
147 _: *mut ::std::os::raw::c_void,
148 _: C::CapeSize,
149 _: *mut *mut CapeReal,
150 ) -> CapeResult {
151 COBIAERR_DENIED //this is an input argument, read only
152 }
153
154 /// Interface v-table
155
156 const VTABLE: C::ICapeArrayReal_VTable = C::ICapeArrayReal_VTable {
157 get: Some(Self::get),
158 setsize: Some(Self::setsize),
159 };
160
161}
162
163impl<'a> CapeArrayRealProviderIn for CapeArrayRealSlice<'a> {
164 /// Convert to ICapeArrayReal
165 ///
166 /// Returns a reference to the ICapeArrayReal interface.
167 ///
168 /// # Examples
169 ///
170 /// ```
171 /// use cobia::*;
172 /// use cobia::prelude::*;
173 /// let arr = cobia::CapeArrayRealSlice::new(&[2.5,4.5]);
174 /// let mut i_cape_array_real=arr.as_cape_array_real_in();
175 /// let i_cape_array_real_ptr=(&i_cape_array_real as *const C::ICapeArrayReal).cast_mut(); //normally a pointer to the interface is received
176 /// let a = cobia::CapeArrayRealIn::new(&i_cape_array_real_ptr); //CapeArrayRealIn from *mut C::ICapeArrayReal
177 /// assert_eq!(a.as_vec(), vec![2.5,4.5]);
178 /// ```
179
180 fn as_cape_array_real_in(&self) -> C::ICapeArrayReal {
181 C::ICapeArrayReal {
182 me: (self as *const Self).cast_mut() as *mut ::std::os::raw::c_void,
183 vTbl: (&CapeArrayRealSlice::VTABLE as *const C::ICapeArrayReal_VTable).cast_mut(),
184 }
185 }
186}
187
188/// Slice based CapeArrayIntegerIn implementation
189///
190/// ICapeArrayInteger is passed as data container between CAPE-OPEN functions.
191/// It is up to the caller to provide the interface, and its implementation.
192/// This class provides a default impementation using a `&[CapeInteger]`
193///
194/// # Examples
195///
196///
197/// ```
198/// use cobia::*;
199///
200/// fn test_content(a: &CapeArrayIntegerIn) {
201/// assert_eq!(a.as_vec(), vec![2,4]);
202/// }
203///
204/// let arr = cobia::CapeArrayIntegerSlice::new(&[2,4]);
205/// test_content(&CapeArrayIntegerInFromProvider::from(&arr).as_cape_array_integer_in());
206/// ```
207pub type CapeArrayIntegerSlice<'a> = CapeArraySlice<'a,CapeInteger>;
208
209impl<'a> CapeArrayIntegerSlice<'a> {
210
211 /// Interface member function
212
213 extern "C" fn get(
214 me: *mut ::std::os::raw::c_void,
215 data: *mut *mut CapeInteger,
216 size: *mut C::CapeSize,
217 ) {
218 let p = me as *mut Self;
219 let arr: &mut Self = unsafe { &mut *p };
220 unsafe {
221 *data = arr.slice.as_ptr() as *mut CapeInteger;
222 *size = arr.slice.len() as C::CapeSize;
223 }
224 }
225
226 /// Interface member function
227
228 extern "C" fn setsize(
229 _: *mut ::std::os::raw::c_void,
230 _: C::CapeSize,
231 _: *mut *mut CapeInteger,
232 ) -> CapeResult {
233 COBIAERR_DENIED //this is an input argument, read only
234 }
235
236 /// Interface v-table
237
238 const VTABLE: C::ICapeArrayInteger_VTable = C::ICapeArrayInteger_VTable {
239 get: Some(Self::get),
240 setsize: Some(Self::setsize),
241 };
242
243}
244
245impl<'a> CapeArrayIntegerProviderIn for CapeArrayIntegerSlice<'a> {
246 /// Convert to ICapeArrayInteger
247 ///
248 /// Returns a reference to the ICapeArrayInteger interface.
249 ///
250 /// # Examples
251 ///
252 /// ```
253 /// use cobia::*;
254 /// use cobia::prelude::*;
255 /// let arr = cobia::CapeArrayIntegerSlice::new(&[9,10,2]);
256 /// let i_cape_array_integer=arr.as_cape_array_integer_in();
257 /// let mut i_cape_array_integer_ptr=(&i_cape_array_integer as *const C::ICapeArrayInteger).cast_mut(); //normally a pointer to the interface is received
258 /// let a = cobia::CapeArrayIntegerIn::new(&mut i_cape_array_integer_ptr); //CapeArrayIntegerIn from *mut C::ICapeArrayInteger
259 /// assert_eq!(a.as_vec(), vec![9,10,2]);
260 /// ```
261
262 fn as_cape_array_integer_in(&self) -> C::ICapeArrayInteger {
263 C::ICapeArrayInteger {
264 me: (self as *const Self).cast_mut() as *mut ::std::os::raw::c_void,
265 vTbl: (&CapeArrayIntegerSlice::VTABLE as *const C::ICapeArrayInteger_VTable).cast_mut(),
266 }
267 }
268}
269
270/// Slice based CapeArrayByteIn implementation
271///
272/// ICapeArrayByte is passed as data container between CAPE-OPEN functions.
273/// It is up to the caller to provide the interface, and its implementation.
274/// This class provides a default impementation using a `&[CapeByte]`.
275///
276/// # Examples
277///
278///
279/// ```
280/// use cobia::*;
281///
282/// fn test_content(a: &CapeArrayByteIn) {
283/// assert_eq!(a.as_vec(), vec![2u8,4u8]);
284/// }
285///
286/// let arr = cobia::CapeArrayByteSlice::new(&[2u8,4u8]);
287/// test_content(&CapeArrayByteInFromProvider::from(&arr).as_cape_array_byte_in());
288/// ```
289pub type CapeArrayByteSlice<'a> = CapeArraySlice<'a,CapeByte>;
290
291impl<'a> CapeArrayByteSlice<'a> {
292
293 /// Interface member function
294
295 extern "C" fn get(
296 me: *mut ::std::os::raw::c_void,
297 data: *mut *mut CapeByte,
298 size: *mut C::CapeSize,
299 ) {
300 let p = me as *mut Self;
301 let arr: &mut Self = unsafe { &mut *p };
302 unsafe {
303 *data = arr.slice.as_ptr() as *mut CapeByte;
304 *size = arr.slice.len() as C::CapeSize;
305 }
306 }
307
308 /// Interface member function
309
310 extern "C" fn setsize(
311 _: *mut ::std::os::raw::c_void,
312 _: C::CapeSize,
313 _: *mut *mut CapeByte,
314 ) -> C::CapeResult {
315 COBIAERR_DENIED //this is an input argument, read only
316 }
317
318 /// Interface v-table
319
320 const VTABLE: C::ICapeArrayByte_VTable = C::ICapeArrayByte_VTable {
321 get: Some(Self::get),
322 setsize: Some(Self::setsize),
323 };
324
325}
326
327impl<'a> CapeArrayByteProviderIn for CapeArrayByteSlice<'a> {
328 /// Convert to ICapeArrayByte
329 ///
330 /// Returns a reference to the ICapeArrayByte interface.
331 ///
332 /// # Examples
333 ///
334 /// ```
335 /// use cobia::*;
336 /// use cobia::prelude::*;
337 /// let arr = cobia::CapeArrayByteSlice::new(&[9u8,10u8,2u8]);
338 /// let i_cape_array_byte=arr.as_cape_array_byte_in();
339 /// let mut i_cape_array_byte_ptr=(&i_cape_array_byte as *const C::ICapeArrayByte).cast_mut(); //normally a pointer to the interface is received
340 /// let a = cobia::CapeArrayByteIn::new(&mut i_cape_array_byte_ptr); //CapeArrayByteIn from *mut C::ICapeArrayByte
341 /// assert_eq!(a.as_vec(), vec![9u8,10u8,2u8]);
342 /// ```
343
344 fn as_cape_array_byte_in(&self) -> C::ICapeArrayByte {
345 C::ICapeArrayByte {
346 me: (self as *const Self).cast_mut() as *mut ::std::os::raw::c_void,
347 vTbl: (&CapeArrayByteSlice::VTABLE as *const C::ICapeArrayByte_VTable).cast_mut(),
348 }
349 }
350}
351
352/// Slice based CapeArrayBooleanIn implementation
353///
354/// ICapeArrayBoolean is passed as data container between CAPE-OPEN functions.
355/// It is up to the caller to provide the interface, and its implementation.
356/// This class provides a default impementation using a `&[CapeBoolean]`.
357///
358/// # Examples
359///
360///
361/// ```
362/// use cobia::*;
363///
364/// fn test_content(a: &CapeArrayBooleanIn) {
365/// assert_eq!(a.as_bool_vec(), vec![true,false]);
366/// }
367///
368/// let arr = cobia::CapeArrayBooleanSlice::new(&[1,0]);
369/// test_content(&CapeArrayBooleanInFromProvider::from(&arr).as_cape_array_boolean_in());
370/// ```
371pub type CapeArrayBooleanSlice<'a> = CapeArraySlice<'a,CapeBoolean>;
372
373impl<'a> CapeArrayBooleanSlice<'a> {
374
375 /// Interface member function
376
377 extern "C" fn get(
378 me: *mut ::std::os::raw::c_void,
379 data: *mut *mut CapeBoolean,
380 size: *mut C::CapeSize,
381 ) {
382 let p = me as *mut Self;
383 let arr: &mut Self = unsafe { &mut *p };
384 unsafe {
385 *data = arr.slice.as_ptr() as *mut CapeBoolean;
386 *size = arr.slice.len() as C::CapeSize;
387 }
388 }
389
390 /// Interface member function
391
392 extern "C" fn setsize(
393 _: *mut ::std::os::raw::c_void,
394 _: C::CapeSize,
395 _: *mut *mut CapeBoolean,
396 ) -> CapeResult {
397 COBIAERR_DENIED //this is an input argument, read only
398 }
399
400 /// Interface v-table
401
402 const VTABLE: C::ICapeArrayBoolean_VTable = C::ICapeArrayBoolean_VTable {
403 get: Some(Self::get),
404 setsize: Some(Self::setsize),
405 };
406
407}
408
409impl<'a> CapeArrayBooleanProviderIn for CapeArrayBooleanSlice<'a> {
410 /// Convert to ICapeArrayBoolean
411 ///
412 /// Returns a reference to the ICapeArrayBoolean interface.
413 ///
414 /// # Examples
415 ///
416 /// ```
417 /// use cobia::*;
418 /// use cobia::prelude::*;
419 /// let arr = cobia::CapeArrayBooleanSlice::new(&[0,1,0]);
420 /// let i_cape_array_boolean=arr.as_cape_array_boolean_in();
421 /// let mut i_cape_array_boolean_ptr=(&i_cape_array_boolean as *const C::ICapeArrayBoolean).cast_mut(); //normally a pointer to the interface is received
422 /// let a = cobia::CapeArrayBooleanIn::new(&mut i_cape_array_boolean_ptr); //CapeArrayBooleanIn from *mut C::ICapeArrayBoolean
423 /// assert_eq!(a.as_bool_vec(), vec![false,true,false]);
424 /// ```
425
426 fn as_cape_array_boolean_in(&self) -> C::ICapeArrayBoolean {
427 C::ICapeArrayBoolean {
428 me: (self as *const Self).cast_mut() as *mut ::std::os::raw::c_void,
429 vTbl: (&CapeArrayBooleanSlice::VTABLE as *const C::ICapeArrayBoolean_VTable).cast_mut(),
430 }
431 }
432}
433
434/// Slice based CapeArrayEnumerationIn implementation
435///
436/// ICapeArrayEnmeration is passed as data container between CAPE-OPEN functions.
437/// It is up to the caller to provide the interface, and its implementation.
438/// This class provides a default impementation using a `&[Element]`.
439///
440/// All CAPE-OPEN and COBIA enumeration types are represented as CapeEnumeration
441///
442/// # Examples
443///
444/// ```
445/// use cobia::*;
446///
447/// fn test_content(a: &CapeArrayEnumerationIn<CapePMCServiceType>) {
448/// assert_eq!(a.as_vec(), vec![CapePMCServiceType::Inproc64,CapePMCServiceType::COM64]);
449/// }
450///
451/// let arr = cobia::CapeArrayEnumerationSlice::new(&[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
452/// test_content(&CapeArrayEnumerationInFromProvider::from(&arr).as_cape_array_enumeration_in());
453/// ```
454
455pub type CapeArrayEnumerationSlice<'a,Element> = CapeArraySlice<'a,Element>;
456
457type CapeArrayEnumerationSliceRaw<'a> = CapeArraySlice<'a,C::CapeEnumeration>;
458
459impl<'a> CapeArrayEnumerationSliceRaw<'a> {
460
461 /// Interface member function
462
463 extern "C" fn get_raw(
464 me: *mut ::std::os::raw::c_void,
465 data: *mut *mut C::CapeEnumeration,
466 size: *mut C::CapeSize,
467 ) {
468 let p = me as *mut Self;
469 let arr: &mut Self = unsafe { &mut *p };
470 unsafe {
471 *data = arr.slice.as_ptr() as *mut C::CapeEnumeration;
472 *size = arr.slice.len() as C::CapeSize;
473 }
474 }
475
476 /// Interface member function
477
478 extern "C" fn setsize_raw(
479 _: *mut ::std::os::raw::c_void,
480 _: C::CapeSize,
481 _: *mut *mut C::CapeEnumeration,
482 ) -> CapeResult {
483 COBIAERR_DENIED //this is an input argument, read only
484 }
485
486 /// Interface v-table
487
488 const VTABLE_RAW: C::ICapeArrayEnumeration_VTable = C::ICapeArrayEnumeration_VTable {
489 get: Some(Self::get_raw),
490 setsize: Some(Self::setsize_raw),
491 };
492
493}
494
495impl<'a,Element:Copy+Clone> CapeArrayEnumerationProviderIn for CapeArrayEnumerationSlice<'a,Element> {
496 /// Convert to ICapeArrayEnumeration
497 ///
498 /// Returns a reference to the ICapeArrayEnumeration interface.
499 ///
500 /// # Examples
501 ///
502 /// ```
503 /// use cobia::*;
504 /// use cobia::prelude::*;
505 /// let arr = cobia::CapeArrayEnumerationSlice::new(&[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
506 /// let i_cape_array_enumeration=arr.as_cape_array_enumeration_in();
507 /// let mut i_cape_array_enumeration_ptr=(&i_cape_array_enumeration as *const C::ICapeArrayEnumeration).cast_mut(); //normally a pointer to the interface is received
508 /// let a = cobia::CapeArrayEnumerationIn::<cobia::CapePMCServiceType>::new(&mut i_cape_array_enumeration_ptr); //CapeArrayEnumerationIn from *mut C::ICapeArrayEnumeration
509 /// assert_eq!(a.as_vec(), vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
510 /// ```
511
512 fn as_cape_array_enumeration_in(&self) -> C::ICapeArrayEnumeration {
513 C::ICapeArrayEnumeration {
514 me: (self as *const Self).cast_mut() as *mut ::std::os::raw::c_void,
515 vTbl: (&CapeArrayEnumerationSliceRaw::VTABLE_RAW as *const C::ICapeArrayEnumeration_VTable).cast_mut(),
516 }
517 }
518}