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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use {Output, View};

use ffi;
use libc::c_void;
use output::handle as output_handle;

use std::any::Any;
use std::mem;
use std::ptr;
use std::rc::Rc;
use view::handle as view_handle;

/// Assiciated user-provided data
///
/// Types implemeting Handle (Output, View) may hold a pointer to user-data.
/// This Trait handles storing and receiving arbitrary data for such Types.
pub trait Handle {
    /// Sets any data as user pointer consuming the data.
    ///
    /// Clears old user data before inserting new one (`drop`s it correctly).
    fn set_user_data<T: Any>(&self, data: T);

    /// Receive a shared reference to the user data of a given type, if user
    /// data exists.
    ///
    /// Returns `None` if no user data is set.
    ///
    /// # Safety
    /// While the function confirms that userdata does actually exists,
    /// it does not verify that `T` is the correct Type for the data making it
    /// unsafe if the wrong type is used.
    unsafe fn user_data<T: Any>(&self) -> Option<Rc<T>>;

    /// Tries to take the userdata exclusively and removes it from the Handle.
    ///
    /// Returns `None` if no data exists or shared references do exist that
    /// make taking impossible.
    ///
    /// # Safety
    /// While the function confirms that userdata does actually exists,
    /// it does not verify that `T` is the correct Type for the data making it
    /// unsafe if the wrong type is used.
    unsafe fn try_take_user_data<T: Any>(&self) -> Option<T>;

    /// Clears currently set user data (`Drop` gets called, after all
    /// references are dropped).
    ///
    /// This is safe and a no-op if no user data exists
    fn clear_user_data(&self);
}

impl Handle for Output {
    fn set_user_data<T: Any>(&self, data: T) {
        self.clear_user_data(); // drop

        let boxed = Box::new(Rc::new(data));
        unsafe {
            ffi::wlc_handle_set_user_data(output_handle(self), Box::into_raw(boxed) as *const c_void);
        }
    }

    unsafe fn user_data<T: Any>(&self) -> Option<Rc<T>> {
        let ptr = ffi::wlc_handle_get_user_data(output_handle(self));
        let boxed: Box<Rc<T>> = if ptr.is_null() {
            return None;
        } else {
            Box::from_raw(ptr as *mut Rc<T>)
        };

        // inc ref count on return
        let result = Some((*boxed).clone());
        mem::forget(boxed);
        result
    }

    unsafe fn try_take_user_data<T: Any>(&self) -> Option<T> {
        match self.user_data::<T>() {
            Some(rc) => {
                self.clear_user_data();
                match Rc::try_unwrap(rc) {
                    Ok(result) => Some(result),
                    Err(rc) => {
                        ffi::wlc_handle_set_user_data(output_handle(self),
                                                      Box::into_raw(Box::new(rc)) as *const c_void);
                        None
                    }
                }
            }
            None => None,
        }
    }

    fn clear_user_data(&self) {
        let ptr = unsafe { ffi::wlc_handle_get_user_data(output_handle(self)) };
        let _drop: Box<Any> = if ptr.is_null() {
            return;
        } else {
            unsafe { Box::from_raw(ptr as *mut Any) }
        };
        unsafe {
            ffi::wlc_handle_set_user_data(output_handle(self), ptr::null_mut());
        }
    }
}


impl Handle for View {
    fn set_user_data<T: Any>(&self, data: T) {
        self.clear_user_data(); // drop

        let boxed = Box::new(Rc::new(data));
        unsafe {
            ffi::wlc_handle_set_user_data(view_handle(self), Box::into_raw(boxed) as *const c_void);
        }
    }

    unsafe fn user_data<T: Any>(&self) -> Option<Rc<T>> {
        let ptr = ffi::wlc_handle_get_user_data(view_handle(self));
        let boxed: Box<Rc<T>> = if ptr.is_null() {
            return None;
        } else {
            Box::from_raw(ptr as *mut Rc<T>)
        };

        // inc ref count on return
        let result = Some((*boxed).clone());
        mem::forget(boxed);
        result
    }

    unsafe fn try_take_user_data<T: Any>(&self) -> Option<T> {
        match self.user_data::<T>() {
            Some(rc) => {
                self.clear_user_data();
                match Rc::try_unwrap(rc) {
                    Ok(result) => Some(result),
                    Err(rc) => {
                        ffi::wlc_handle_set_user_data(view_handle(self),
                                                      Box::into_raw(Box::new(rc)) as *const c_void);
                        None
                    }
                }
            }
            None => None,
        }
    }

    fn clear_user_data(&self) {
        let ptr = unsafe { ffi::wlc_handle_get_user_data(view_handle(self)) };
        let _drop: Box<Any> = if ptr.is_null() {
            return;
        } else {
            unsafe { Box::from_raw(ptr as *mut Any) }
        };
        unsafe {
            ffi::wlc_handle_set_user_data(view_handle(self), ptr::null_mut());
        }
    }
}