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
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::{translate::*, value::FromValue, Value};

glib::wrapper! {
    /// An opaque structure representing a watched [`Expression`][crate::Expression].
    ///
    /// The contents of [`ExpressionWatch`][crate::ExpressionWatch] should only be accessed through the
    /// provided API.
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    #[doc(alias = "GtkExpressionWatch")]
    pub struct ExpressionWatch(Shared<ffi::GtkExpressionWatch>);

    match fn {
        ref => |ptr| ffi::gtk_expression_watch_ref(ptr),
        unref => |ptr| ffi::gtk_expression_watch_unref(ptr),
    }
}

impl ExpressionWatch {
    /// Evaluates the watched expression and on success stores the result
    /// in `value`.
    ///
    /// This is equivalent to calling [`Expression::evaluate()`][crate::Expression::evaluate()] with the
    /// expression and this pointer originally used to create `watch`.
    /// ## `value`
    /// an empty `GValue` to be set
    ///
    /// # Returns
    ///
    /// `TRUE` if the expression could be evaluated and `value` was set
    #[doc(alias = "gtk_expression_watch_evaluate")]
    pub fn evaluate(&self) -> Option<Value> {
        assert_initialized_main_thread!();
        unsafe {
            let mut value = Value::uninitialized();
            let ret = ffi::gtk_expression_watch_evaluate(
                self.to_glib_none().0,
                value.to_glib_none_mut().0,
            );
            if from_glib(ret) {
                Some(value)
            } else {
                None
            }
        }
    }

    // rustdoc-stripper-ignore-next
    /// Similar to [`Self::evaluate`] but panics if the value is of a different
    /// type.
    #[doc(alias = "gtk_expression_evaluate")]
    pub fn evaluate_as<V: for<'b> FromValue<'b> + 'static>(&self) -> Option<V> {
        self.evaluate().map(|v| {
            v.get_owned::<V>()
                .expect("Failed to evaluate to this value type")
        })
    }

    /// Stops watching an expression.
    ///
    /// See [`Expression::watch()`][crate::Expression::watch()] for how the watch
    /// was established.
    #[doc(alias = "gtk_expression_watch_unwatch")]
    pub fn unwatch(&self) {
        unsafe { ffi::gtk_expression_watch_unwatch(self.to_glib_none().0) }
    }
}

#[cfg(feature = "v4_2")]
impl glib::prelude::StaticType for ExpressionWatch {
    #[doc(alias = "gtk_expression_watch_get_type")]
    #[inline]
    fn static_type() -> glib::Type {
        unsafe { from_glib(ffi::gtk_expression_watch_get_type()) }
    }
}