use error::Error;
use framework::IFramework;
use device::{IDevice, DeviceType};
#[derive(Debug, Clone)]
pub struct Backend<F: IFramework> {
framework: Box<F>,
device: DeviceType,
}
impl<F: IFramework + Clone> Backend<F> {
pub fn new(config: BackendConfig<F>) -> Result<Backend<F>, Error> {
let device = try!(config.framework.new_device(config.hardwares));
Ok(
Backend {
framework: Box::new(config.framework),
device: device,
}
)
}
pub fn hardwares(&self) -> &[F::H] {
self.framework.hardwares()
}
pub fn framework(&self) -> &Box<F> {
&self.framework
}
pub fn device(&self) -> &DeviceType {
&self.device
}
}
pub trait IBackend {
type F: IFramework + Clone;
fn device(&self) -> &DeviceType;
fn default() -> Result<Backend<Self::F>, Error> where Self: Sized {
let hw_framework = Self::F::new();
let hardwares = hw_framework.hardwares();
let framework = Self::F::new(); let backend_config = BackendConfig::new(framework, hardwares);
Backend::new(backend_config)
}
fn synchronize(&self) -> Result<(), ::framework::Error> { Ok(()) }
}
#[derive(Debug, Clone)]
pub struct BackendConfig<'a, F: IFramework + 'a> {
framework: F,
hardwares: &'a [F::H],
}
impl<'a, F: IFramework + Clone> BackendConfig<'a, F> {
pub fn new(framework: F, hardwares: &'a [F::H]) -> BackendConfig<'a, F> {
BackendConfig {
framework: framework.clone(),
hardwares: hardwares,
}
}
}