blackboard for visitor

This commit is contained in:
Dmitry
2023-04-21 21:22:17 +03:00
parent 7d6e3aa1be
commit d4f258f35d
2 changed files with 35 additions and 3 deletions
+34 -3
View File
@@ -28,6 +28,7 @@ use crate::{
use base64::Engine;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use fxhash::FxHashMap;
use std::any::TypeId;
use std::{
any::Any,
cell::{Cell, RefCell},
@@ -1064,6 +1065,36 @@ impl<'a> Drop for RegionGuard<'a> {
}
}
pub struct Blackboard {
items: FxHashMap<TypeId, Arc<dyn Any>>,
}
impl Blackboard {
pub fn new() -> Self {
Self {
items: Default::default(),
}
}
pub fn register<T: Any>(&mut self, value: T) {
self.items.insert(value.type_id(), Arc::new(value));
}
pub fn get<T: Any>(&self) -> Option<&T> {
self.items
.get(&TypeId::of::<T>())
.and_then(|v| v.downcast_ref())
}
pub fn inner(&self) -> &FxHashMap<TypeId, Arc<dyn Any>> {
&self.items
}
pub fn inner_mut(&mut self) -> &mut FxHashMap<TypeId, Arc<dyn Any>> {
&mut self.items
}
}
pub struct Visitor {
nodes: Pool<VisitorNode>,
rc_map: FxHashMap<u64, Rc<dyn Any>>,
@@ -1071,7 +1102,7 @@ pub struct Visitor {
reading: bool,
current_node: Handle<VisitorNode>,
root: Handle<VisitorNode>,
pub environment: Option<Arc<dyn Any>>,
pub blackboard: Blackboard,
}
pub trait Visit {
@@ -1097,7 +1128,7 @@ impl Visitor {
reading: false,
current_node: root,
root,
environment: None,
blackboard: Blackboard::new(),
}
}
@@ -1284,7 +1315,7 @@ impl Visitor {
reading: true,
current_node: Handle::NONE,
root: Handle::NONE,
environment: None,
blackboard: Blackboard::new(),
};
visitor.root = visitor.load_node_binary(&mut reader)?;
visitor.current_node = visitor.root;
+1
View File
@@ -16,3 +16,4 @@ rust-version = "1.56"
[dependencies]
fyrox-core = { path = "../fyrox-core", version = "0.23.0" }
fxhash = "0.2.1"