1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::fmt;

use necsim_core::reporter::{boolean::Boolean, FilteredReporter, Reporter};

use necsim_partitioning_core::reporter::{FinalisableReporter, ReporterContext};

use necsim_plugins_core::import::ReporterPluginVec;

pub struct DynamicReporterContext<
    ReportSpeciation: Boolean,
    ReportDispersal: Boolean,
    ReportProgress: Boolean,
> {
    reporter: ReporterPluginVec<ReportSpeciation, ReportDispersal, ReportProgress>,
}

impl<ReportSpeciation: Boolean, ReportDispersal: Boolean, ReportProgress: Boolean> fmt::Debug
    for DynamicReporterContext<ReportSpeciation, ReportDispersal, ReportProgress>
{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct(stringify!(DynamicReporterContext))
            .field("reporter", &self.reporter)
            .finish()
    }
}

impl<ReportSpeciation: Boolean, ReportDispersal: Boolean, ReportProgress: Boolean>
    DynamicReporterContext<ReportSpeciation, ReportDispersal, ReportProgress>
{
    #[allow(dead_code)]
    pub fn new(
        reporter: ReporterPluginVec<ReportSpeciation, ReportDispersal, ReportProgress>,
    ) -> Self {
        Self { reporter }
    }
}

impl<ReportSpeciation: Boolean, ReportDispersal: Boolean, ReportProgress: Boolean> ReporterContext
    for DynamicReporterContext<ReportSpeciation, ReportDispersal, ReportProgress>
{
    type Reporter = ReporterPluginVec<ReportSpeciation, ReportDispersal, ReportProgress>;

    fn try_build<KeepSpeciation: Boolean, KeepDispersal: Boolean, KeepProgress: Boolean>(
        self,
    ) -> anyhow::Result<FilteredReporter<Self::Reporter, KeepSpeciation, KeepDispersal, KeepProgress>>
    {
        let mut filtered_reporter = self
            .reporter
            .internal_filter::<KeepSpeciation, KeepDispersal, KeepProgress>();

        match filtered_reporter.initialise() {
            Ok(()) => Ok(FilteredReporter::from(filtered_reporter)),
            Err(err) => Err(anyhow::Error::msg(err)),
        }
    }
}

#[cfg_attr(
    not(any(
        feature = "gillespie-algorithms",
        feature = "independent-algorithm",
        feature = "cuda-algorithm"
    )),
    allow(dead_code)
)]
#[allow(clippy::module_name_repetitions)]
pub enum FinalisablePartitioningReporter<R: Reporter> {
    Monolithic(<necsim_partitioning_monolithic::MonolithicPartitioning as necsim_partitioning_core::Partitioning>::FinalisableReporter<R>),
    #[cfg(feature = "mpi-partitioning")]
    Mpi(<necsim_partitioning_mpi::MpiPartitioning as necsim_partitioning_core::Partitioning>::FinalisableReporter<R>),
    #[cfg(feature = "threads-partitioning")]
    Threads(<necsim_partitioning_threads::ThreadsPartitioning as necsim_partitioning_core::Partitioning>::FinalisableReporter<R>),
}

impl<R: Reporter> FinalisableReporter for FinalisablePartitioningReporter<R> {
    fn finalise(self) {
        match self {
            Self::Monolithic(reporter) => reporter.finalise(),
            #[cfg(feature = "mpi-partitioning")]
            Self::Mpi(reporter) => reporter.finalise(),
            #[cfg(feature = "threads-partitioning")]
            Self::Threads(reporter) => reporter.finalise(),
        }
    }
}