Skip to main content

datex_core/shared_values/
shared_value_container.rs

1use crate::{
2    shared_values::{
3        observers::Observer, shared_container::SharedContainerMutability,
4    },
5    traits::value_eq::ValueEq,
6    types::definition::TypeDefinition,
7    utils::freemap::FreeHashMap,
8    values::{value::Value, value_container::ValueContainer},
9};
10
11use crate::{prelude::*, shared_values::pointer::Pointer};
12use core::{cell::RefCell, fmt::Debug, prelude::rust_2024::*};
13
14pub struct SharedValueContainer {
15    pub pointer: Pointer,
16    /// the value that this reference points to
17    pub value_container: ValueContainer,
18    /// pointer id, can be initialized as None for local pointers
19    /// custom type for the pointer that the Datex value is allowed to reference
20    pub allowed_type: TypeDefinition,
21    /// list of observer callbacks
22    pub observers: FreeHashMap<u32, Observer>,
23    pub mutability: SharedContainerMutability,
24}
25
26impl SharedValueContainer {
27    pub fn new(
28        value_container: ValueContainer,
29        pointer: Pointer,
30        allowed_type: TypeDefinition,
31        mutability: SharedContainerMutability,
32    ) -> Self {
33        SharedValueContainer {
34            value_container,
35            pointer,
36            allowed_type,
37            observers: FreeHashMap::new(),
38            mutability,
39        }
40    }
41}
42
43impl Debug for SharedValueContainer {
44    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
45        f.debug_struct("ReferenceData")
46            .field("value_container", &self.value_container)
47            .field("pointer_address", &self.pointer.address())
48            .field("allowed_type", &self.allowed_type)
49            .field("observers", &self.observers.len())
50            .finish()
51    }
52}
53
54impl PartialEq for SharedValueContainer {
55    fn eq(&self, other: &Self) -> bool {
56        // Two value references are equal if their value containers are equal
57        self.value_container.value_eq(&other.value_container)
58    }
59}
60
61impl SharedValueContainer {
62    pub fn current_value_container(&self) -> &ValueContainer {
63        &self.value_container
64    }
65
66    pub fn resolve_current_value(&self) -> Rc<RefCell<Value>> {
67        self.value_container.to_value()
68    }
69
70    pub fn is_mutable(&self) -> bool {
71        core::matches!(self.mutability, SharedContainerMutability::Mutable)
72    }
73}