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
//! Global read-only storage
//!
//! ```
//! use macroquad::experimental::collections::storage;
//!
//! struct WorldBoundaries(i32);
//!
//! fn draw_player() {
//!   let boundaries: i32 = storage::get::<WorldBoundaries>().0;
//!   assert_eq!(boundaries, 23);
//! }
//!
//! storage::store(WorldBoundaries(23));
//! draw_player();
//! ```
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::{
    cell::RefCell,
    ops::{Deref, DerefMut},
    rc::Rc,
};
static mut STORAGE: Option<HashMap<TypeId, Box<dyn Any>>> = None;
/// Store data in global storage.
/// Will silently overwrite an old value if any.
pub fn store<T: Any>(data: T) {
    unsafe {
        if STORAGE.is_none() {
            STORAGE = Some(HashMap::new());
        }
        STORAGE
            .as_mut()
            .unwrap()
            .insert(TypeId::of::<T>(), Box::new(Rc::new(RefCell::new(data))))
    };
}
/// Get reference to data from global storage.
/// Will panic if there is no data available with this type.
pub fn get<T: Any>() -> impl Deref<Target = T> {
    try_get::<T>().unwrap()
}
/// Get reference to data from global storage.
/// Will return None if there is no data available with this type.
pub fn try_get<T: Any>() -> Option<impl Deref<Target = T>> {
    unsafe {
        if STORAGE.is_none() {
            STORAGE = Some(HashMap::new());
        }
        STORAGE.as_mut().unwrap().get(&TypeId::of::<T>()).as_ref()
    }
    .and_then(|data| {
        data.downcast_ref::<Rc<RefCell<T>>>()
            .map(|data| data.borrow())
    })
}
/// Get mutable reference to data from global storage.
/// Will return None if there is no data available with this type.
pub fn try_get_mut<T: Any>() -> Option<impl DerefMut<Target = T>> {
    unsafe {
        if STORAGE.is_none() {
            STORAGE = Some(HashMap::new());
        }
        STORAGE.as_mut().unwrap().get(&TypeId::of::<T>()).as_ref()
    }
    .and_then(|data| {
        data.downcast_ref::<Rc<RefCell<T>>>()
            .map(|data| data.borrow_mut())
    })
}
/// Get mutable reference to data from global storage.
/// Will panic if there is no data available with this type.
pub fn get_mut<T: Any>() -> impl DerefMut<Target = T> {
    try_get_mut::<T>().unwrap()
}