1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::{ffi::c_void, ptr};

use crate::{bindings::{mpr_obj, mpr_obj_get_prop_by_idx, mpr_obj_get_type, mpr_obj_set_prop, mpr_prop, mpr_type}, device::{Device, MappableType}, graph::Map, signal::Signal};

pub trait AsMprObject {
  fn as_mpr_object(&self) -> *mut c_void;
}

impl AsMprObject for Signal {
  fn as_mpr_object(&self) -> *mut c_void {
    self.handle as *mut c_void
  }
}
impl AsMprObject for Device<'_> {
  fn as_mpr_object(&self) -> *mut c_void {
    self.handle as *mut c_void
  }
}

impl AsMprObject for Map {
  fn as_mpr_object(&self) -> *mut c_void {
    self.handle as *mut c_void
  }
}

impl AsMprObject for mpr_obj {
  fn as_mpr_object(&self) -> *mut c_void {
      *self as *mut c_void
  }
}

pub trait MapperObject {
  /// Get the `mpr_type` representing this object
  fn get_type(&self) -> mpr_type;
  /// Set a property on this object to a numerical value
  fn set_property<T: MappableType>(&self, property: mpr_prop, value: T);
  /// Set a property on this object to a string value
  fn set_property_str(&self, property: mpr_prop, value: &str);

  /// Get the value of a property by it's key from this object.
  /// If the property does not exist, or if the type is not matched, this function will return an error.
  fn get_property<T: MappableType + Default + Copy>(&self, property: mpr_prop) -> Result<T, PropertyError>;

  /// Get the value of a string property by it's key from this object.
  /// If the property does not exist, or if the type is not matched, this function will return an error.
  fn get_property_str(&self, property: mpr_prop) -> Result<String, PropertyError>;
}

impl<A> MapperObject for A where A: AsMprObject {
  fn get_type(&self) -> mpr_type {
    unsafe {
      mpr_obj_get_type(self.as_mpr_object())
    }
  }

  fn set_property<T: MappableType>(&self, property: mpr_prop, value: T) {
    unsafe {
      mpr_obj_set_prop(self.as_mpr_object(), property, ptr::null(), 1, T::get_mpr_type(), &value as *const T as *const c_void, 1);
    }
  }

  fn set_property_str(&self, property: mpr_prop, value: &str) {
    let value_ptr = std::ffi::CString::new(value).expect("CString::new failed");
    unsafe {
      mpr_obj_set_prop(self.as_mpr_object(), property, ptr::null(), 1, mpr_type::MPR_STR, value_ptr.as_ptr() as *const c_void, 1);
    }
  }

  fn get_property<T: MappableType + Default + Copy>(&self, property: mpr_prop) -> Result<T, PropertyError> {
    unsafe {
      let mut actual_type: mpr_type = mpr_type::MPR_NULL;
      let mut value: *const c_void  = ptr::null();
      mpr_obj_get_prop_by_idx(self.as_mpr_object(), property as i32,  ptr::null_mut(), ptr::null_mut(), 
      &mut actual_type, &mut value, ptr::null_mut());
      if value.is_null() {
        return Err(PropertyError::PropertyNotFound)
      }
      if actual_type != T::get_mpr_type() {
        return Err(PropertyError::TypeMismatch)
      }
      let value = value as *const T;
      Ok(*value)
    }
  }

  fn get_property_str(&self, property: mpr_prop) -> Result<String, PropertyError> {
    unsafe {
      let mut actual_type: mpr_type = mpr_type::MPR_NULL;
      let mut value: *const c_void  = ptr::null();
      mpr_obj_get_prop_by_idx(self.as_mpr_object(), property as i32,  ptr::null_mut(), ptr::null_mut(), 
      &mut actual_type, &mut value, ptr::null_mut());
      if value.is_null() {
        return Err(PropertyError::PropertyNotFound)
      }
      if actual_type != mpr_type::MPR_STR {
        return Err(PropertyError::TypeMismatch)
      }
      let value = value as *const std::os::raw::c_char;
      let value = std::ffi::CStr::from_ptr(value).to_str().unwrap().to_string();
      Ok(value)
    }
  }
}

/// Errors that can occur when working with properties
#[derive(Debug, PartialEq)]
pub enum PropertyError {
  /// The property was not found on the object
  PropertyNotFound,
  /// The property was found, but the type did not match the expected type
  TypeMismatch
}