Skip to content

Commit

Permalink
Make all complex function arguments be passed by const& for non-cal…
Browse files Browse the repository at this point in the history
…lback functions

Signed-off-by: Martynas Gurskas <[email protected]>
  • Loading branch information
Lipt0nas committed Feb 12, 2024
1 parent 24751f8 commit 43ca1e5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
36 changes: 30 additions & 6 deletions bindgen/src/bindings/cpp/gen_cpp/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub(crate) use uniffi_bindgen::backend::filters::*;
use uniffi_bindgen::{
backend::CodeType,
interface::{Argument, AsType, FfiType, Literal, Type, Variant},
ComponentInterface,
};

use crate::bindings::cpp::gen_cpp::{
Expand Down Expand Up @@ -177,12 +178,35 @@ pub(crate) fn class_name(nm: &str) -> Result<String> {
Ok(CppCodeOracle.class_name(nm))
}

pub(crate) fn parameter(arg: &Argument) -> Result<String> {
Ok(match arg.as_type() {
Type::Object { .. } | Type::CallbackInterface { .. } => {
format!("const {} &{}", arg.as_codetype().type_label(), arg.name())
}
t => format!("{} {}", type_name(&t)?, arg.name()),
pub(crate) fn by_ref(ci: &ComponentInterface, arg: &Argument) -> bool {
match arg.as_type() {
Type::UInt8
| Type::Int8
| Type::UInt16
| Type::Int16
| Type::UInt32
| Type::Int32
| Type::UInt64
| Type::Int64
| Type::Float32
| Type::Float64
| Type::Boolean
| Type::Optional { .. } => false,
Type::Enum {
module_path: _,
name,
} => match ci.get_enum_definition(&name) {
Some(_enum) => _enum.is_flat(),
None => false,
},
_ => true,
}
}

pub(crate) fn parameter(arg: &Argument, ci: &ComponentInterface) -> Result<String> {
Ok(match by_ref(ci, arg) {
true => format!("const {} &{}", arg.as_codetype().type_label(), arg.name()),
false => format!("{} {}", arg.as_codetype().type_label(), arg.name()),
})
}

Expand Down
5 changes: 4 additions & 1 deletion bindgen/src/bindings/cpp/templates/callback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ struct {{ canonical_type_name }} {
virtual
{%- match method.return_type() %}
{% when Some with (return_type) %} {{ return_type|type_name }} {% else %} void {% endmatch -%}
{{ method.name()|fn_name }}({% call macros::param_list(method) %}) = 0;
{{ method.name()|fn_name }}(
{%- for arg in method.arguments() %}
{{- arg.as_type().borrow()|type_name }} {{ arg.name() }}{% if !loop.last %}, {% endif -%}
{% endfor %}) = 0;
{%- endfor %}

virtual ~{{ canonical_type_name }}() = default;
Expand Down
2 changes: 1 addition & 1 deletion bindgen/src/bindings/cpp/templates/callback_iface_tmpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ int {{ class_name }}::callback_stub(uint64_t handle, uint32_t method, uint8_t *a
auto ret = {% endif -%}
impl->{{ method.name() }}(
{%- for arg in method.arguments() %}
arg{{ loop.index0 }}{%- if !loop.last %}, {% else %}{% endif %}
std::move(arg{{ loop.index0 }}){%- if !loop.last %}, {% else %}{% endif %}
{%- endfor -%}
);
{%- match method.return_type() %}
Expand Down
2 changes: 1 addition & 1 deletion bindgen/src/bindings/cpp/templates/macros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

{% macro param_list(func) %}
{%- for arg in func.arguments() -%}
{{ arg|parameter }}
{{ arg|parameter(ci) }}
{%- if !loop.last -%}, {% endif -%}
{% endfor -%}
{% endmacro %}
Expand Down

0 comments on commit 43ca1e5

Please sign in to comment.