Skip to content

Commit 1db005d

Browse files
committed
[C++ bindings] Add move-assign operator, require rvalue for move
This adds a move-assignment operator (`A& operator=(A&& o)`) to our C++ wrapper classes as well as requiring an rvalue for the move auto-convert operator (`operator CStruct()() &&`). The second makes the C++ wrapper classes much easier to work with by requiring an explicit `std::move` when the bindings will automatically move a C++-wrapper object into a C object.
1 parent b4c2457 commit 1db005d

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

c-bindings-gen/src/blocks.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ pub fn write_cpp_wrapper(cpp_header_file: &mut File, ty: &str, has_destructor: b
1616
writeln!(cpp_header_file, "\tLDK{} self;", ty).unwrap();
1717
writeln!(cpp_header_file, "public:").unwrap();
1818
writeln!(cpp_header_file, "\t{}(const {}&) = delete;", ty, ty).unwrap();
19+
writeln!(cpp_header_file, "\t{}({}&& o) : self(o.self) {{ memset(&o, 0, sizeof({})); }}", ty, ty, ty).unwrap();
20+
writeln!(cpp_header_file, "\t{}(LDK{}&& m_self) : self(m_self) {{ memset(&m_self, 0, sizeof(LDK{})); }}", ty, ty, ty).unwrap();
21+
writeln!(cpp_header_file, "\toperator LDK{}() && {{ LDK{} res = self; memset(&self, 0, sizeof(LDK{})); return res; }}", ty, ty, ty).unwrap();
1922
if has_destructor {
2023
writeln!(cpp_header_file, "\t~{}() {{ {}_free(self); }}", ty, ty).unwrap();
24+
writeln!(cpp_header_file, "\t{}& operator=({}&& o) {{ {}_free(self); self = o.self; memset(&o, 0, sizeof({})); return *this; }}", ty, ty, ty, ty).unwrap();
25+
} else {
26+
writeln!(cpp_header_file, "\t{}& operator=({}&& o) {{ self = o.self; memset(&o, 0, sizeof({})); return *this; }}", ty, ty, ty).unwrap();
2127
}
22-
writeln!(cpp_header_file, "\t{}({}&& o) : self(o.self) {{ memset(&o, 0, sizeof({})); }}", ty, ty, ty).unwrap();
23-
writeln!(cpp_header_file, "\t{}(LDK{}&& m_self) : self(m_self) {{ memset(&m_self, 0, sizeof(LDK{})); }}", ty, ty, ty).unwrap();
24-
writeln!(cpp_header_file, "\toperator LDK{}() {{ LDK{} res = self; memset(&self, 0, sizeof(LDK{})); return res; }}", ty, ty, ty).unwrap();
2528
writeln!(cpp_header_file, "\tLDK{}* operator &() {{ return &self; }}", ty).unwrap();
2629
writeln!(cpp_header_file, "\tLDK{}* operator ->() {{ return &self; }}", ty).unwrap();
2730
writeln!(cpp_header_file, "\tconst LDK{}* operator &() const {{ return &self; }}", ty).unwrap();

0 commit comments

Comments
 (0)