rusty_bind_parser/cpp/
swig_generator.rs1use super::templates::TargetLanguageTypeName;
19use crate::binding_types::RustWrapperType;
20use crate::extern_module_translator::ExternModuleTranslator;
21
22pub fn generate_swig_based_on_wrappers(
23    extern_module_translator: &ExternModuleTranslator,
24) -> String {
25    let templates = extern_module_translator
26        .rust_types_wrappers
27        .unordered_iter()
28        .map(|key| match &key.rust_type {
29            RustWrapperType::Vector(inner_type) => format!(
30                "%template(Vec{0}) RustVec<{1}>;\n",
31                inner_type.as_ref().wrapper_name,
32                inner_type.get_name(),
33            ),
34            RustWrapperType::Option(inner_type) => format!(
35                "%template(Optional{0}) Optional<{1}>;\n",
36                inner_type.as_ref().wrapper_name,
37                inner_type.get_name(),
38            ),
39            _ => "".to_owned(),
40        })
41        .collect();
42    let directors = extern_module_translator
43        .user_traits
44        .keys()
45        .map(|key| format!("%feature(\"director\") {};\n", key.wrapper_name))
46        .collect::<String>();
47    generate_swig(directors, templates)
48}
49
50fn generate_swig(directors: String, templates: String) -> String {
51    format!(
52        r#"%module(directors="1") ffi_interface
53// The problem:
54// Swig by default do some copying of the objects that come from the source
55// language. The following SWIG typemaps introduces move semantics in the
56// generated code instead of copying objects.
57%typemap(out, optimal="1") SWIGTYPE %{{
58  $result = new $1_ltype(( $1_ltype &&)$1);
59%}}
60
61// Python uses a different approach.
62#if defined(SWIGPYTHON)
63%typemap(out, optimal="1") SWIGTYPE %{{
64  $result = SWIG_NewPointerObj((new $1_ltype(static_cast< $1_ltype&&  >($1))), $&1_descriptor, SWIG_POINTER_OWN |  0 );
65%}}
66#endif
67
68{directors}
69
70//////////////////////////////////
71// Inlcude the generated C++ API
72//////////////////////////////////
73%{{
74    #include "ffi_cxx.h"
75%}}
76%include "std_string.i"
77%include "ffi_cxx.h"
78
79{templates}
80
81"#
82    )
83}