mod context_extension;
mod data;
mod event;
pub(crate) mod external_api;
mod index;
mod macros;
pub(crate) mod methods;
mod multi_property;
mod property;
mod query;
use std::any::TypeId;
use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter};
use std::hash::Hash;
pub use context_extension::ContextPeopleExt;
use data::PeopleData;
pub use data::PersonPropertyHolder;
pub use event::{PersonCreatedEvent, PersonPropertyChangeEvent};
pub use index::Index;
pub use macros::*;
pub use multi_property::*;
pub use property::PersonProperty;
pub use query::Query;
use seq_macro::seq;
use serde::{Deserialize, Serialize};
use crate::context::Context;
use crate::{define_data_plugin, HashMap, HashMapExt, HashSet, HashSetExt};
pub type HashValueType = u128;
define_data_plugin!(
PeoplePlugin,
PeopleData,
PeopleData {
is_initializing: false,
current_population: 0,
methods: RefCell::new(HashMap::new()),
properties_map: RefCell::new(HashMap::new()),
registered_properties: RefCell::new(HashSet::new()),
dependency_map: RefCell::new(HashMap::new()),
property_indexes: RefCell::new(HashMap::new()),
people_types: RefCell::new(HashMap::new()),
}
);
#[derive(Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct PersonId(pub(crate) usize);
impl Display for PersonId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Debug for PersonId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Person {}", self.0)
}
}
pub trait InitializationList {
fn has_property(&self, t: TypeId) -> bool;
fn set_properties(&self, context: &mut Context, person_id: PersonId);
}
impl InitializationList for () {
fn has_property(&self, _: TypeId) -> bool {
false
}
fn set_properties(&self, _context: &mut Context, _person_id: PersonId) {}
}
impl<T1: PersonProperty> InitializationList for (T1, T1::Value) {
fn has_property(&self, t: TypeId) -> bool {
t == TypeId::of::<T1>()
}
fn set_properties(&self, context: &mut Context, person_id: PersonId) {
context.set_person_property(person_id, T1::get_instance(), self.1);
}
}
macro_rules! impl_initialization_list {
($ct:expr) => {
seq!(N in 0..$ct {
impl<
#(
T~N : PersonProperty,
)*
> InitializationList for (
#(
(T~N, T~N::Value),
)*
)
{
fn has_property(&self, t: TypeId) -> bool {
#(
if t == TypeId::of::<T~N>() { return true; }
)*
return false
}
fn set_properties(&self, context: &mut Context, person_id: PersonId) {
#(
context.set_person_property(person_id, T~N::get_instance(), self.N.1 );
)*
}
}
});
}
}
seq!(Z in 1..20 {
impl_initialization_list!(Z);
});