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
//! Common validation used for the [contexts][crate::context].

use std::collections::HashSet;

/// Ensures that parameter changes send from the GUI are wrapped in parameter gestures, and that the
/// gestures are handled consistently (no duplicate starts and ends, no end before start, etc.).
///
/// Should only be used in debug builds.
#[derive(Debug, Default)]
pub struct ParamGestureChecker {
    /// The parameters with an active gesture.
    active_params: HashSet<String>,
}

impl Drop for ParamGestureChecker {
    fn drop(&mut self) {
        nih_debug_assert!(
            self.active_params.is_empty(),
            "GuiContext::end_set_parameter() was never called for {} {} {:?}",
            self.active_params.len(),
            if self.active_params.len() == 1 {
                "parameter"
            } else {
                "parameters"
            },
            self.active_params
        );
    }
}

impl ParamGestureChecker {
    /// Called for
    /// [`GuiContext::begin_set_parameter()`][crate::prelude::GuiContext::begin_set_parameter()].
    /// Triggers a debug assertion failure if the state is inconsistent.
    pub fn begin_set_parameter(&mut self, param_id: &str) {
        nih_debug_assert!(
            !self.active_params.contains(param_id),
            "GuiContext::begin_set_parameter() was called twice for parameter '{}'",
            param_id
        );
        self.active_params.insert(param_id.to_owned());
    }

    /// Called for [`GuiContext::set_parameter()`][crate::prelude::GuiContext::set_parameter()].
    /// Triggers a debug assertion failure if the state is inconsistent.
    pub fn set_parameter(&self, param_id: &str) {
        nih_debug_assert!(
            self.active_params.contains(param_id),
            "GuiContext::set_parameter() was called for parameter '{}' without a preceding \
             begin_set_parameter() call",
            param_id
        );
    }

    /// Called for
    /// [`GuiContext::end_set_parameter()`][crate::prelude::GuiContext::end_set_parameter()].
    /// Triggers a debug assertion failure if the state is inconsistent.
    pub fn end_set_parameter(&mut self, param_id: &str) {
        nih_debug_assert!(
            self.active_params.contains(param_id),
            "GuiContext::end_set_parameter() was called for parameter '{}' without a preceding \
             begin_set_parameter() call",
            param_id
        );
        self.active_params.remove(param_id);
    }
}