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