Complete refactor of the FFI to use handles instead of pointers.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
use crate::static_data::Nature;
|
||||
use crate::{Random, StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
use crate::{Random, StringKey};
|
||||
use anyhow::bail;
|
||||
use anyhow_ext::{ensure, Result};
|
||||
use indexmap::IndexMap;
|
||||
use parking_lot::RwLock;
|
||||
use std::fmt::Debug;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// A library of all natures that can be used, stored by their names.
|
||||
pub trait NatureLibrary: Debug + ValueIdentifiable {
|
||||
pub trait NatureLibrary: Debug {
|
||||
/// Adds a new nature with name to the library.
|
||||
fn load_nature(&mut self, name: StringKey, nature: Arc<dyn Nature>);
|
||||
fn load_nature(&self, name: StringKey, nature: Arc<dyn Nature>);
|
||||
/// Gets a nature by name.
|
||||
fn get_nature(&self, key: &StringKey) -> Option<Arc<dyn Nature>>;
|
||||
/// Gets a random nature.
|
||||
@@ -21,38 +22,35 @@ pub trait NatureLibrary: Debug + ValueIdentifiable {
|
||||
/// A library of all natures that can be used, stored by their names.
|
||||
#[derive(Debug)]
|
||||
pub struct NatureLibraryImpl {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The underlying data structure.
|
||||
map: IndexMap<StringKey, Arc<dyn Nature>>,
|
||||
map: RwLock<IndexMap<StringKey, Arc<dyn Nature>>>,
|
||||
}
|
||||
|
||||
impl NatureLibraryImpl {
|
||||
/// Creates a new nature library with a given capacity.
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
Self {
|
||||
identifier: Default::default(),
|
||||
map: IndexMap::with_capacity(capacity),
|
||||
map: RwLock::new(IndexMap::with_capacity(capacity)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NatureLibrary for NatureLibraryImpl {
|
||||
/// Adds a new nature with name to the library.
|
||||
fn load_nature(&mut self, name: StringKey, nature: Arc<dyn Nature>) {
|
||||
self.map.insert(name, nature);
|
||||
fn load_nature(&self, name: StringKey, nature: Arc<dyn Nature>) {
|
||||
self.map.write().insert(name, nature);
|
||||
}
|
||||
|
||||
/// Gets a nature by name.
|
||||
fn get_nature(&self, key: &StringKey) -> Option<Arc<dyn Nature>> {
|
||||
self.map.get(key).cloned()
|
||||
self.map.read().get(key).cloned()
|
||||
}
|
||||
|
||||
fn get_random_nature(&self, rand: &mut Random) -> Result<Arc<dyn Nature>> {
|
||||
ensure!(!self.map.is_empty(), "No natures were loaded into the library.");
|
||||
let i = rand.get_between(0, self.map.len() as i32);
|
||||
Ok(self
|
||||
.map
|
||||
let map = self.map.read();
|
||||
ensure!(!map.is_empty(), "No natures were loaded into the library.");
|
||||
let i = rand.get_between(0, map.len() as i32);
|
||||
Ok(map
|
||||
.get_index(i as usize)
|
||||
// this should never happen, but we'll check anyway.
|
||||
.ok_or(anyhow_ext::anyhow!("Failed to get a random nature from the library."))?
|
||||
@@ -62,10 +60,11 @@ impl NatureLibrary for NatureLibraryImpl {
|
||||
|
||||
/// Finds a nature name by nature.
|
||||
fn get_nature_name(&self, nature: &Arc<dyn Nature>) -> Result<StringKey> {
|
||||
for kv in &self.map {
|
||||
let read_lock = self.map.read();
|
||||
for kv in read_lock.iter() {
|
||||
// As natures can't be copied, and should always be the same reference as the value
|
||||
// in the map, we just compare by reference.
|
||||
if kv.1.value_identifier() == nature.value_identifier() {
|
||||
if Arc::ptr_eq(&kv.1, nature) {
|
||||
return Ok(kv.0.clone());
|
||||
}
|
||||
}
|
||||
@@ -73,12 +72,6 @@ impl NatureLibrary for NatureLibraryImpl {
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for NatureLibraryImpl {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
@@ -89,7 +82,7 @@ pub mod tests {
|
||||
use crate::static_data::{NatureImpl, NatureLibrary, NatureLibraryImpl};
|
||||
|
||||
pub fn build() -> NatureLibraryImpl {
|
||||
let mut lib = NatureLibraryImpl::new(2);
|
||||
let lib = NatureLibraryImpl::new(2);
|
||||
|
||||
lib.load_nature(
|
||||
"test_nature".into(),
|
||||
@@ -103,21 +96,16 @@ pub mod tests {
|
||||
#[derive(Debug)]
|
||||
pub NatureLibrary{}
|
||||
impl NatureLibrary for NatureLibrary {
|
||||
fn load_nature(&mut self, name: StringKey, nature: Arc<dyn Nature>);
|
||||
fn load_nature(&self, name: StringKey, nature: Arc<dyn Nature>);
|
||||
fn get_nature(&self, key: &StringKey) -> Option<Arc<dyn Nature>>;
|
||||
fn get_nature_name(&self, nature: &Arc<dyn Nature>) -> Result<StringKey>;
|
||||
fn get_random_nature(&self, rand: &mut Random) -> Result<Arc<dyn Nature>>;
|
||||
}
|
||||
impl ValueIdentifiable for NatureLibrary {
|
||||
fn value_identifier(&self) -> ValueIdentifier{
|
||||
ValueIdentifier::new(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_nature_library_insert_and_retrieve() {
|
||||
let mut lib = NatureLibraryImpl::new(2);
|
||||
let lib = NatureLibraryImpl::new(2);
|
||||
lib.load_nature(
|
||||
"foo".into(),
|
||||
NatureImpl::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
@@ -135,7 +123,7 @@ pub mod tests {
|
||||
|
||||
#[test]
|
||||
fn create_nature_library_insert_and_get_name() {
|
||||
let mut lib = NatureLibraryImpl::new(2);
|
||||
let lib = NatureLibraryImpl::new(2);
|
||||
lib.load_nature(
|
||||
"foo".into(),
|
||||
NatureImpl::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
@@ -155,7 +143,7 @@ pub mod tests {
|
||||
|
||||
#[test]
|
||||
fn create_nature_library_insert_single_and_retrieve_random() {
|
||||
let mut lib = NatureLibraryImpl::new(2);
|
||||
let lib = NatureLibraryImpl::new(2);
|
||||
lib.load_nature(
|
||||
"foo".into(),
|
||||
NatureImpl::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
|
||||
Reference in New Issue
Block a user