Skip to content

Commit

Permalink
Improve Vector generation
Browse files Browse the repository at this point in the history
  • Loading branch information
twistedfall committed Nov 1, 2022
1 parent 2842a14 commit 6cb094a
Show file tree
Hide file tree
Showing 14 changed files with 437 additions and 171 deletions.
5 changes: 1 addition & 4 deletions binding-generator/src/func.rs
Expand Up @@ -409,10 +409,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
}
Kind::FieldAccessor(..) => {
if self.type_hint == FunctionTypeHint::FieldSetter {
TypeRef::new(
self.gen_env.resolve_type("void").expect("Can't resolve void type"),
self.gen_env,
)
self.gen_env.resolve_typeref("void")
} else {
let mut out = Field::new(self.entity, self.gen_env).type_ref();
out.set_type_hint(TypeRefTypeHint::PrimitiveRefAsPointer);
Expand Down
7 changes: 7 additions & 0 deletions binding-generator/src/generator_env.rs
Expand Up @@ -330,6 +330,13 @@ impl<'tu> GeneratorEnv<'tu> {
self.type_resolve_cache.get(typ).copied()
}

pub fn resolve_typeref<'ge>(&'ge self, typ: &str) -> TypeRef<'tu, 'ge> {
self
.resolve_type(typ)
.map(|typ| TypeRef::new(typ, self))
.expect(&format!("Can't resolve type: {}", typ))
}

pub fn is_used_in_smart_ptr(&self, entity: Entity) -> bool {
self.used_in_smart_ptr.contains(&entity)
}
Expand Down
4 changes: 2 additions & 2 deletions binding-generator/src/settings.rs
Expand Up @@ -1109,8 +1109,7 @@ pub static NO_SKIP_NAMESPACE_IN_LOCALNAME: Lazy<HashMap<&str, HashMap<&str, &str

pub static PREVENT_VECTOR_TYPEDEF_GENERATION: Lazy<HashSet<&str>> = Lazy::new(|| {
hashset! {
"cv::ppf_match_3d::Pose3DPtr",
"cv::dnn::Net::LayerId",
"cv::dnn::MatShape",
}
});

Expand All @@ -1132,6 +1131,7 @@ pub static GENERATOR_MODULE_TWEAKS: Lazy<HashMap<&str, ModuleTweak>> = Lazy::new
"int",
"unsigned int",
"double",
"size_t",
// return of String
"const char*",
"void*",
Expand Down
18 changes: 6 additions & 12 deletions binding-generator/src/type_ref.rs
Expand Up @@ -762,7 +762,7 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
pub fn rust_module(&self) -> Cow<str> {
match self.kind() {
Kind::Primitive(..) => "core".into(),
Kind::StdVector(vec) => vec.rust_module().into_owned().into(),
Kind::StdVector(vec) => vec.rust_element_module().into_owned().into(),
Kind::Array(inner, ..) | Kind::Pointer(inner) | Kind::Reference(inner) => inner.rust_module().into_owned().into(),
Kind::SmartPtr(ptr) => ptr.rust_module().into_owned().into(),
Kind::Class(cls) => cls.rust_module().into_owned().into(),
Expand Down Expand Up @@ -889,17 +889,11 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
// cv::String is an typedef to std::string and it would lead to duplicate definition error
// That's why we try to resolve both types and check if they are the same, if they are we only generate
// vector<std::string> if not - both.
let vec_cv_string = self
.gen_env
.resolve_type("std::vector<cv::String>")
.expect("Can't resolve std::vector<cv::String>");
let vec_cv_string = self.gen_env.resolve_typeref("std::vector<cv::String>");
if let DependentTypeMode::ForReturn(def_location) = mode {
let vec_std_string = self
.gen_env
.resolve_type("std::vector<std::string>")
.expect("Can't resolve std::vector<std::string>");
let vec_type_ref = if vec_cv_string.get_canonical_type() == vec_std_string.get_canonical_type() {
TypeRef::new(vec_std_string, self.gen_env)
let vec_std_string = self.gen_env.resolve_typeref("std::vector<std::string>");
let vec_type_ref = if vec_cv_string.canonical() == vec_std_string.canonical() {
vec_std_string
} else {
vec.type_ref()
};
Expand All @@ -914,7 +908,7 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
// implement workaround for race when type with std::string gets generated first
// we only want vector<cv::String> because it's more compatible across OpenCV versions
if matches!(str_type, StrType::StdString(_)) {
let tref = TypeRef::new(vec_cv_string, self.gen_env);
let tref = vec_cv_string;
out.push(DependentType::from_vector(
tref.as_vector().expect("Not possible unless something is terribly broken"),
));
Expand Down
12 changes: 6 additions & 6 deletions binding-generator/src/vector.rs
Expand Up @@ -12,7 +12,7 @@ use crate::{
#[derive(Clone)]
pub struct Vector<'tu, 'ge> {
type_ref: Type<'tu>,
gen_env: &'ge GeneratorEnv<'tu>,
pub(crate) gen_env: &'ge GeneratorEnv<'tu>,
}

impl<'tu, 'ge> Vector<'tu, 'ge> {
Expand All @@ -28,6 +28,10 @@ impl<'tu, 'ge> Vector<'tu, 'ge> {
TypeRef::new(self.type_ref, self.gen_env)
}

pub fn rust_element_module(&self) -> Cow<str> {
self.element_type().rust_module().into_owned().into()
}

pub fn element_type(&self) -> TypeRef<'tu, 'ge> {
self
.type_ref()
Expand Down Expand Up @@ -147,11 +151,7 @@ impl Element for Vector<'_, '_> {
}

fn rust_module(&self) -> Cow<str> {
self.element_type().rust_module().into_owned().into()
}

fn rust_namespace(&self) -> Cow<str> {
"core".into()
DefaultElement::rust_module(self)
}

fn rust_name(&self, style: NameStyle) -> Cow<str> {
Expand Down
57 changes: 1 addition & 56 deletions binding-generator/src/writer/rust_native/tpl/vector/cpp.tpl.cpp
@@ -1,60 +1,5 @@
extern "C" {
void cv_{{rust_localalias}}_delete({{cpp_full}}* instance) {
delete instance;
}

{{cpp_extern_return}} cv_{{rust_localalias}}_new() {
return new {{cpp_full}}();
}

size_t cv_{{rust_localalias}}_len(const {{cpp_full}}* instance) {
return instance->size();
}

bool cv_{{rust_localalias}}_is_empty(const {{cpp_full}}* instance) {
return instance->empty();
}

size_t cv_{{rust_localalias}}_capacity(const {{cpp_full}}* instance) {
return instance->capacity();
}

void cv_{{rust_localalias}}_shrink_to_fit({{cpp_full}}* instance) {
instance->shrink_to_fit();
}

void cv_{{rust_localalias}}_reserve({{cpp_full}}* instance, size_t additional) {
instance->reserve(instance->size() + additional);
}

void cv_{{rust_localalias}}_remove({{cpp_full}}* instance, size_t index) {
instance->erase(instance->begin() + index);
}

void cv_{{rust_localalias}}_swap({{cpp_full}}* instance, size_t index1, size_t index2) {
{{swap_func}}((*instance)[index1], (*instance)[index2]);
}

void cv_{{rust_localalias}}_clear({{cpp_full}}* instance) {
instance->clear();
}

void cv_{{rust_localalias}}_push({{cpp_full}}* instance, {{inner_cpp_func_decl}}) {
instance->push_back({{inner_cpp_func_call}});
}

void cv_{{rust_localalias}}_insert({{cpp_full}}* instance, size_t index, {{inner_cpp_func_decl}}) {
instance->insert(instance->begin() + index, {{inner_cpp_func_call}});
}

void cv_{{rust_localalias}}_get(const {{cpp_full}}* instance, size_t index, {{inner_cpp_extern_return}}* ocvrs_return) {
*ocvrs_return = {{prefix}}(*instance)[index]{{suffix}};
}

void cv_{{rust_localalias}}_set({{cpp_full}}* instance, size_t index, {{inner_cpp_func_decl}}) {
(*instance)[index] = {{inner_cpp_func_call}};
}

{{methods}}
{{exports}}
}

Expand Down

This file was deleted.

Expand Up @@ -6,10 +6,6 @@ const {{inner_cpp_extern_return}}* cv_{{rust_localalias}}_data(const {{cpp_full}
return instance->data();
}

{{cpp_extern_return}} cv_{{rust_localalias}}_clone(const {{cpp_full}}* instance) {
return new {{cpp_full}}(*instance);
}

{{cpp_extern_return}} cv_{{rust_localalias}}_from_slice(const {{inner_cpp_full}}* data, size_t len) {
return new {{cpp_full}}(data, data + len);
}
Expand Down
15 changes: 6 additions & 9 deletions binding-generator/src/writer/rust_native/tpl/vector/rust.tpl.rs
@@ -1,13 +1,10 @@
{{alias}}
vector_extern! { {{inner_rust_full}}, {{rust_extern_const}}, {{rust_extern_mut}},
cv_{{rust_localalias}}_new, cv_{{rust_localalias}}_delete,
cv_{{rust_localalias}}_len, cv_{{rust_localalias}}_is_empty,
cv_{{rust_localalias}}_capacity, cv_{{rust_localalias}}_shrink_to_fit,
cv_{{rust_localalias}}_reserve, cv_{{rust_localalias}}_remove,
cv_{{rust_localalias}}_swap, cv_{{rust_localalias}}_clear,
cv_{{rust_localalias}}_get, cv_{{rust_localalias}}_set,
cv_{{rust_localalias}}_push, cv_{{rust_localalias}}_insert,
pub type {{rust_localalias}} = {{rust_full}};

impl {{rust_localalias}} {
pub fn as_raw_{{rust_localalias}}(&self) -> {{rust_extern_const}} { self.as_raw() }
pub fn as_raw_mut_{{rust_localalias}}(&mut self) -> {{rust_extern_mut}} { self.as_raw_mut() }
}
{{extern}}
{{additional_methods}}
{{impls}}

Expand Down
@@ -0,0 +1,11 @@


vector_extern! { {{inner_rust_full}}, {{rust_extern_const}}, {{rust_extern_mut}},
cv_{{rust_localalias}}_new, cv_{{rust_localalias}}_delete,
cv_{{rust_localalias}}_len, cv_{{rust_localalias}}_is_empty,
cv_{{rust_localalias}}_capacity, cv_{{rust_localalias}}_shrink_to_fit,
cv_{{rust_localalias}}_reserve, cv_{{rust_localalias}}_remove,
cv_{{rust_localalias}}_swap, cv_{{rust_localalias}}_clear,
cv_{{rust_localalias}}_get, cv_{{rust_localalias}}_set,
cv_{{rust_localalias}}_push, cv_{{rust_localalias}}_insert,
}

This file was deleted.

0 comments on commit 6cb094a

Please sign in to comment.