Skip to main content

opencv_binding_generator/
settings.rs

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