opencv_binding_generator/
settings.rs

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// todo add doccomments

macro_rules! pred {
	($args: expr $(,)?) => {
		[$crate::func::Pred::ArgNames(&$args)].as_slice()
	};
	($args: expr, $types: expr $(,)?) => {
		[$crate::func::Pred::ArgNames(&$args), $crate::func::Pred::ArgTypes(&$types)].as_slice()
	};
	(const, $args: expr $(,)?) => {
		[
			$crate::func::Pred::Constness($crate::type_ref::Constness::Const),
			$crate::func::Pred::ArgNames(&$args),
		]
		.as_slice()
	};
	(const, $args: expr, $types: expr $(,)?) => {
		[
			$crate::func::Pred::Constness($crate::type_ref::Constness::Const),
			$crate::func::Pred::ArgNames(&$args),
			$crate::func::Pred::ArgTypes(&$types),
		]
		.as_slice()
	};
	(mut, $args: expr $(,)?) => {
		[
			$crate::func::Pred::Constness($crate::type_ref::Constness::Mut),
			$crate::func::Pred::ArgNames(&$args),
		]
		.as_slice()
	};
	(mut, $args: expr, $types: expr $(,)?) => {
		[
			$crate::func::Pred::Constness($crate::type_ref::Constness::Mut),
			$crate::func::Pred::ArgNames(&$args),
			$crate::func::Pred::ArgTypes(&$types),
		]
		.as_slice()
	};
}

use std::collections::{BTreeSet, HashMap, HashSet};

pub use argument_names::{ARGUMENT_NAMES_MULTIPLE_SLICE, ARGUMENT_NAMES_NOT_SLICE, ARGUMENT_NAMES_USERDATA};
pub use argument_override::{
	arg_override_factory, property_override_factory, return_override_factory, ArgOverride, PropertyOverride, ReturnOverride,
	ARG_OVERRIDE_SELF,
};
pub use element_exclude_kind::ELEMENT_EXCLUDE_KIND;
pub use element_export_tweak::ELEMENT_EXPORT_TWEAK;
pub use force_infallible::{force_infallible_factory, ForceInfallible};
pub use func_cfg_attr::{func_cfg_attr_factory, FuncCfgAttr, CFG_ATTR_NOT_ON_WINDOWS, CFG_ATTR_ONLY_OPENCV_5};
pub use func_companion_tweak::{func_companion_tweak_factory, CompanionTweak, FuncCompanionTweak};
pub use func_exclude::{func_exclude_factory, FuncExclude};
pub use func_inject::{func_inject_factory, FuncFactory, FuncInject};
pub use func_rename::{func_rename_factory, FuncRename};
pub use func_replace::{func_replace_factory, FuncInheritFactory, FuncReplace};
pub use func_specialize::{func_specialize_factory, FuncSpec, FuncSpecialize};
pub use func_unsafe::{func_unsafe_factory, FuncUnsafe};
pub use generator_module_tweaks::{generator_module_tweaks_factory, ModuleTweak};
pub use implemented::{
	IMPLEMENTED_CONST_GENERICS, IMPLEMENTED_FUNCTION_LIKE_MACROS, IMPLEMENTED_GENERICS, IMPLEMENTED_MANUAL_DEBUG,
	IMPLEMENTED_SYSTEM_CLASSES,
};
use once_cell::sync::Lazy;
pub use property_tweaks::{property_tweaks_factory, PropertyReadWrite, PropertyTweak, PropertyTweaks};

use crate::func::{FuncMatcher, UsageTracker};
use crate::type_ref::TypeRef;

mod argument_names;
mod argument_override;
mod element_exclude_kind;
mod element_export_tweak;
mod force_infallible;
mod func_cfg_attr;
mod func_companion_tweak;
mod func_exclude;
mod func_inject;
mod func_rename;
mod func_replace;
mod func_specialize;
mod func_unsafe;
mod generator_module_tweaks;
mod implemented;
mod property_tweaks;

pub type TypeRefFactory = fn() -> TypeRef<'static, 'static>;

/// Injectable global and module level overrides, todo: migrate the global statics to this over time
#[derive(Debug)]
pub struct Settings {
	pub arg_override: ArgOverride,
	pub return_override: ReturnOverride,
	pub force_infallible: ForceInfallible,
	pub func_cfg_attr: FuncCfgAttr,
	pub func_companion_tweak: FuncCompanionTweak,
	pub func_exclude: FuncExclude,
	pub func_inject: FuncInject,
	pub func_rename: FuncRename,
	pub func_replace: FuncReplace,
	pub func_specialize: FuncSpecialize,
	pub func_unsafe: FuncUnsafe,
	pub generator_module_tweaks: ModuleTweak<'static>,
	pub property_override: PropertyOverride,
	pub property_tweaks: PropertyTweaks,
}

impl Settings {
	pub fn empty() -> Self {
		Self {
			arg_override: ArgOverride::empty(),
			return_override: ReturnOverride::empty(),
			force_infallible: ForceInfallible::empty(),
			func_cfg_attr: FuncCfgAttr::empty(),
			func_companion_tweak: FuncCompanionTweak::empty(),
			func_exclude: FuncExclude::default(),
			func_inject: FuncInject::default(),
			func_rename: FuncRename::default(),
			func_replace: FuncReplace::empty(),
			func_specialize: FuncMatcher::empty(),
			func_unsafe: FuncUnsafe::empty(),
			generator_module_tweaks: ModuleTweak::empty(),
			property_override: PropertyOverride::default(),
			property_tweaks: PropertyTweaks::default(),
		}
	}

	pub fn for_module(module: &str) -> Self {
		Self {
			arg_override: arg_override_factory(module),
			return_override: return_override_factory(module),
			force_infallible: force_infallible_factory(module),
			func_cfg_attr: func_cfg_attr_factory(module),
			func_companion_tweak: func_companion_tweak_factory(module),
			func_exclude: func_exclude_factory(module),
			func_inject: func_inject_factory(module),
			func_rename: func_rename_factory(module),
			func_replace: func_replace_factory(module),
			func_specialize: func_specialize_factory(module),
			func_unsafe: func_unsafe_factory(module),
			generator_module_tweaks: generator_module_tweaks_factory(module),
			property_override: property_override_factory(module),
			property_tweaks: property_tweaks_factory(module),
		}
	}

	pub fn start_usage_tracking(&mut self) {
		self.arg_override.start_usage_tracking();
		self.return_override.start_usage_tracking();
		self.force_infallible.start_usage_tracking();
		self.func_companion_tweak.start_usage_tracking();
		self.func_replace.start_usage_tracking();
		self.func_specialize.start_usage_tracking();
		self.func_unsafe.start_usage_tracking();
	}

	pub fn finish_usage_tracking(&mut self) -> HashMap<&'static str, HashSet<UsageTracker>> {
		HashMap::from([
			("ARG_OVERRIDE", self.arg_override.finish_usage_tracking()),
			("RETURN_OVERRIDE", self.return_override.finish_usage_tracking()),
			("FORCE_INFALLIBLE", self.force_infallible.finish_usage_tracking()),
			("FUNC_COMPANION_TWEAK", self.func_companion_tweak.finish_usage_tracking()),
			("FUNC_REPLACE", self.func_replace.finish_usage_tracking()),
			("FUNC_SPECIALIZE", self.func_specialize.finish_usage_tracking()),
			("FUNC_UNSAFE", self.func_unsafe.finish_usage_tracking()),
		])
	}
}

// fixme, generalize, make it use constant::ValueKind
pub static CONST_TYPE_USIZE: Lazy<HashSet<&str>> = Lazy::new(|| HashSet::from(["Mat_AUTO_STEP"]));

/// map of reserved Rust keywords and their replacement to be used in var, function and class names
/// key: reserved keyword
/// value: replacement
pub static RESERVED_RENAME: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
	HashMap::from([
		("box", "box_"),
		("fn", "fn_"),
		("in", "in_"),
		("match", "match_"),
		("move", "move_"),
		("ref", "ref_"),
		("type", "typ"),
		("use", "use_"),
		("impl", "impl_"),
		("loop", "loop_"),
		("yield", "yield_"),
		("where", "where_"),
	])
});

/// cpp_name(Reference) => ( rust_name(Reference(No)), cpp_name(Reference) )
pub static PRIMITIVE_TYPEDEFS: Lazy<HashMap<&str, (&str, &str)>> = Lazy::new(|| {
	HashMap::from([
		("size_t", ("size_t", "size_t")),
		("ptrdiff_t", ("ptrdiff_t", "ptrdiff_t")),
		("clock_t", ("clock_t", "clock_t")),
		("schar", ("i8", "signed char")),
		("uchar", ("u8", "unsigned char")),
		("uint8_t", ("u8", "uint8_t")),
		("uint16_t", ("u16", "uint16_t")),
		("uint", ("u32", "unsigned int")),
		("uint32_t", ("u32", "uint32_t")),
		("int64_t", ("i64", "int64_t")),
		("int64", ("i64", "int64_t")),
		("uint64_t", ("u64", "uint64_t")),
		("uint64", ("u64", "uint64_t")),
		("ushort", ("u16", "unsigned short")),
	])
});

pub static STATIC_MODULES: Lazy<BTreeSet<&str>> = Lazy::new(|| BTreeSet::from(["core", "sys", "types"]));

/// Types that can be used as `Mat` element
/// cpp_name(Reference)
pub static DATA_TYPES: Lazy<HashSet<&str>> = Lazy::new(|| {
	HashSet::from([
		"unsigned char",
		"char",
		"uint8_t",
		"int8_t",
		"unsigned short",
		"short",
		"uint16_t",
		"int16_t",
		"unsigned int",
		"int",
		"int32_t",
		"float",
		"double",
		"hfloat",
		"float16_t",
		"__fp16",
		"cv::Vec",
		"cv::Scalar_",
		"cv::Point_",
		"cv::Point3_",
		"cv::Size_",
		"cv::Rect_",
	])
});

/// Types that can be used as `Mat` element since OpenCV 5.0
/// cpp_name(Reference)
pub static DATA_TYPES_5_0: Lazy<HashSet<&str>> =
	Lazy::new(|| HashSet::from(["uint32_t", "bfloat", "bfloat16_t", "uint64_t", "int64_t", "bool"]));

pub static NO_SKIP_NAMESPACE_IN_LOCALNAME: Lazy<HashMap<&str, HashMap<&str, &str>>> = Lazy::new(|| {
	HashMap::from([
		("*", HashMap::from([("detail", "Detail")])),
		("calib3d", HashMap::from([("fisheye", "Fisheye")])),
		("cudabgsegm", HashMap::from([("cuda", "CUDA")])),
		("cudacodec", HashMap::from([("cudacodec", "CUDA")])),
		("cudafeatures2d", HashMap::from([("cuda", "CUDA")])),
		("cudaimgproc", HashMap::from([("cuda", "CUDA")])),
		("cudalegacy", HashMap::from([("cuda", "CUDA")])),
		("cudaobjdetect", HashMap::from([("cuda", "CUDA")])),
		("cudaoptflow", HashMap::from([("cuda", "CUDA")])),
		("cudastereo", HashMap::from([("cuda", "CUDA")])),
		("gapi", HashMap::from([("imgproc", "ImgProc")])),
		("mcc", HashMap::from([("mcc", "MCC")])),
		("rapid", HashMap::from([("rapid", "Rapid")])),
		(
			"rgbd",
			HashMap::from([
				("dynafu", "Dynafu"),
				("kinfu", "Kinfu"),
				("colored_kinfu", "ColoredKinfu"),
				("linemod", "LineMod"),
			]),
		),
		("stitching", HashMap::from([("fisheye", "Fisheye")])),
		("superres", HashMap::from([("superres", "SuperRes")])),
	])
});

pub static PREVENT_VECTOR_TYPEDEF_GENERATION: Lazy<HashSet<&str>> = Lazy::new(|| {
	HashSet::from([
		// `MatShape` is an alias to `Vector<i32>` and this leads to duplication of definition for the `Vector<Vector<i32>>` type
		"cv::dnn::MatShape",
		// `MatType` is an alias `i32` and we don't want to duplicate `Vector<i32>` with `Vector<MatType>`
		"cv::dnn::MatType",
	])
});