From e57c225e0f1822809879da01e79e6157cc0996f7 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 31 Dec 2020 15:34:50 -0500 Subject: [PATCH] [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. --- c-bindings-gen/src/blocks.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/c-bindings-gen/src/blocks.rs b/c-bindings-gen/src/blocks.rs index 6ba829537..107c72b3d 100644 --- a/c-bindings-gen/src/blocks.rs +++ b/c-bindings-gen/src/blocks.rs @@ -16,12 +16,15 @@ pub fn write_cpp_wrapper(cpp_header_file: &mut File, ty: &str, has_destructor: b writeln!(cpp_header_file, "\tLDK{} self;", ty).unwrap(); writeln!(cpp_header_file, "public:").unwrap(); writeln!(cpp_header_file, "\t{}(const {}&) = delete;", ty, ty).unwrap(); + writeln!(cpp_header_file, "\t{}({}&& o) : self(o.self) {{ memset(&o, 0, sizeof({})); }}", ty, ty, ty).unwrap(); + writeln!(cpp_header_file, "\t{}(LDK{}&& m_self) : self(m_self) {{ memset(&m_self, 0, sizeof(LDK{})); }}", ty, ty, ty).unwrap(); + writeln!(cpp_header_file, "\toperator LDK{}() && {{ LDK{} res = self; memset(&self, 0, sizeof(LDK{})); return res; }}", ty, ty, ty).unwrap(); if has_destructor { writeln!(cpp_header_file, "\t~{}() {{ {}_free(self); }}", ty, ty).unwrap(); + writeln!(cpp_header_file, "\t{}& operator=({}&& o) {{ {}_free(self); self = o.self; memset(&o, 0, sizeof({})); return *this; }}", ty, ty, ty, ty).unwrap(); + } else { + writeln!(cpp_header_file, "\t{}& operator=({}&& o) {{ self = o.self; memset(&o, 0, sizeof({})); return *this; }}", ty, ty, ty).unwrap(); } - writeln!(cpp_header_file, "\t{}({}&& o) : self(o.self) {{ memset(&o, 0, sizeof({})); }}", ty, ty, ty).unwrap(); - writeln!(cpp_header_file, "\t{}(LDK{}&& m_self) : self(m_self) {{ memset(&m_self, 0, sizeof(LDK{})); }}", ty, ty, ty).unwrap(); - writeln!(cpp_header_file, "\toperator LDK{}() {{ LDK{} res = self; memset(&self, 0, sizeof(LDK{})); return res; }}", ty, ty, ty).unwrap(); writeln!(cpp_header_file, "\tLDK{}* operator &() {{ return &self; }}", ty).unwrap(); writeln!(cpp_header_file, "\tLDK{}* operator ->() {{ return &self; }}", ty).unwrap(); writeln!(cpp_header_file, "\tconst LDK{}* operator &() const {{ return &self; }}", ty).unwrap(); -- 2.39.5