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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::ffi::c_void;
pub trait Boxed: Sized {
unsafe fn from_raw(ptr: *mut c_void) -> Self;
fn into_raw(self) -> *mut c_void;
fn as_raw(&self) -> *const c_void;
fn as_raw_mut(&mut self) -> *mut c_void;
}
#[macro_export]
macro_rules! opencv_type_boxed {
($type: ty) => {
impl $crate::traits::Boxed for $type {
#[inline]
unsafe fn from_raw(ptr: *mut ::std::ffi::c_void) -> Self {
Self { ptr }
}
#[inline]
fn into_raw(self) -> *mut ::std::ffi::c_void {
::std::mem::ManuallyDrop::new(self).ptr
}
#[inline]
fn as_raw(&self) -> *const ::std::ffi::c_void {
self.ptr
}
#[inline]
fn as_raw_mut(&mut self) -> *mut ::std::ffi::c_void {
self.ptr
}
}
impl $crate::traits::OpenCVType<'_> for $type {
type Arg = Self;
type ExternReceive = *mut ::std::ffi::c_void;
#[inline]
unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {
Self::from_raw(s)
}
}
impl $crate::traits::OpenCVTypeArg<'_> for $type {
type ExternContainer = Self;
#[inline]
fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
self
}
}
impl $crate::traits::OpenCVTypeExternContainer for $type {
type ExternSend = *const ::std::ffi::c_void;
type ExternSendMut = *mut ::std::ffi::c_void;
#[inline]
fn opencv_as_extern(&self) -> Self::ExternSend {
self.as_raw()
}
#[inline]
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self.as_raw_mut()
}
#[inline]
fn opencv_into_extern(self) -> Self::ExternSendMut {
self.into_raw()
}
}
};
}
#[macro_export]
macro_rules! boxed_cast_base {
($type: ty, $base: ty, $extern_convert: ident $(,)?) => {
impl ::std::convert::From<$type> for $base {
#[inline]
fn from(s: $type) -> Self {
extern "C" {
fn $extern_convert(val: *mut ::std::ffi::c_void) -> *mut ::std::ffi::c_void;
}
unsafe { Self::from_raw($extern_convert(s.into_raw())) }
}
}
};
}
#[macro_export]
macro_rules! boxed_cast_descendant {
($type: ty, $descendant: ty, $extern_convert: ident $(,)?) => {
impl ::std::convert::TryFrom<$type> for $descendant {
type Error = $crate::Error;
#[inline]
fn try_from(s: $type) -> $crate::Result<Self> {
extern "C" {
fn $extern_convert(val: *mut ::std::ffi::c_void) -> *mut ::std::ffi::c_void;
}
let ret = unsafe { $extern_convert(s.into_raw()) };
if ret.is_null() {
Err($crate::Error::new(
$crate::core::StsBadArg,
format!(
"Unable to cast base class: {} to: {}",
stringify!($type),
stringify!($descendant)
),
))
} else {
Ok(unsafe { Self::from_raw(ret) })
}
}
}
};
}