Skip to main content

opencv_binding_generator/
vector.rs

1use std::borrow::Cow;
2use std::fmt;
3use std::rc::Rc;
4
5use clang::Type;
6pub use desc::VectorDesc;
7
8use crate::element::ExcludeKind;
9use crate::type_ref::{Constness, CppNameStyle, TemplateArg, TypeRefDesc, TypeRefKind};
10use crate::{DefaultElement, Element, GeneratedType, GeneratorEnv, StrExt, TypeRef};
11
12mod desc;
13
14#[derive(Clone)]
15pub enum Vector<'tu, 'ge> {
16	Clang {
17		type_ref: Type<'tu>,
18		gen_env: &'ge GeneratorEnv<'tu>,
19	},
20	Desc(Rc<VectorDesc<'tu, 'ge>>),
21}
22
23impl<'tu, 'ge> Vector<'tu, 'ge> {
24	pub fn new(type_ref: Type<'tu>, gen_env: &'ge GeneratorEnv<'tu>) -> Self {
25		Self::Clang { type_ref, gen_env }
26	}
27
28	pub fn new_desc(desc: VectorDesc<'tu, 'ge>) -> Self {
29		Self::Desc(Rc::new(desc))
30	}
31
32	pub fn type_ref(&self) -> TypeRef<'tu, 'ge> {
33		match self {
34			&Vector::Clang { type_ref, gen_env } => TypeRef::new(type_ref, gen_env),
35			Vector::Desc(desc) => TypeRef::new_desc(TypeRefDesc::new(
36				TypeRefKind::StdVector(Vector::Desc(Rc::clone(desc))),
37				Constness::Mut,
38			)),
39		}
40	}
41
42	pub fn element_type(&self) -> TypeRef<'tu, 'ge> {
43		match self {
44			&Vector::Clang { type_ref, gen_env } => TypeRef::new(type_ref, gen_env)
45				.template_specialization_args()
46				.into_owned()
47				.into_iter()
48				.find_map(TemplateArg::into_typename)
49				.expect("vector template argument list is empty"),
50			Vector::Desc(desc) => desc.element_type.clone(),
51		}
52	}
53
54	pub fn generated_types(&self) -> Vec<GeneratedType<'tu, 'ge>> {
55		self.element_type().generated_types()
56	}
57}
58
59impl Element for Vector<'_, '_> {
60	fn exclude_kind(&self) -> ExcludeKind {
61		DefaultElement::exclude_kind(self).with_exclude_kind(|| self.element_type().exclude_kind())
62	}
63
64	fn is_system(&self) -> bool {
65		true
66	}
67
68	fn is_public(&self) -> bool {
69		true
70	}
71
72	fn doc_comment(&self) -> Cow<'_, str> {
73		"".into()
74	}
75
76	fn cpp_namespace(&self) -> Cow<'_, str> {
77		// force this to be std because on some systems the actual namespace for vector is something like "std::__1"
78		"std".into()
79	}
80
81	fn cpp_name(&self, style: CppNameStyle) -> Cow<'_, str> {
82		"std::vector".cpp_name_from_fullname(style).into()
83	}
84}
85
86impl PartialEq for Vector<'_, '_> {
87	fn eq(&self, other: &Self) -> bool {
88		self.element_type() == other.element_type()
89	}
90}
91
92impl fmt::Debug for Vector<'_, '_> {
93	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94		let mut debug_struct = f.debug_struct(match self {
95			Self::Clang { .. } => "Vector::Clang",
96			Self::Desc(_) => "Vector::Desc",
97		});
98		self
99			.update_debug_struct(&mut debug_struct)
100			.field("element_type", &self.element_type())
101			.finish()
102	}
103}