- out_java_struct.write(consts.hu_struct_file_prefix)
- out_java_struct.write("public class " + human_ty + " extends CommonBase {\n")
- out_java_struct.write("\tprivate " + human_ty + "(Object _dummy, long ptr) { super(ptr); }\n")
- out_java_struct.write("\tprotected void finalize() throws Throwable {\n")
- out_java_struct.write("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); } super.finalize();\n")
- out_java_struct.write("\t}\n\n")
- out_java_struct.write("\tstatic " + human_ty + " constr_from_ptr(long ptr) {\n")
- out_java_struct.write("\t\tif (bindings." + struct_name + "_result_ok(ptr)) {\n")
- out_java_struct.write("\t\t\treturn new " + human_ty + "_OK(null, ptr);\n")
- out_java_struct.write("\t\t} else {\n")
- out_java_struct.write("\t\t\treturn new " + human_ty + "_Err(null, ptr);\n")
- out_java_struct.write("\t\t}\n")
- out_java_struct.write("\t}\n")
-
- res_map = type_mapping_generator.map_type(res_ty + " res", True, None, False, True)
- err_map = type_mapping_generator.map_type(err_ty + " err", True, None, False, True)
- can_clone = True
- if not res_map.is_native_primitive and (res_map.rust_obj.replace("LDK", "") + "_clone" not in clone_fns):
- can_clone = False
- if not err_map.is_native_primitive and (err_map.rust_obj.replace("LDK", "") + "_clone" not in clone_fns):
- can_clone = False
-
- out_java.write("\tpublic static native boolean " + struct_name + "_result_ok(long arg);\n")
- write_c(consts.c_fn_ty_pfx + "jboolean " + consts.c_fn_name_define_pfx(struct_name + "_result_ok", True) + consts.ptr_c_ty + " arg) {\n")
- write_c("\treturn ((" + struct_name + "*)arg)->result_ok;\n")
- write_c("}\n")
-
- out_java.write("\tpublic static native " + res_map.java_ty + " " + struct_name + "_get_ok(long arg);\n")
- write_c(consts.c_fn_ty_pfx + res_map.c_ty + " " + consts.c_fn_name_define_pfx(struct_name + "_get_ok", True) + consts.ptr_c_ty + " arg) {\n")
- write_c("\t" + struct_name + " *val = (" + struct_name + "*)(arg & ~1);\n")
- write_c("\tCHECK(val->result_ok);\n\t")
- out_java_struct.write("\tpublic static final class " + human_ty + "_OK extends " + human_ty + " {\n")
- if res_map.ret_conv is not None:
- write_c(res_map.ret_conv[0].replace("\n", "\n\t") + "(*val->contents.result)")
- write_c(res_map.ret_conv[1].replace("\n", "\n\t") + "\n\treturn " + res_map.ret_conv_name)
- else:
- write_c("return *val->contents.result")
- write_c(";\n}\n")
-
- if res_map.java_hu_ty != "void":
- out_java_struct.write("\t\tpublic final " + res_map.java_hu_ty + " res;\n")
- out_java_struct.write("\t\tprivate " + human_ty + "_OK(Object _dummy, long ptr) {\n")
- out_java_struct.write("\t\t\tsuper(_dummy, ptr);\n")
- if res_map.java_hu_ty == "void":
- pass
- elif res_map.to_hu_conv is not None:
- out_java_struct.write("\t\t\t" + res_map.java_ty + " res = bindings." + struct_name + "_get_ok(ptr);\n")
- out_java_struct.write("\t\t\t" + res_map.to_hu_conv.replace("\n", "\n\t\t\t"))
- out_java_struct.write("\n\t\t\tthis.res = " + res_map.to_hu_conv_name + ";\n")
- else:
- out_java_struct.write("\t\t\tthis.res = bindings." + struct_name + "_get_ok(ptr);\n")
- out_java_struct.write("\t\t}\n")
- if struct_name.startswith("LDKCResult_None"):
- out_java_struct.write("\t\tpublic " + human_ty + "_OK() {\n\t\t\tthis(null, bindings.C" + human_ty + "_ok());\n")
- else:
- out_java_struct.write("\t\tpublic " + human_ty + "_OK(" + res_map.java_hu_ty + " res) {\n")
- if res_map.from_hu_conv is not None:
- out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_ok(" + res_map.from_hu_conv[0] + "));\n")
- if res_map.from_hu_conv[1] != "":
- out_java_struct.write("\t\t\t" + res_map.from_hu_conv[1].replace("\n", "\n\t\t\t") + ";\n")
- else:
- out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_ok(res));\n")
- out_java_struct.write("\t\t}\n\t}\n\n")
-
- out_java.write("\tpublic static native " + err_map.java_ty + " " + struct_name + "_get_err(long arg);\n")
- write_c(consts.c_fn_ty_pfx + err_map.c_ty + " " + consts.c_fn_name_define_pfx(struct_name + "_get_err", True) + consts.ptr_c_ty + " arg) {\n")
- write_c("\t" + struct_name + " *val = (" + struct_name + "*)(arg & ~1);\n")
- write_c("\tCHECK(!val->result_ok);\n\t")
- out_java_struct.write("\tpublic static final class " + human_ty + "_Err extends " + human_ty + " {\n")
- if err_map.ret_conv is not None:
- write_c(err_map.ret_conv[0].replace("\n", "\n\t") + "(*val->contents.err)")
- write_c(err_map.ret_conv[1].replace("\n", "\n\t") + "\n\treturn " + err_map.ret_conv_name)
- else:
- write_c("return *val->contents.err")
- write_c(";\n}\n")
-
- if err_map.java_hu_ty != "void":
- out_java_struct.write("\t\tpublic final " + err_map.java_hu_ty + " err;\n")
- out_java_struct.write("\t\tprivate " + human_ty + "_Err(Object _dummy, long ptr) {\n")
- out_java_struct.write("\t\t\tsuper(_dummy, ptr);\n")
- if err_map.java_hu_ty == "void":
- pass
- elif err_map.to_hu_conv is not None:
- out_java_struct.write("\t\t\t" + err_map.java_ty + " err = bindings." + struct_name + "_get_err(ptr);\n")
- out_java_struct.write("\t\t\t" + err_map.to_hu_conv.replace("\n", "\n\t\t\t"))
- out_java_struct.write("\n\t\t\tthis.err = " + err_map.to_hu_conv_name + ";\n")
- else:
- out_java_struct.write("\t\t\tthis.err = bindings." + struct_name + "_get_err(ptr);\n")
- out_java_struct.write("\t\t}\n")
-
- if struct_name.endswith("NoneZ"):
- out_java_struct.write("\t\tpublic " + human_ty + "_Err() {\n\t\t\tthis(null, bindings.C" + human_ty + "_err());\n")
- else:
- out_java_struct.write("\t\tpublic " + human_ty + "_Err(" + err_map.java_hu_ty + " err) {\n")
- if err_map.from_hu_conv is not None:
- out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_err(" + err_map.from_hu_conv[0] + "));\n")
- if err_map.from_hu_conv[1] != "":
- out_java_struct.write("\t\t\t" + err_map.from_hu_conv[1].replace("\n", "\n\t\t\t") + ";\n")
- else:
- out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_err(err));\n")
- out_java_struct.write("\t\t}\n\t}\n}\n")
+ out_java_struct.write(java_hu_struct)
+
+ def create_getter(struct_name, field_decl, field_name, accessor, check_sfx):
+ field_ty = java_c_types(field_decl + " " + field_name, None)
+ ptr_fn_defn = field_decl + " *" + struct_name.replace("LDK", "") + "_get_" + field_name + "(" + struct_name + " *NONNULL_PTR owner)"
+ owned_fn_defn = field_decl + " " + struct_name.replace("LDK", "") + "_get_" + field_name + "(" + struct_name + " *NONNULL_PTR owner)"
+
+ holds_ref = False
+ if field_ty.rust_obj is not None and field_ty.rust_obj in opaque_structs:
+ fn_defn = owned_fn_defn
+ write_c("static inline " + fn_defn + "{\n")
+ write_c("\t" + field_ty.rust_obj + " ret = " + accessor[0] + "owner" + accessor[1] + ";\n")
+ write_c("\tret.is_owned = false;\n")
+ write_c("\treturn ret;\n")
+ elif field_ty.rust_obj is not None and field_ty.rust_obj.replace("LDK", "") + "_clone" in clone_fns:
+ fn_defn = owned_fn_defn
+ write_c("static inline " + fn_defn + "{\n")
+ if check_sfx is not None:
+ write_c("CHECK(" + check_sfx[0] + "owner" + check_sfx[1] + ");\n")
+ write_c("\treturn " + field_ty.rust_obj.replace("LDK", "") + "_clone(&" + accessor[0] + "owner" + accessor[1] + ");\n")
+ elif field_ty.arr_len is not None or field_ty.is_native_primitive or field_ty.rust_obj in unitary_enums:
+ fn_defn = owned_fn_defn
+ write_c("static inline " + fn_defn + "{\n")
+ if check_sfx is not None:
+ write_c("CHECK(" + check_sfx[0] + "owner" + check_sfx[1] + ");\n")
+ write_c("\treturn " + accessor[0] + "owner" + accessor[1] + ";\n")
+ holds_ref = True
+ else:
+ fn_defn = ptr_fn_defn
+ write_c("static inline " + fn_defn + "{\n")
+ if check_sfx is not None:
+ write_c("CHECK(" + check_sfx[0] + "owner" + check_sfx[1] + ");\n")
+ write_c("\treturn &" + accessor[0] + "owner" + accessor[1] + ";\n")
+ holds_ref = True
+ write_c("}\n")
+ dummy_line = fn_defn + ";\n"
+ map_fn_with_ref_option(dummy_line, reg_fn_regex.match(dummy_line), None, None, "", holds_ref)