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
use std::{ops::Deref};
use blaze_proc::docfg;
use crate::{core::*};
use super::{Context, RawContext, ContextProperties, CommandQueue};

#[doc = include_str!("../../docs/src/context/simple.md")]
#[derive(Clone)]
pub struct SimpleContext {
    ctx: RawContext,
    queue: CommandQueue
}

impl SimpleContext {
    pub fn new (device: &RawDevice, ctx_props: ContextProperties, props: impl Into<QueueProperties>) -> Result<Self> {
        let ctx = RawContext::new(ctx_props, core::slice::from_ref(device))?;
        let queue = RawCommandQueue::new(&ctx, props.into(), device).map(CommandQueue::new)?;
        Ok(Self { ctx, queue })
    }

    #[docfg(feature = "cl3")]
    pub fn with_logger (device: &RawDevice, ctx_props: ContextProperties, props: impl Into<QueueProperties>, loger: impl 'static + Fn(&std::ffi::CStr) + Send) -> Result<Self> {
        let ctx = RawContext::with_logger(ctx_props, core::slice::from_ref(device), loger)?;
        let queue = RawCommandQueue::new(&ctx, props.into(), device).map(CommandQueue::new)?;
        Ok(Self { ctx, queue })
    }

    #[inline(always)]
    pub fn default() -> Result<Self> {
        let device = RawDevice::first().ok_or(ErrorKind::InvalidDevice)?;

        cfg_if::cfg_if! {
            if #[cfg(all(debug_assertions, feature = "cl3"))] {
                Self::with_logger(device, ContextProperties::default(), QueueProperties::default(), |x| println!("{x:?}"))
            } else {
                Self::new(device, ContextProperties::default(), QueueProperties::default())
            }
        }
    }
}

impl Context for SimpleContext {
    #[inline(always)]
    fn as_raw (&self) -> &RawContext {
        &self.ctx
    }

    #[inline(always)]
    fn queues (&self) -> &[CommandQueue] {
        core::slice::from_ref(&self.queue)
    }

    #[inline(always)]
    fn next_queue (&self) -> &CommandQueue {
        &self.queue
    }
}

impl Deref for SimpleContext {
    type Target = RawContext;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.ctx
    }
}