Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make all complex function arguments be passed by const& for non-callback functions #26

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
----

- Scaffolding: Decorate public functions with `__declspec(dllexport)` under Windows and `__attribute__((visibility("default")))` on other platforms
- Core: Make complex function arguments be passed by `const&` for non-callback functions

#### v0.4.0+v0.25.0

Expand Down
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
Loading