open_cl_low_level/
object_wrapper.rs1use std::fmt;
2use crate::{ClObject, RetainRelease, CheckValidClObject, Output};
3
4
5pub struct ObjectWrapper<T: ClObject + RetainRelease + CheckValidClObject> {
6 object: T
7}
8
9impl<T: ClObject + RetainRelease + CheckValidClObject> ObjectWrapper<T> {
10 pub unsafe fn unchecked_new(object: T) -> Self {
11 ObjectWrapper { object }
12 }
13
14 pub unsafe fn cl_object(&self) -> T {
15 self.object
16 }
17
18 pub unsafe fn cl_object_ref(&self) -> &T {
19 &self.object
20 }
21
22 pub unsafe fn cl_object_mut_ref(&mut self) -> &mut T {
23 &mut self.object
24 }
25
26 pub unsafe fn new(object: T) -> Output<Self> {
27 object.check_valid_cl_object()?;
28 Ok(Self::unchecked_new(object))
29 }
30
31 pub unsafe fn retain_new(object: T) -> Output<Self> {
32 object.check_valid_cl_object()?;
33 object.retain();
34 Ok(Self::unchecked_new(object))
35 }
36
37 pub fn address(&self) -> String {
38 self.object.address()
39 }
40
41 pub fn type_name(&self) -> &'static str {
42 self.object.type_name()
43 }
44}
45
46impl<T: RetainRelease> Drop for ObjectWrapper<T> {
47 fn drop(&mut self) {
48 unsafe { self.cl_object().release(); }
49 }
50}
51
52impl<T: RetainRelease> Clone for ObjectWrapper<T> {
53 fn clone(&self) -> ObjectWrapper<T> {
54 let object = self.object;
55 unsafe { object.retain() };
56 ObjectWrapper{ object }
57 }
58}
59
60impl<T: ClObject + RetainRelease> PartialEq for ObjectWrapper<T> {
61 fn eq(&self, other: &Self) -> bool {
62 unsafe { self.cl_object() == other.cl_object() }
63 }
64}
65
66impl<T: ClObject + RetainRelease> Eq for ObjectWrapper<T> {}
67
68impl<T: ClObject + RetainRelease> fmt::Debug for ObjectWrapper<T> {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(f, "{:?} at {:?}", self.type_name(), self.address())
71 }
72}