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
use std::{
borrow::Cow,
fmt,
};
use clang::{Entity, EntityKind, EntityVisitResult, Type};
use crate::{
Element,
Field,
GeneratorEnv,
IteratorExt,
type_ref::{FishStyle, NameStyle},
TypeRef,
};
#[derive(Clone)]
pub struct Function<'tu, 'ge> {
type_ref: Type<'tu>,
parent_entity: Entity<'tu>,
gen_env: &'ge GeneratorEnv<'tu>,
}
impl<'tu, 'ge> Function<'tu, 'ge> {
pub fn new(type_ref: Type<'tu>, parent_entity: Entity<'tu>, gen_env: &'ge GeneratorEnv<'tu>) -> Self {
Self { type_ref, parent_entity, gen_env }
}
pub fn arguments(&self) -> Vec<Field<'tu, 'ge>> {
let mut out = Vec::with_capacity(10);
self.parent_entity.visit_children(|c, _| {
if c.get_kind() == EntityKind::ParmDecl {
out.push(Field::new(c, self.gen_env));
}
EntityVisitResult::Continue
});
out
}
pub fn rust_arguments(&self) -> Vec<Field<'tu, 'ge>> {
self.arguments().into_iter()
.filter(|a| !a.is_user_data())
.collect()
}
pub fn has_userdata(&self) -> bool {
self.arguments().into_iter().any(|f| f.is_user_data())
}
pub fn return_type(&self) -> TypeRef<'tu, 'ge> {
TypeRef::new(self.type_ref.get_result_type().expect("Can't get result type"), self.gen_env)
}
pub fn rust_extern(&self) -> Cow<str> {
let args = self.arguments().into_iter()
.map(|a| a.type_ref().rust_extern().into_owned())
.join(", ");
let ret = self.return_type();
format!(r#"Option<unsafe extern "C" fn({args}) -> {ret}>"#, args=args, ret=ret.rust_extern()).into()
}
}
impl Element for Function<'_, '_> {
fn is_system(&self) -> bool {
false
}
fn is_public(&self) -> bool {
true
}
fn usr(&self) -> Cow<str> {
"".into()
}
fn rendered_doc_comment_with_prefix(&self, _prefix: &str, _opencv_version: &str) -> String {
"".to_string()
}
fn cpp_namespace(&self) -> Cow<str> {
"<unset>".into()
}
fn cpp_name(&self, _style: NameStyle) -> Cow<str> {
self.cpp_localname()
}
fn cpp_localname(&self) -> Cow<str> {
let args = self.arguments().into_iter()
.map(|a| a.type_ref().cpp_full_ext("", false).into_owned())
.join(", ");
let ret = self.return_type();
format!("{ret} (*)({args})", args=args, ret=ret.cpp_full()).into()
}
fn rust_module(&self) -> Cow<str> {
"<unset>".into()
}
fn rust_name(&self, _style: NameStyle) -> Cow<str> {
self.rust_localname(FishStyle::No)
}
fn rust_localname(&self, fish_style: FishStyle) -> Cow<str> {
let ret = self.return_type();
if self.has_userdata() {
let args = self.rust_arguments().into_iter()
.map(|a| a.type_ref().rust_extern().into_owned())
.join(", ");
format!(
"Option{fish}<Box{fish}<dyn FnMut({args}) -> {ret} + Send + Sync + 'static>>",
fish = fish_style.rust_qual(),
args = args,
ret = ret.rust_extern(),
).into()
} else {
self.rust_extern()
}
}
}
impl fmt::Display for Function<'_, '_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.cpp_fullname())
}
}
impl fmt::Debug for Function<'_, '_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut debug_struct = f.debug_struct("Function");
self.update_debug_struct(&mut debug_struct)
.field("arguments", &self.arguments())
.finish()
}
}