opencv_binding_generator/
lib.rs

1// todo support converting pointer + size to slice of Mat and other similar objects
2// todo add support for arrays in dnn::DictValue
3// todo allow ergonomically combining of enum variants with |
4// todo almost everything from the manual module must be connected to the binding generator, not the main crate
5// todo check that FN_FaceDetector works at all (receiving InputArray, passing as callback)
6// todo public static properties like opencv2/core/base.hpp:384 Hamming::normType
7// todo test returning reference to array like cv_MatStep_buf
8// todo, allow extension of simple classes for e.g. Elliptic_KeyPoint
9// fixme vector<Mat*> get's interpreted as Vector<Mat> which should be wrong (e.g. Layer::forward and Layer::apply_halide_scheduler)
10// fixme MatConstIterator::m return Mat**, is it handled correctly?
11// fixme VectorOfMat::get allows mutation
12// fixme MatSize looks like just a pointer inside a Mat so it should take a lifetime
13
14// copy-pasted form python generator (may be obsolete):
15// fixme returning MatAllocator (trait) by reference is bad, check knearestneighbour
16
17#![allow(clippy::nonminimal_bool)] // pattern `!type_ref.as_vector().is_some()` used for more clarity
18
19use std::borrow::Cow;
20use std::fs::File;
21use std::io::{BufRead, Read, Seek, SeekFrom};
22use std::ops::ControlFlow;
23
24pub use abstract_ref_wrapper::AbstractRefWrapper;
25use clang::Entity;
26pub use class::Class;
27pub use constant::Const;
28pub use element::{is_opencv_path, opencv_module_from_path, DefaultElement, Element, EntityElement};
29#[allow(unused)]
30use entity::dbg_clang_entity;
31pub use entity::EntityExt;
32pub use enumeration::Enum;
33use field::Field;
34pub use func::{Func, FuncTypeHint, Pred, UsageTracker};
35pub use generator::{GeneratedType, Generator, GeneratorVisitor, OpenCvWalker};
36pub use generator_env::{ClassKindOverride, ExportConfig, GeneratorEnv};
37pub use iterator_ext::IteratorExt;
38pub use map_borrowed::CowMapBorrowedExt;
39use memoize::{MemoizeMap, MemoizeMapExt};
40use smart_ptr::SmartPtr;
41pub use string_ext::{CompiledInterpolation, StrExt, StringExt};
42pub use supported_module::SupportedModule;
43use tuple::Tuple;
44#[allow(unused)]
45use type_ref::dbg_clang_type;
46use type_ref::TypeRef;
47pub use type_ref::{Constness, CppNameStyle, NameStyle};
48pub use typedef::Typedef;
49use vector::Vector;
50pub use walker::{EntityWalkerExt, EntityWalkerVisitor};
51
52use crate::debug::NameDebug;
53
54mod abstract_ref_wrapper;
55mod class;
56pub mod comment;
57mod constant;
58mod debug;
59mod element;
60mod entity;
61mod enumeration;
62mod field;
63mod func;
64mod function;
65mod generator;
66mod generator_env;
67mod iterator_ext;
68mod map_borrowed;
69mod memoize;
70mod name_pool;
71mod renderer;
72pub mod settings;
73mod smart_ptr;
74mod string_ext;
75mod supported_module;
76#[cfg(test)]
77mod test;
78mod tuple;
79mod type_ref;
80mod typedef;
81mod vector;
82mod walker;
83pub mod writer;
84
85fn get_definition_text(entity: Entity) -> String {
86	if let Some(range) = entity.get_range() {
87		let loc = range.get_start().get_spelling_location();
88		let start = loc.offset;
89		let end = range.get_end().get_spelling_location().offset;
90		let len = usize::try_from(end - start).expect("Definition span is too large");
91		let mut def_bytes = vec![0; len];
92		let mut source = File::open(loc.file.expect("Can't get file").get_path()).expect("Can't open source file");
93		source.seek(SeekFrom::Start(u64::from(start))).expect("Cannot seek");
94		source.read_exact(&mut def_bytes).expect("Can't read definition");
95		String::from_utf8(def_bytes).expect("Can't parse definition")
96	} else {
97		unreachable!("Can't get entity range: {:#?}", entity)
98	}
99}
100
101fn reserved_rename(val: Cow<str>) -> Cow<str> {
102	settings::RESERVED_RENAME.get(val.as_ref()).map_or(val, |&v| v.into())
103}
104
105#[inline(always)]
106pub fn line_reader(mut b: impl BufRead, mut cb: impl FnMut(&str) -> ControlFlow<()>) {
107	let mut line = String::with_capacity(256);
108	while let Ok(bytes_read) = b.read_line(&mut line) {
109		if bytes_read == 0 {
110			break;
111		}
112		if cb(&line).is_break() {
113			break;
114		}
115		line.clear();
116	}
117}