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