mcpat/
system.rs

1use raw;
2use std::fs;
3use std::marker::PhantomData;
4use std::path::Path;
5
6use {Raw, Result, Processor};
7
8/// A system.
9pub struct System {
10    raw: Raw<raw::ParseXML>,
11    phantom: PhantomData<raw::ParseXML>,
12}
13
14impl System {
15    /// Load a system from a file.
16    ///
17    /// The file is a configuration file of McPAT.
18    pub fn open<T: AsRef<Path>>(path: T) -> Result<System> {
19        let path = path.as_ref();
20        if !exists(path) {
21            raise!(NotFound, format!("the file {:?} does not exist", path));
22        }
23        unsafe {
24            let raw = not_null!(raw::new_ParseXML());
25            raw::ParseXML_parse(raw, path_to_cstr!(path).as_ptr() as *mut _);
26            Ok(System {
27                raw: (raw, debug_not_null!(raw::ParseXML_sys(raw))),
28                phantom: PhantomData,
29            })
30        }
31    }
32
33    /// Perform optimization and produce an instance of the system.
34    pub fn compute<'l>(&'l self) -> Result<Processor<'l>> {
35        let raw = unsafe { not_null!(raw::new_Processor(self.raw.0)) };
36        Ok(::processor::from_raw((raw, self.raw.1)))
37    }
38
39    /// Return the raw specification of the system.
40    #[inline(always)]
41    pub fn raw(&self) -> &raw::root_system {
42        unsafe { &*self.raw.1 }
43    }
44}
45
46impl Drop for System {
47    #[inline]
48    fn drop(&mut self) {
49        unsafe { raw::delete_ParseXML(debug_not_null!(self.raw.0)) };
50    }
51}
52
53#[inline]
54fn exists<T: AsRef<Path>>(path: T) -> bool {
55    match fs::metadata(path) {
56        Ok(metadata) => !metadata.is_dir(),
57        Err(_) => false,
58    }
59}