Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ekr docs global properties #106

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/global_properties.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
//! A generic mechanism for storing context-wide data.
//!
//! Global properties are not mutable and represent variables that are
//! required in a global scope during the simulation, such as
//! simulation parameters.
//! A global property can be of any type, and is is just a value
//! stored in the context. Global properties are defined by the
//! [`define_global_property!()`] macro and can then be
//! set in one of two ways:
//!
//! * Directly by using [`Context::set_global_property_value()`]
//! * Loaded from a configuration file using [`Context::load_global_properties()`]
//!
//! Attempting to change a global property which has been set already
//! will result in an error.
//!
//! Global properties can be read with [`Context::get_global_property_value()`]
use crate::context::Context;
use crate::error::IxaError;
use serde::de::DeserializeOwned;
Expand All @@ -22,6 +39,7 @@ type PropertySetterFn =
// RefCell/Mutex/LazyLock combo to allow it to be globally
// shared and initialized at startup time while still being
// safe.
#[doc(hidden)]
pub static GLOBAL_PROPERTIES: LazyLock<Mutex<RefCell<HashMap<String, Arc<PropertySetterFn>>>>> =
LazyLock::new(|| Mutex::new(RefCell::new(HashMap::new())));

Expand Down Expand Up @@ -96,13 +114,15 @@ macro_rules! define_global_property {
};
}

/// Global properties are not mutable and represent variables that are required
/// in a global scope during the simulation, such as simulation parameters.
/// The trait representing a global property. Do not use this
/// directly, but instead define global properties with
/// [`define_global_property()`]
pub trait GlobalProperty: Any {
type Value: Any;
type Value: Any; // The actual type of the data.

fn new() -> Self;
#[allow(clippy::missing_errors_doc)]
// A function which validates the global property.
fn validate(value: &Self::Value) -> Result<(), IxaError>;
}

Expand Down Expand Up @@ -161,8 +181,12 @@ pub trait ContextGlobalPropertiesExt {
/// * There are two values for the same object.
///
/// Ixa automatically knows about any property defined with
/// `define_global_property!()` so you don't need to register them
/// [`define_global_property!()`] so you don't need to register them
/// explicitly.
///
/// It is possible to call [`Context::load_global_properties()`] multiple
/// times with different files as long as the files have disjoint
/// sets of properties.
fn load_global_properties(&mut self, file_name: &Path) -> Result<(), IxaError>;
}

Expand Down