Skip to content

Commit 1a5121a

Browse files
authored
Merge pull request #29 from NordSecurity/code_cleanup
Clean up templates
2 parents 7eb9bfd + e0ba2dc commit 1a5121a

File tree

8 files changed

+110
-177
lines changed

8 files changed

+110
-177
lines changed

bindgen/src/bindings/cpp/templates/cpp_scaffolding.cpp

Lines changed: 9 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -50,55 +50,7 @@ struct RustCallStatus {
5050

5151
typedef int ForeignCallback(uint64_t handle, uint32_t method, uint8_t *args_data, int32_t args_len, RustBuffer *buf_ptr);
5252

53-
struct RustStreamBuffer: std::basic_streambuf<uint8_t> {
54-
RustStreamBuffer(RustBuffer *buf) {
55-
this->setg(buf->data, buf->data, buf->data + buf->len);
56-
this->setp(buf->data, buf->data + buf->capacity);
57-
}
58-
~RustStreamBuffer() = default;
59-
60-
private:
61-
RustStreamBuffer() = delete;
62-
RustStreamBuffer(const RustStreamBuffer &) = delete;
63-
RustStreamBuffer(RustStreamBuffer &&) = delete;
64-
65-
RustStreamBuffer &operator=(const RustStreamBuffer &) = delete;
66-
RustStreamBuffer &operator=(RustStreamBuffer &&) = delete;
67-
};
68-
69-
struct RustStream: std::basic_iostream<uint8_t> {
70-
RustStream(RustBuffer *buf):
71-
streambuf(RustStreamBuffer(buf)), std::basic_iostream<uint8_t>(&streambuf) { }
72-
73-
template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
74-
RustStream &operator>>(T &val) {
75-
read(reinterpret_cast<uint8_t *>(&val), sizeof(T));
76-
77-
if (std::endian::native != std::endian::big) {
78-
auto bytes = reinterpret_cast<char *>(&val);
79-
80-
std::reverse(bytes, bytes + sizeof(T));
81-
}
82-
83-
return *this;
84-
}
85-
86-
template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
87-
RustStream &operator<<(T val) {
88-
if (std::endian::native != std::endian::big) {
89-
auto bytes = reinterpret_cast<char *>(&val);
90-
91-
std::reverse(bytes, bytes + sizeof(T));
92-
}
93-
94-
write(reinterpret_cast<uint8_t *>(&val), sizeof(T));
95-
96-
return *this;
97-
}
98-
private:
99-
RustStreamBuffer streambuf;
100-
};
101-
53+
{%- include "rust_buf_stream.cpp" %}
10254
{%- include "scaffolding/object_map.cpp" %}
10355

10456
{%- for typ in ci.iter_types() %}
@@ -209,28 +161,9 @@ UNIFFI_EXPORT RustBuffer {{ ci.ffi_rustbuffer_reserve().name() }}(RustBuffer buf
209161

210162
{% for func in ci.function_definitions() %}
211163
{% let ffi_func = func.ffi_func() %}
212-
UNIFFI_EXPORT
213-
{% match ffi_func.return_type() -%}
214-
{% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_func.name() }}(
215-
{%- for arg in ffi_func.arguments() %}
216-
{{- arg.type_().borrow()|ffi_type_name }} {{ arg.name() }}{% if !loop.last || ffi_func.has_rust_call_status_arg() %}, {% endif -%}
217-
{% endfor %}
218-
{%- if ffi_func.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
219-
) {
164+
UNIFFI_EXPORT {%- call macros::fn_definition(ffi_func) %} {
220165
{%- call macros::fn_prologue(ci, func, ffi_func) %}
221-
{%- match func.return_type() %}
222-
{%- when Some with (return_type) %}
223-
auto ret = {{ namespace }}::{{ func.name() }}(
224-
{%- for arg in func.arguments() %}
225-
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
226-
{% endfor %});
227-
return {{ return_type|lower_fn }}(ret);
228-
{% when None %}
229-
{{ namespace }}::{{ func.name() }}(
230-
{%- for arg in func.arguments() %}
231-
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
232-
{%- endfor %});
233-
{%- endmatch %}
166+
{%- call macros::invoke_native_fn(func, namespace) %}
234167
{%- call macros::fn_epilogue(ci, func, ffi_func) %}
235168
}
236169
{% endfor %}
@@ -248,14 +181,7 @@ UNIFFI_EXPORT void {{ ffi_func.name() }}(ForeignCallback callback_stub, RustCall
248181

249182
{% for ctor in obj.constructors() %}
250183
{% let ffi_ctor = ctor.ffi_func() %}
251-
UNIFFI_EXPORT
252-
{% match ffi_ctor.return_type() -%}
253-
{% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_ctor.name() }}(
254-
{%- for arg in ffi_ctor.arguments() %}
255-
{{- arg.type_().borrow()|ffi_type_name }} {{ arg.name() }}{% if !loop.last || ffi_ctor.has_rust_call_status_arg() %}, {% endif -%}
256-
{% endfor %}
257-
{%- if ffi_ctor.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
258-
) {
184+
UNIFFI_EXPORT {%- call macros::fn_definition(ffi_ctor) %} {
259185
{%- if ffi_ctor.has_rust_call_status_arg() %}
260186
out_status->code = UNIFFI_CALL_STATUS_OK;
261187
{% endif -%}
@@ -269,14 +195,7 @@ UNIFFI_EXPORT
269195
{% endfor %}
270196

271197
{% let ffi_dtor = obj.ffi_object_free() %}
272-
UNIFFI_EXPORT
273-
{% match ffi_dtor.return_type() -%}
274-
{% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_dtor.name() }}(
275-
{%- for arg in ffi_dtor.arguments() %}
276-
{{- arg.type_().borrow()|ffi_type_name }} {{ arg.name() }}{% if !loop.last || ffi_dtor.has_rust_call_status_arg() %}, {% endif -%}
277-
{% endfor %}
278-
{%- if ffi_dtor.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
279-
) {
198+
UNIFFI_EXPORT {%- call macros::fn_definition(ffi_dtor) %} {
280199
{%- if ffi_dtor.has_rust_call_status_arg() %}
281200
out_status->code = UNIFFI_CALL_STATUS_OK;
282201
{% endif -%}
@@ -286,48 +205,17 @@ UNIFFI_EXPORT
286205

287206
{% for func in obj.methods() %}
288207
{% let ffi_func = func.ffi_func() %}
289-
UNIFFI_EXPORT
290-
{% match ffi_func.return_type() -%}
291-
{% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_func.name() }}(
292-
{%- for arg in ffi_func.arguments() %}
293-
{{- arg.type_().borrow()|ffi_type_name }} {{ arg.name() }}{% if !loop.last || ffi_func.has_rust_call_status_arg() %}, {% endif -%}
294-
{% endfor %}
295-
{%- if ffi_func.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
296-
) {
297-
{%- if ffi_func.has_rust_call_status_arg() %}
298-
out_status->code = UNIFFI_CALL_STATUS_OK;
299-
{% endif -%}
300-
301-
auto obj = {{ obj.name() }}_map.at((uint64_t)ptr);
302-
208+
UNIFFI_EXPORT {%- call macros::fn_definition(ffi_func) %} {
303209
{%- call macros::fn_prologue(ci, func, ffi_func) %}
304-
{% match func.return_type() %}
305-
{% when Some with (return_type) %}
306-
auto ret = obj->{{ func.name() }}({% if func.takes_self_by_arc() %}obj{% if !func.arguments().is_empty() %},{% endif %}{% endif %}
307-
{%- for arg in func.arguments() %}
308-
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
309-
{% endfor %});
310-
return {{ return_type|lower_fn }}(ret);
311-
{% when None %}
312-
obj->{{ func.name() }}(
313-
{%- for arg in func.arguments() %}
314-
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
315-
{% endfor %});
316-
{% endmatch %}
210+
auto obj = {{ obj.name() }}_map.at((uint64_t)ptr);
211+
{%- call macros::invoke_native_fn_obj(func) %}
317212
{%- call macros::fn_epilogue(ci, func, ffi_func) %}
318213
}
319214
{% endfor %}
320215
{% endfor %}
321216

322217
{% for func in ci.iter_futures_ffi_function_definitons() %}
323-
UNIFFI_EXPORT
324-
{% match func.return_type() -%}
325-
{% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ func.name() }}(
326-
{%- for arg in func.arguments() %}
327-
{{- arg.type_().borrow()|ffi_type_name }} {{ arg.name() }}{% if !loop.last || func.has_rust_call_status_arg() %}, {% endif -%}
328-
{% endfor %}
329-
{%- if func.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
330-
) {
218+
UNIFFI_EXPORT {%- call macros::fn_definition(func) %} {
331219
{%- match func.return_type() %}{% when Some with (return_type) %}
332220
{% match return_type %}
333221
{% when FfiType::RustArcPtr(_) %}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
struct RustStreamBuffer: std::basic_streambuf<uint8_t> {
2+
RustStreamBuffer(RustBuffer *buf) {
3+
this->setg(buf->data, buf->data, buf->data + buf->len);
4+
this->setp(buf->data, buf->data + buf->capacity);
5+
}
6+
~RustStreamBuffer() = default;
7+
8+
private:
9+
RustStreamBuffer() = delete;
10+
RustStreamBuffer(const RustStreamBuffer &) = delete;
11+
RustStreamBuffer(RustStreamBuffer &&) = delete;
12+
13+
RustStreamBuffer &operator=(const RustStreamBuffer &) = delete;
14+
RustStreamBuffer &operator=(RustStreamBuffer &&) = delete;
15+
};
16+
17+
struct RustStream: std::basic_iostream<uint8_t> {
18+
RustStream(RustBuffer *buf):
19+
streambuf(RustStreamBuffer(buf)), std::basic_iostream<uint8_t>(&streambuf) { }
20+
21+
template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
22+
RustStream &operator>>(T &val) {
23+
read(reinterpret_cast<uint8_t *>(&val), sizeof(T));
24+
25+
if (std::endian::native != std::endian::big) {
26+
auto bytes = reinterpret_cast<char *>(&val);
27+
28+
std::reverse(bytes, bytes + sizeof(T));
29+
}
30+
31+
return *this;
32+
}
33+
34+
template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
35+
RustStream &operator<<(T val) {
36+
if (std::endian::native != std::endian::big) {
37+
auto bytes = reinterpret_cast<char *>(&val);
38+
39+
std::reverse(bytes, bytes + sizeof(T));
40+
}
41+
42+
write(reinterpret_cast<uint8_t *>(&val), sizeof(T));
43+
44+
return *this;
45+
}
46+
private:
47+
RustStreamBuffer streambuf;
48+
};
49+

bindgen/src/bindings/cpp/templates/scaffolding/macros.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,46 @@
3030
{% endmatch %}
3131
{%- endif %}
3232
{% endmacro %}
33+
34+
{% macro invoke_native_fn(scaffolding_fn, namespace) %}
35+
{% match func.return_type() %}
36+
{% when Some with (return_type) %}
37+
auto ret = {{ namespace }}::{{ scaffolding_fn.name() }}(
38+
{%- for arg in scaffolding_fn.arguments() %}
39+
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
40+
{% endfor %});
41+
return {{ return_type|lower_fn }}(ret);
42+
{% when None %}
43+
{{ namespace }}::{{ scaffolding_fn.name() }}(
44+
{%- for arg in scaffolding_fn.arguments() %}
45+
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
46+
{% endfor %});
47+
{% endmatch %}
48+
{% endmacro %}
49+
50+
{% macro invoke_native_fn_obj(scaffolding_fn) %}
51+
{% match func.return_type() %}
52+
{% when Some with (return_type) %}
53+
auto ret = obj->{{ scaffolding_fn.name() }}(
54+
{% if scaffolding_fn.takes_self_by_arc() %}obj{% if !scaffolding_fn.arguments().is_empty() %},{% endif %}{% endif %}
55+
{%- for arg in scaffolding_fn.arguments() %}
56+
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
57+
{% endfor %});
58+
return {{ return_type|lower_fn }}(ret);
59+
{% when None %}
60+
obj->{{ scaffolding_fn.name() }}(
61+
{% if scaffolding_fn.takes_self_by_arc() %}obj{% if !scaffolding_fn.arguments().is_empty() %},{% endif %}{% endif %}
62+
{%- for arg in scaffolding_fn.arguments() %}
63+
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
64+
{% endfor %});
65+
{% endmatch %}
66+
{% endmacro %}
67+
68+
{% macro fn_definition(ffi_func) %}
69+
{% match ffi_func.return_type() -%}
70+
{% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_func.name() }}(
71+
{%- for arg in ffi_func.arguments() %}
72+
{{- arg.type_().borrow()|ffi_type_name }} {{ arg.name() }}{% if !loop.last || ffi_func.has_rust_call_status_arg() %}, {% endif -%}
73+
{% endfor %}
74+
{%- if ffi_func.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%})
75+
{% endmacro %}

bindgen/src/bindings/cpp/templates/wrapper.hpp

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -79,54 +79,7 @@ typedef {{ type_name }} {{ name }};
7979
{%- endfor %}
8080

8181
namespace uniffi {
82-
struct RustStreamBuffer: std::basic_streambuf<uint8_t> {
83-
RustStreamBuffer(RustBuffer *buf) {
84-
this->setg(buf->data, buf->data, buf->data + buf->len);
85-
this->setp(buf->data, buf->data + buf->capacity);
86-
}
87-
~RustStreamBuffer() = default;
88-
89-
private:
90-
RustStreamBuffer() = delete;
91-
RustStreamBuffer(const RustStreamBuffer &) = delete;
92-
RustStreamBuffer(RustStreamBuffer &&) = delete;
93-
94-
RustStreamBuffer &operator=(const RustStreamBuffer &) = delete;
95-
RustStreamBuffer &operator=(RustStreamBuffer &&) = delete;
96-
};
97-
98-
struct RustStream: std::basic_iostream<uint8_t> {
99-
RustStream(RustBuffer *buf):
100-
streambuf(RustStreamBuffer(buf)), std::basic_iostream<uint8_t>(&streambuf) { }
101-
102-
template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
103-
RustStream &operator>>(T &val) {
104-
read(reinterpret_cast<uint8_t *>(&val), sizeof(T));
105-
106-
if (std::endian::native != std::endian::big) {
107-
auto bytes = reinterpret_cast<char *>(&val);
108-
109-
std::reverse(bytes, bytes + sizeof(T));
110-
}
111-
112-
return *this;
113-
}
114-
115-
template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
116-
RustStream &operator<<(T val) {
117-
if (std::endian::native != std::endian::big) {
118-
auto bytes = reinterpret_cast<char *>(&val);
119-
120-
std::reverse(bytes, bytes + sizeof(T));
121-
}
122-
123-
write(reinterpret_cast<uint8_t *>(&val), sizeof(T));
124-
125-
return *this;
126-
}
127-
private:
128-
RustStreamBuffer streambuf;
129-
};
82+
{%- include "rust_buf_stream.cpp" %}
13083

13184
RustBuffer rustbuffer_alloc(int32_t);
13285
RustBuffer rustbuffer_from_bytes(const ForeignBytes &);

cpp-tests/scaffolding_tests/coverall/lib_coverall.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ std::shared_ptr<coverall::Coveralls> coverall::Coveralls::get_other() {
117117
return this->other;
118118
}
119119

120-
void coverall::Coveralls::take_other_fallible() {
120+
void coverall::Coveralls::take_other_fallible(const std::shared_ptr<Coveralls> &self) {
121121
throw coverall::coverall_error::TooManyHoles();
122122
}
123123

124-
void coverall::Coveralls::take_other_panic(std::string message) {
124+
void coverall::Coveralls::take_other_panic(const std::shared_ptr<Coveralls> &self, std::string message) {
125125
throw std::runtime_error(message);
126126
}
127127

cpp-tests/scaffolding_tests/coverall/lib_coverall.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ namespace {
9191

9292
void take_other(const std::shared_ptr<Coveralls> &other);
9393
std::shared_ptr<Coveralls> get_other();
94-
void take_other_fallible();
95-
void take_other_panic(std::string message);
94+
void take_other_fallible(const std::shared_ptr<Coveralls> &self);
95+
void take_other_panic(const std::shared_ptr<Coveralls> &self, std::string message);
9696

9797
std::shared_ptr<Coveralls> clone_me();
9898

cpp-tests/scaffolding_tests/todolist/lib_todolist.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ void todolist::TodoList::clear_item(const std::string &todo) {
9595
this->items.erase(it);
9696
}
9797

98-
void todolist::TodoList::make_default() {
99-
todolist::set_default_list(this->shared_from_this());
98+
void todolist::TodoList::make_default(const std::shared_ptr<TodoList> &self) {
99+
todolist::set_default_list(self);
100100
}
101101

102102
#include <todolist_cpp_scaffolding.cpp>

cpp-tests/scaffolding_tests/todolist/lib_todolist.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace {
4848
std::string text;
4949
};
5050

51-
class TodoList: public std::enable_shared_from_this<TodoList>{
51+
class TodoList {
5252
public:
5353
TodoList() = default;
5454

@@ -62,7 +62,7 @@ namespace {
6262
std::string get_last();
6363
std::string get_first();
6464
void clear_item(const std::string &item);
65-
void make_default();
65+
void make_default(const std::shared_ptr<TodoList> &self);
6666
private:
6767
std::vector<std::string> items;
6868
std::mutex items_mutex;

0 commit comments

Comments
 (0)