cobia/cape_value_impl.rs
1use crate::C;
2use crate::*;
3
4/// CapeValueImpl content
5#[derive(Debug,PartialEq,Clone)]
6pub enum CapeValueContent {
7 Empty,
8 String(std::string::String),
9 Integer(CapeInteger),
10 Boolean(bool),
11 Real(CapeReal),
12}
13
14/// Default CapeValue implementation
15///
16/// ICapeValue is passed as data container between CAPE-OPEN functions.
17/// It is up to the caller to provide the interface, and its implementation.
18/// This class provides a default impementation.
19///
20/// This class can be used as input and output argument.
21///
22/// # Examples
23///
24/// ```
25/// use cobia::*;
26///
27/// fn set_value(v: &mut CapeValueOut) {
28/// v.set_string("my value").unwrap();
29/// }
30///
31/// let mut val = cobia::CapeValueImpl::new();
32/// set_value(&mut CapeValueOutFromProvider::from(&mut val).as_cape_value_out());
33/// assert_eq!(val.value(), CapeValueContent::String("my value".into()));
34/// ```
35
36#[derive (Clone)]
37pub struct CapeValueImpl {
38 value: CapeValueContent,
39 str_val: CapeStringImpl, //used to internally store string values, for which the pointer is returned to the external caller
40}
41
42impl CapeValueImpl {
43
44 /// Create a new, empty, CapeValueImpl
45 ///
46 /// Creates a new empty CapeValueImpl
47 ///
48 /// # Examples
49 ///
50 /// ```
51 /// use cobia::*;
52 ///
53 /// fn set_value(v: &mut CapeValueOut) {
54 /// v.set_string("my value").unwrap();
55 /// }
56 ///
57 /// let mut val = cobia::CapeValueImpl::new();
58 /// set_value(&mut CapeValueOutFromProvider::from(&mut val).as_cape_value_out());
59 /// assert_eq!(val.value(), CapeValueContent::String("my value".into()));
60 /// ```
61 pub fn new() -> Self {
62 Self {
63 value: CapeValueContent::Empty,
64 str_val: CapeStringImpl::new()
65 }
66 }
67
68 /// Create a new CapeValueImpl from string
69 ///
70 /// Creates a new CapeValueImpl from the given string
71 ///
72 /// # Arguments
73 ///
74 /// * `value` - The initial value
75 ///
76 /// # Examples
77 ///
78 /// ```
79 /// use cobia::*;
80 ///
81 /// fn check_value(v: &CapeValueIn) {
82 /// assert_eq!(v.get_string().unwrap(), "C2H6".to_string());
83 /// }
84 ///
85 /// let val = cobia::CapeValueImpl::from_str("C2H6");
86 /// check_value(&CapeValueInFromProvider::from(&val).as_cape_value_in());
87 /// ```
88
89 pub fn from_str(value: &str) -> Self {
90 Self {
91 value: CapeValueContent::String(value.to_string()),
92 str_val: CapeStringImpl::new()
93 }
94 }
95
96 /// Create a new CapeValueImpl from string
97 ///
98 /// Creates a new CapeValueImpl from the given string
99 ///
100 /// # Arguments
101 ///
102 /// * `value` - The initial value
103 ///
104 /// # Examples
105 ///
106 /// ```
107 /// use cobia::*;
108 ///
109 /// fn check_value(v: &CapeValueIn) {
110 /// assert_eq!(v.get_string().unwrap(), "C2H6".to_string());
111 /// }
112 ///
113 /// let val = cobia::CapeValueImpl::from_string("C2H6".to_string());
114 /// check_value(&CapeValueInFromProvider::from(&val).as_cape_value_in());
115 /// ```
116
117 pub fn from_string(value: String) -> Self {
118 Self {
119 value: CapeValueContent::String(value),
120 str_val: CapeStringImpl::new()
121 }
122 }
123
124 /// Create a new CapeValueImpl from content
125 ///
126 /// Creates a new CapeValueImpl from the given CapeValueContent
127 ///
128 /// # Arguments
129 ///
130 /// * `value` - The initial value
131 ///
132 /// # Examples
133 ///
134 /// ```
135 /// use cobia::*;
136 ///
137 /// fn check_value(v: &CapeValueIn) {
138 /// assert_eq!(v.get_string().unwrap(), "n-butane".to_string());
139 /// }
140 ///
141 /// let val = cobia::CapeValueImpl::from_content(cobia::CapeValueContent::String("n-butane".into()));
142 /// check_value(&CapeValueInFromProvider::from(&val).as_cape_value_in());
143 /// ```
144
145 pub fn from_content(value: CapeValueContent) -> Self {
146 Self {
147 value,
148 str_val: CapeStringImpl::new()
149 }
150 }
151
152 /// Create a new CapeValueImpl from integer
153 ///
154 /// Creates a new CapeValueImpl from the given integer
155 ///
156 /// # Arguments
157 ///
158 /// * `value` - The initial value
159 ///
160 /// # Examples
161 ///
162 /// ```
163 /// use cobia::*;
164 ///
165 /// fn check_value(v: &CapeValueIn) {
166 /// assert_eq!(v.get_integer().unwrap(),5);
167 /// }
168 ///
169 /// let val = cobia::CapeValueImpl::from_integer(5);
170 /// check_value(&CapeValueInFromProvider::from(&val).as_cape_value_in());
171 /// ```
172
173 pub fn from_integer(value: CapeInteger) -> Self {
174 Self {
175 value: CapeValueContent::Integer(value),
176 str_val: CapeStringImpl::new()
177 }
178 }
179
180 /// Create a new CapeValueImpl from boolean
181 ///
182 /// Creates a new CapeValueImpl from the given boolean
183 ///
184 /// # Arguments
185 ///
186 /// * `value` - The initial value
187 ///
188 /// # Examples
189 ///
190 /// ```
191 /// use cobia::*;
192 ///
193 /// fn check_value(v: &CapeValueIn) {
194 /// assert_eq!(v.get_boolean().unwrap(),false);
195 /// }
196 ///
197 /// let val = cobia::CapeValueImpl::from_boolean(false);
198 /// check_value(&CapeValueInFromProvider::from(&val).as_cape_value_in());
199 /// ```
200
201 pub fn from_boolean(value: bool) -> Self {
202 Self {
203 value: CapeValueContent::Boolean(value),
204 str_val: CapeStringImpl::new()
205 }
206 }
207
208 /// Create a new CapeValueImpl from real
209 ///
210 /// Creates a new CapeValueImpl from the given real
211 ///
212 /// # Arguments
213 ///
214 /// * `value` - The initial value
215 ///
216 /// # Examples
217 ///
218 /// ```
219 /// use cobia::*;
220 ///
221 /// fn check_value(v: &CapeValueIn) {
222 /// assert_eq!(v.get_real().unwrap(),3.0);
223 /// }
224 ///
225 /// let val = cobia::CapeValueImpl::from_real(3.0);
226 /// check_value(&CapeValueInFromProvider::from(&val).as_cape_value_in());
227 /// ```
228
229 pub fn from_real(value: CapeReal) -> Self {
230 Self {
231 value: CapeValueContent::Real(value),
232 str_val: CapeStringImpl::new()
233 }
234 }
235
236 /// Set to empty
237 ///
238 /// Change the type to empty
239 ///
240 /// # Examples
241 ///
242 /// ```
243 /// use cobia::*;
244 /// let mut val = cobia::CapeValueImpl::from_real(3.0);
245 /// val.reset();
246 /// assert_eq!(val.value(), CapeValueContent::Empty);
247 /// ```
248 pub fn reset(&mut self) {
249 self.value=CapeValueContent::Empty;
250 }
251
252 /// Set to string
253 ///
254 /// Change the type to the given string
255 ///
256 /// # Arguments
257 ///
258 /// * `value` - The new value
259 ///
260 /// # Examples
261 ///
262 /// ```
263 /// use cobia::*;
264 /// let mut val = cobia::CapeValueImpl::new();
265 /// val.set_string("H2O".into());
266 /// assert_eq!(val.value(), CapeValueContent::String("H2O".into()));
267 /// ```
268
269 pub fn set_string(&mut self,value: String) {
270 self.value = CapeValueContent::String(value);
271 }
272
273 /// Set to string
274 ///
275 /// Change the type to the given string
276 ///
277 /// # Arguments
278 ///
279 /// * `value` - The new value
280 ///
281 /// # Examples
282 ///
283 /// ```
284 /// use cobia::*;
285 /// let mut val = cobia::CapeValueImpl::new();
286 /// val.set_str("H2O");
287 /// assert_eq!(val.value(), CapeValueContent::String("H2O".into()));
288 /// ```
289
290 pub fn set_str<T: AsRef<str>>(&mut self,value: T) {
291 self.value = CapeValueContent::String(value.as_ref().into());
292 }
293
294 /// Set to integer
295 ///
296 /// Change the type to the given integer
297 ///
298 /// # Arguments
299 ///
300 /// * `value` - The new value
301 ///
302 /// # Examples
303 ///
304 /// ```
305 /// use cobia::*;
306 /// let mut val = cobia::CapeValueImpl::new();
307 /// val.set_integer(5);
308 /// assert_eq!(val.value(), CapeValueContent::Integer(5));
309 /// ```
310
311 pub fn set_integer(&mut self,value: CapeInteger) {
312 self.value=CapeValueContent::Integer(value);
313 }
314
315 /// Set to boolean
316 ///
317 /// Change the type to the given boolean
318 ///
319 /// # Arguments
320 ///
321 /// * `value` - The new value
322 ///
323 /// # Examples
324 ///
325 /// ```
326 /// use cobia::*;
327 /// let mut val = cobia::CapeValueImpl::new();
328 /// val.set_boolean(true);
329 /// assert_eq!(val.value(), CapeValueContent::Boolean(true));
330 /// val.set_boolean(false);
331 /// assert_eq!(val.value(), CapeValueContent::Boolean(false));
332 /// ```
333
334 pub fn set_boolean(&mut self,value: bool) {
335 self.value=CapeValueContent::Boolean(value);
336 }
337
338 /// Set to real
339 ///
340 /// Change the type to the given real
341 ///
342 /// # Arguments
343 ///
344 /// * `value` - The initial value
345 ///
346 /// # Examples
347 ///
348 /// ```
349 /// use cobia::*;
350 /// let mut val = cobia::CapeValueImpl::new();
351 /// val.set_real(3.1);
352 /// assert_eq!(val.value(), CapeValueContent::Real(3.1));
353 /// ```
354
355 pub fn set_real(&mut self,value: CapeReal) {
356 self.value=CapeValueContent::Real(value);
357 }
358
359 /// Set the content of the value from any object that implements CapeValueProviderIn.
360 ///
361 /// # Arguments
362 /// * `val` - An object that implements CapeValueProviderIn
363 ///
364 /// # Example
365 ///
366 /// ```
367 /// use cobia;
368 /// let mut val = cobia::CapeValueImpl::new();
369 /// let val1 = cobia::CapeValueImpl::from_str("test");
370 /// val.set(&val1);
371 /// assert_eq!(val.value(), cobia::CapeValueContent::String("test".into()));
372 ///
373 /// fn set_value(val: &mut cobia::CapeValueImpl,v: &cobia::CapeValueIn) {
374 /// val.set(v);
375 /// }
376 ///
377 /// let val2 = cobia::CapeValueImpl::from_str("test2");
378 /// set_value(&mut val,&cobia::CapeValueInFromProvider::from(&val2).as_cape_value_in());
379 /// assert_eq!(val.value(), cobia::CapeValueContent::String("test2".into()));
380 /// ```
381 pub fn set<T:CapeValueProviderIn>(&mut self,val:&T) -> Result<(), COBIAError> {
382 let mut value_in_from_provider = CapeValueInFromProvider::from(val);
383 let value=value_in_from_provider.as_cape_value_in();
384 match value.get_type()? {
385 CapeValueType::String => {self.set_string(value.get_string()?)},
386 CapeValueType::Integer => {self.set_integer(value.get_integer()?)},
387 CapeValueType::Boolean => {self.set_boolean(value.get_boolean()?);},
388 CapeValueType::Real => {self.set_real(value.get_real()?)},
389 CapeValueType::Empty => {self.reset()},
390 }
391 Ok(())
392 }
393
394 /// Get a reference to the value
395 ///
396 /// Returns a reference to the value
397 ///
398 /// # Examples
399 ///
400 /// ```
401 /// use cobia;
402 /// let val = cobia::CapeValueImpl::from_str("test");
403 /// assert_eq!(val.value_ref(),&cobia::CapeValueContent::String("test".into()));
404 /// ```
405
406 pub fn value_ref(&self) -> &CapeValueContent {
407 &self.value
408 }
409
410 /// Get a mutable reference to the value
411 ///
412 /// Returns a mutable reference to the value
413 ///
414 /// # Examples
415 ///
416 /// ```
417 /// use cobia;
418 /// use cobia::prelude::*;
419 /// let mut val = cobia::CapeValueImpl::from_integer(2);
420 /// *val.value_ref_mut()=cobia::CapeValueContent::Boolean(false);
421 /// assert_eq!(val.value_ref(),&cobia::CapeValueContent::Boolean(false));
422 /// ```
423
424 pub fn value_ref_mut(&mut self) -> &mut CapeValueContent {
425 &mut self.value
426 }
427
428 /// Get a clone of the value
429 ///
430 /// Returns a clone of the value
431 ///
432 /// # Examples
433 ///
434 /// ```
435 /// use cobia;
436 /// use cobia::prelude::*;
437 /// let val = cobia::CapeValueImpl::from_integer(2);
438 /// assert_eq!(val.value(),cobia::CapeValueContent::Integer(2));
439 /// ```
440
441 pub fn value(&self) -> CapeValueContent {
442 self.value.clone()
443 }
444
445 /// Interface member function
446
447 extern "C" fn get_value_type(me: *mut ::std::os::raw::c_void) -> C::CapeValueType {
448 let p = me as *mut Self;
449 let myself: &mut Self = unsafe { &mut *p };
450 match myself.value {
451 CapeValueContent::Empty => CapeValueType::Empty as C::CapeValueType,
452 CapeValueContent::String(_) => CapeValueType::String as C::CapeValueType,
453 CapeValueContent::Integer(_) => CapeValueType::Integer as C::CapeValueType,
454 CapeValueContent::Boolean(_) => CapeValueType::Boolean as C::CapeValueType,
455 CapeValueContent::Real(_) => CapeValueType::Real as C::CapeValueType,
456 }
457 }
458
459 /// Interface member function
460
461 extern "C" fn get_string_value(me: *mut ::std::os::raw::c_void,data: *mut *const CapeCharacter,size: *mut C::CapeSize) -> CapeResult {
462 let p = me as *mut Self;
463 let myself: &mut Self = unsafe { &mut *p };
464 match &myself.value {
465 CapeValueContent::String(s) => {
466 myself.str_val.set_string(s);
467 let (d, s)=myself.str_val.as_capechar_const_with_length();
468 unsafe {
469 *data = d;
470 *size = s;
471 }
472 COBIAERR_NOERROR
473 }
474 _ => COBIAERR_NOSUCHITEM
475 }
476 }
477
478 /// Interface member function
479
480 extern "C" fn get_integer_value(me: *mut ::std::os::raw::c_void,value: *mut CapeInteger) -> CapeResult {
481 let p = me as *mut Self;
482 let myself: &mut Self = unsafe { &mut *p };
483 match &myself.value {
484 CapeValueContent::Integer(i) => {
485 unsafe {
486 *value = *i;
487 }
488 COBIAERR_NOERROR
489 }
490 _ => COBIAERR_NOSUCHITEM
491 }
492 }
493
494 /// Interface member function
495
496 extern "C" fn get_boolean_value(me: *mut ::std::os::raw::c_void,value: *mut CapeBoolean) -> CapeResult {
497 let p = me as *mut Self;
498 let myself: &mut Self = unsafe { &mut *p };
499 match &myself.value {
500 CapeValueContent::Boolean(b) => {
501 unsafe {
502 *value = *b as CapeBoolean;
503 }
504 COBIAERR_NOERROR
505 }
506 _ => COBIAERR_NOSUCHITEM
507 }
508 }
509
510 /// Interface member function
511
512 extern "C" fn get_real_value(me: *mut ::std::os::raw::c_void,value: *mut CapeReal) -> CapeResult {
513 let p = me as *mut Self;
514 let myself: &mut Self = unsafe { &mut *p };
515 match &myself.value {
516 CapeValueContent::Real(r) => {
517 unsafe {
518 *value = *r;
519 }
520 COBIAERR_NOERROR
521 }
522 _ => COBIAERR_NOSUCHITEM
523 }
524 }
525
526 /// Interface member function
527
528 extern "C" fn set_string_value(me: *mut ::std::os::raw::c_void,data: *const CapeCharacter,size: C::CapeSize) -> CapeResult {
529 let p = me as *mut Self;
530 let myself: &mut Self = unsafe { &mut *p };
531 myself.str_val = unsafe { CapeStringImpl::from_raw_data(data,size) };
532 myself.value=CapeValueContent::String(myself.str_val.as_string());
533 COBIAERR_NOERROR
534 }
535
536 /// Interface member function
537
538 extern "C" fn set_integer_value(me: *mut ::std::os::raw::c_void,value: CapeInteger) -> CapeResult {
539 let p = me as *mut Self;
540 let myself: &mut Self = unsafe { &mut *p };
541 myself.value = CapeValueContent::Integer(value);
542 COBIAERR_NOERROR
543 }
544
545 /// Interface member function
546
547 extern "C" fn set_boolean_value(me: *mut ::std::os::raw::c_void,value: CapeBoolean) -> CapeResult {
548 let p = me as *mut Self;
549 let myself: &mut Self = unsafe { &mut *p };
550 myself.value = CapeValueContent::Boolean(value!=0);
551 COBIAERR_NOERROR
552 }
553
554 /// Interface member function
555
556 extern "C" fn set_real_value(me: *mut ::std::os::raw::c_void,value: CapeReal) -> CapeResult {
557 let p = me as *mut Self;
558 let myself: &mut Self = unsafe { &mut *p };
559 myself.value = CapeValueContent::Real(value);
560 COBIAERR_NOERROR
561 }
562
563 /// Interface member function
564
565 extern "C" fn clear(me: *mut ::std::os::raw::c_void) -> CapeResult {
566 let p = me as *mut Self;
567 let myself: &mut Self = unsafe { &mut *p };
568 myself.value = CapeValueContent::Empty;
569 COBIAERR_NOERROR
570 }
571
572 /// Interface v-table
573
574 const VTABLE: C::ICapeValue_VTable = C::ICapeValue_VTable {
575 getValueType: Some(Self::get_value_type),
576 getStringValue: Some(Self::get_string_value),
577 getIntegerValue: Some(Self::get_integer_value),
578 getBooleanValue: Some(Self::get_boolean_value),
579 getRealValue: Some(Self::get_real_value),
580 setStringValue: Some(Self::set_string_value),
581 setIntegerValue: Some(Self::set_integer_value),
582 setBooleanValue: Some(Self::set_boolean_value),
583 setRealValue: Some(Self::set_real_value),
584 clear: Some(Self::clear),
585 };
586
587}
588
589
590impl CapeValueProviderIn for CapeValueImpl {
591 /// Convert to ICapeValue
592 ///
593 /// Returns a reference to the ICapeValue interface.
594 ///
595 /// # Examples
596 ///
597 /// ```
598 /// use cobia::*;
599 /// use cobia::prelude::*;
600 /// let val = cobia::CapeValueImpl::from_integer(2);
601 /// let i_cape_value=val.as_cape_value_in();
602 /// let mut i_cape_value_ptr=(&i_cape_value as *const C::ICapeValue).cast_mut(); //normally a pointer to the interface is received
603 /// let v = cobia::CapeValueIn::new(&mut i_cape_value_ptr); //CapeValueIn from *mut C::ICapeValue
604 /// assert_eq!(v.get_integer().unwrap(), 2);
605 /// ```
606
607 fn as_cape_value_in(&self) -> C::ICapeValue {
608 C::ICapeValue {
609 vTbl:(&Self::VTABLE as *const C::ICapeValue_VTable).cast_mut(),
610 me:(self as *const Self).cast_mut() as *mut ::std::os::raw::c_void
611 }
612 }
613}
614
615impl CapeValueProviderOut for CapeValueImpl {
616 /// Convert to ICapeValue
617 ///
618 /// Returns a mutable reference to the ICapeValue interface.
619 ///
620 /// # Examples
621 ///
622 /// ```
623 /// use cobia::*;
624 /// use cobia::prelude::*;
625 /// let mut val = cobia::CapeValueImpl::new();
626 /// let i_cape_value=val.as_cape_value_out();
627 /// let mut i_cape_value_ptr=(&i_cape_value as *const C::ICapeValue).cast_mut(); //normally a pointer to the interface is received
628 /// let mut v = cobia::CapeValueOut::new(&mut i_cape_value_ptr); //CapeValueOut from *mut C::ICapeValue
629 /// v.set_real(2.5).unwrap();
630 /// assert_eq!(val.value(), cobia::cape_value_impl::CapeValueContent::Real(2.5));
631 /// ```
632
633 fn as_cape_value_out(&mut self) -> C::ICapeValue {
634 C::ICapeValue {
635 vTbl:(&Self::VTABLE as *const C::ICapeValue_VTable).cast_mut(),
636 me:(self as *const Self).cast_mut() as *mut ::std::os::raw::c_void
637 }
638 }
639}
640
641impl std::default::Default for CapeValueImpl {
642 fn default() -> Self {
643 Self::new()
644 }
645}
646
647impl<T: CapeValueProviderIn> PartialEq<T> for CapeValueImpl {
648 /// Partial equality
649 ///
650 /// Checks if the content of the CapeValueVec is equal to the content of another object that implements CapeValueProviderIn.
651 ///
652 /// # Arguments
653 ///
654 /// * `other` - An object that implements CapeValueProviderIn
655 ///
656 /// # Examples
657 ///
658 /// ```
659 /// use cobia::*;
660 /// let val1 = cobia::CapeValueImpl::from_integer(2);
661 /// let val2 = cobia::CapeValueImpl::from_integer(2);
662 /// let val3 = cobia::CapeValueImpl::from_boolean(true);
663 /// assert!(val1 == val2);
664 /// assert!(val1 != val3);
665 /// ```
666 fn eq(&self, other: &T) -> bool {
667 let mut provider=CapeValueInFromProvider::from(other);
668 let other=provider.as_cape_value_in();
669 //compare the values
670 match self.value {
671 CapeValueContent::Empty => {
672 if other.get_type().unwrap() != CapeValueType::Empty {
673 return false;
674 }
675 },
676 CapeValueContent::String(ref s) => {
677 if other.get_type().unwrap() != CapeValueType::String || other.get_string().unwrap() != *s {
678 return false;
679 }
680 },
681 CapeValueContent::Integer(i) => {
682 if other.get_type().unwrap() != CapeValueType::Integer || other.get_integer().unwrap() != i {
683 return false;
684 }
685 },
686 CapeValueContent::Boolean(b) => {
687 if other.get_type().unwrap() != CapeValueType::Boolean || other.get_boolean().unwrap() != b {
688 return false;
689 }
690 },
691 CapeValueContent::Real(r) => {
692 if other.get_type().unwrap() != CapeValueType::Real || other.get_real().unwrap() != r {
693 return false;
694 }
695 },
696 }
697 true
698 }
699}