opencv_binding_generator/
settings.rs

1// todo add doccomments
2
3macro_rules! pred {
4	($args: expr $(,)?) => {
5		[$crate::func::Pred::ArgNames(&$args)].as_slice()
6	};
7	($args: expr, $types: expr $(,)?) => {
8		[$crate::func::Pred::ArgNames(&$args), $crate::func::Pred::ArgTypes(&$types)].as_slice()
9	};
10	(const, $args: expr $(,)?) => {
11		[
12			$crate::func::Pred::Constness($crate::type_ref::Constness::Const),
13			$crate::func::Pred::ArgNames(&$args),
14		]
15		.as_slice()
16	};
17	(const, $args: expr, $types: expr $(,)?) => {
18		[
19			$crate::func::Pred::Constness($crate::type_ref::Constness::Const),
20			$crate::func::Pred::ArgNames(&$args),
21			$crate::func::Pred::ArgTypes(&$types),
22		]
23		.as_slice()
24	};
25	(mut, $args: expr $(,)?) => {
26		[
27			$crate::func::Pred::Constness($crate::type_ref::Constness::Mut),
28			$crate::func::Pred::ArgNames(&$args),
29		]
30		.as_slice()
31	};
32	(mut, $args: expr, $types: expr $(,)?) => {
33		[
34			$crate::func::Pred::Constness($crate::type_ref::Constness::Mut),
35			$crate::func::Pred::ArgNames(&$args),
36			$crate::func::Pred::ArgTypes(&$types),
37		]
38		.as_slice()
39	};
40}
41
42use std::collections::{BTreeSet, HashMap, HashSet};
43
44pub use argument_names::{ARGUMENT_NAMES_MULTIPLE_SLICE, ARGUMENT_NAMES_NOT_SLICE, ARGUMENT_NAMES_USERDATA};
45pub use argument_override::{
46	arg_override_factory, property_override_factory, return_override_factory, ArgOverride, PropertyOverride, ReturnOverride,
47	ARG_OVERRIDE_SELF,
48};
49pub use const_tweak::CONST_TYPE_OVERRIDE;
50pub use element_exclude_kind::ELEMENT_EXCLUDE_KIND;
51pub use element_export_tweak::ELEMENT_EXPORT_TWEAK;
52pub use force_infallible::{force_infallible_factory, ForceInfallible};
53pub use func_cfg_attr::{func_cfg_attr_factory, FuncCfgAttr, CFG_ATTR_NOT_ON_WINDOWS, CFG_ATTR_ONLY_OPENCV_5};
54pub use func_companion_tweak::{func_companion_tweak_factory, CompanionTweak, FuncCompanionTweak};
55pub use func_exclude::{func_exclude_factory, FuncExclude};
56pub use func_inject::{func_inject_factory, FuncFactory, FuncInject};
57pub use func_rename::{func_rename_factory, FuncRename};
58pub use func_replace::{func_replace_factory, FuncInheritFactory, FuncReplace};
59pub use func_specialize::{func_specialize_factory, FuncSpec, FuncSpecialize};
60pub use func_unsafe::{func_unsafe_factory, FuncUnsafe};
61pub use generator_module_tweaks::{generator_module_tweaks_factory, ModuleTweak};
62pub use implemented::{
63	IMPLEMENTED_CONST_GENERICS, IMPLEMENTED_FUNCTION_LIKE_MACROS, IMPLEMENTED_GENERICS, IMPLEMENTED_MANUAL_DEBUG,
64	IMPLEMENTED_SYSTEM_CLASSES,
65};
66use once_cell::sync::Lazy;
67pub use property_tweaks::{property_tweaks_factory, PropertyReadWrite, PropertyTweak, PropertyTweaks};
68
69use crate::func::{FuncMatcher, UsageTracker};
70use crate::type_ref::TypeRef;
71
72mod argument_names;
73mod argument_override;
74mod const_tweak;
75mod element_exclude_kind;
76mod element_export_tweak;
77mod force_infallible;
78mod func_cfg_attr;
79mod func_companion_tweak;
80mod func_exclude;
81mod func_inject;
82mod func_rename;
83mod func_replace;
84mod func_specialize;
85mod func_unsafe;
86mod generator_module_tweaks;
87mod implemented;
88mod property_tweaks;
89
90pub type TypeRefFactory = fn() -> TypeRef<'static, 'static>;
91
92/// Injectable global and module level overrides, todo: migrate the global statics to this over time
93#[derive(Debug)]
94pub struct Settings {
95	pub arg_override: ArgOverride,
96	pub return_override: ReturnOverride,
97	pub force_infallible: ForceInfallible,
98	pub func_cfg_attr: FuncCfgAttr,
99	pub func_companion_tweak: FuncCompanionTweak,
100	pub func_exclude: FuncExclude,
101	pub func_inject: FuncInject,
102	pub func_rename: FuncRename,
103	pub func_replace: FuncReplace,
104	pub func_specialize: FuncSpecialize,
105	pub func_unsafe: FuncUnsafe,
106	pub generator_module_tweaks: ModuleTweak<'static>,
107	pub property_override: PropertyOverride,
108	pub property_tweaks: PropertyTweaks,
109}
110
111impl Settings {
112	pub fn empty() -> Self {
113		Self {
114			arg_override: ArgOverride::empty(),
115			return_override: ReturnOverride::empty(),
116			force_infallible: ForceInfallible::empty(),
117			func_cfg_attr: FuncCfgAttr::empty(),
118			func_companion_tweak: FuncCompanionTweak::empty(),
119			func_exclude: FuncExclude::default(),
120			func_inject: FuncInject::default(),
121			func_rename: FuncRename::default(),
122			func_replace: FuncReplace::empty(),
123			func_specialize: FuncMatcher::empty(),
124			func_unsafe: FuncUnsafe::empty(),
125			generator_module_tweaks: ModuleTweak::empty(),
126			property_override: PropertyOverride::default(),
127			property_tweaks: PropertyTweaks::default(),
128		}
129	}
130
131	pub fn for_module(module: &str) -> Self {
132		Self {
133			arg_override: arg_override_factory(module),
134			return_override: return_override_factory(module),
135			force_infallible: force_infallible_factory(module),
136			func_cfg_attr: func_cfg_attr_factory(module),
137			func_companion_tweak: func_companion_tweak_factory(module),
138			func_exclude: func_exclude_factory(module),
139			func_inject: func_inject_factory(module),
140			func_rename: func_rename_factory(module),
141			func_replace: func_replace_factory(module),
142			func_specialize: func_specialize_factory(module),
143			func_unsafe: func_unsafe_factory(module),
144			generator_module_tweaks: generator_module_tweaks_factory(module),
145			property_override: property_override_factory(module),
146			property_tweaks: property_tweaks_factory(module),
147		}
148	}
149
150	pub fn start_usage_tracking(&mut self) {
151		self.arg_override.start_usage_tracking();
152		self.return_override.start_usage_tracking();
153		self.force_infallible.start_usage_tracking();
154		self.func_companion_tweak.start_usage_tracking();
155		self.func_replace.start_usage_tracking();
156		self.func_specialize.start_usage_tracking();
157		self.func_unsafe.start_usage_tracking();
158	}
159
160	pub fn finish_usage_tracking(&mut self) -> HashMap<&'static str, HashSet<UsageTracker>> {
161		HashMap::from([
162			("ARG_OVERRIDE", self.arg_override.finish_usage_tracking()),
163			("RETURN_OVERRIDE", self.return_override.finish_usage_tracking()),
164			("FORCE_INFALLIBLE", self.force_infallible.finish_usage_tracking()),
165			("FUNC_COMPANION_TWEAK", self.func_companion_tweak.finish_usage_tracking()),
166			("FUNC_REPLACE", self.func_replace.finish_usage_tracking()),
167			("FUNC_SPECIALIZE", self.func_specialize.finish_usage_tracking()),
168			("FUNC_UNSAFE", self.func_unsafe.finish_usage_tracking()),
169		])
170	}
171}
172
173/// map of reserved Rust keywords and their replacement to be used in var, function and class names
174/// key: reserved keyword
175/// value: replacement
176pub static RESERVED_RENAME: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
177	HashMap::from([
178		("box", "box_"),
179		("fn", "fn_"),
180		("in", "in_"),
181		("match", "match_"),
182		("move", "move_"),
183		("ref", "ref_"),
184		("type", "typ"),
185		("use", "use_"),
186		("impl", "impl_"),
187		("loop", "loop_"),
188		("yield", "yield_"),
189		("where", "where_"),
190	])
191});
192
193/// cpp_name(Reference) => ( rust_name(Reference(No)), cpp_name(Reference) )
194pub static PRIMITIVE_TYPEDEFS: Lazy<HashMap<&str, (&str, &str)>> = Lazy::new(|| {
195	HashMap::from([
196		("size_t", ("size_t", "size_t")),
197		("ptrdiff_t", ("ptrdiff_t", "ptrdiff_t")),
198		("clock_t", ("clock_t", "clock_t")),
199		("schar", ("i8", "signed char")),
200		("uchar", ("u8", "unsigned char")),
201		("uint8_t", ("u8", "uint8_t")),
202		("uint16_t", ("u16", "uint16_t")),
203		("uint", ("u32", "unsigned int")),
204		("uint32_t", ("u32", "uint32_t")),
205		("int64_t", ("i64", "int64_t")),
206		("int64", ("i64", "int64_t")),
207		("uint64_t", ("u64", "uint64_t")),
208		("uint64", ("u64", "uint64_t")),
209		("ushort", ("u16", "unsigned short")),
210	])
211});
212
213pub static STATIC_MODULES: Lazy<BTreeSet<&str>> = Lazy::new(|| BTreeSet::from(["core", "sys", "types"]));
214
215/// Types that can be used as `Mat` element
216/// cpp_name(Reference)
217pub static DATA_TYPES: Lazy<HashSet<&str>> = Lazy::new(|| {
218	HashSet::from([
219		"unsigned char",
220		"char",
221		"uint8_t",
222		"int8_t",
223		"unsigned short",
224		"short",
225		"uint16_t",
226		"int16_t",
227		"unsigned int",
228		"int",
229		"int32_t",
230		"float",
231		"double",
232		"hfloat",
233		"float16_t",
234		"__fp16",
235		"cv::Vec",
236		"cv::Scalar_",
237		"cv::Point_",
238		"cv::Point3_",
239		"cv::Size_",
240		"cv::Rect_",
241	])
242});
243
244/// Types that can be used as `Mat` element since OpenCV 5.0
245/// cpp_name(Reference)
246pub static DATA_TYPES_5_0: Lazy<HashSet<&str>> =
247	Lazy::new(|| HashSet::from(["uint32_t", "bfloat", "bfloat16_t", "uint64_t", "int64_t", "bool"]));
248
249pub static NO_SKIP_NAMESPACE_IN_LOCALNAME: Lazy<HashMap<&str, HashMap<&str, &str>>> = Lazy::new(|| {
250	HashMap::from([
251		("*", HashMap::from([("detail", "Detail")])),
252		("calib3d", HashMap::from([("fisheye", "Fisheye")])),
253		("cudabgsegm", HashMap::from([("cuda", "CUDA")])),
254		("cudacodec", HashMap::from([("cudacodec", "CUDA")])),
255		("cudafeatures2d", HashMap::from([("cuda", "CUDA")])),
256		("cudaimgproc", HashMap::from([("cuda", "CUDA")])),
257		("cudalegacy", HashMap::from([("cuda", "CUDA")])),
258		("cudaobjdetect", HashMap::from([("cuda", "CUDA")])),
259		("cudaoptflow", HashMap::from([("cuda", "CUDA")])),
260		("cudastereo", HashMap::from([("cuda", "CUDA")])),
261		("gapi", HashMap::from([("imgproc", "ImgProc")])),
262		("mcc", HashMap::from([("mcc", "MCC")])),
263		("rapid", HashMap::from([("rapid", "Rapid")])),
264		(
265			"rgbd",
266			HashMap::from([
267				("dynafu", "Dynafu"),
268				("kinfu", "Kinfu"),
269				("colored_kinfu", "ColoredKinfu"),
270				("linemod", "LineMod"),
271			]),
272		),
273		("stitching", HashMap::from([("fisheye", "Fisheye")])),
274		("superres", HashMap::from([("superres", "SuperRes")])),
275	])
276});
277
278pub static PREVENT_VECTOR_TYPEDEF_GENERATION: Lazy<HashSet<&str>> = Lazy::new(|| {
279	HashSet::from([
280		// `MatShape` is an alias to `Vector<i32>` and this leads to duplication of definition for the `Vector<Vector<i32>>` type
281		"cv::dnn::MatShape",
282		// `MatType` is an alias `i32` and we don't want to duplicate `Vector<i32>` with `Vector<MatType>`
283		"cv::dnn::MatType",
284	])
285});