use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

/// A Keeper keeps track of all guard (i.e. atomic bools) added to it via the add_guard function.
/// When the clear_all method is called they are all set to false
pub struct Keeper {
    guards: Vec<Arc<AtomicBool>>,
}

impl Keeper {
    /// Returns a Keeper with an empty guards vector
    pub fn new() -> Self {
        Self { guards: Vec::new() }
    }

    /// Adds a guard to the Keeper
    ///
    /// # Arguments
    ///
    /// * `guard` - An AtomicBool wrapped by an Arc representing a guard which decides whether a loop should keep running
    ///
    pub fn add_guard(&mut self, guard: Arc<AtomicBool>) {
        self.guards.push(guard);
    }

    /// Clears all guards known to the Keeper
    ///
    pub fn clear_all(&self) {
        for guard in &self.guards {
            guard.store(false, Ordering::SeqCst);
        }
    }
}
