From: Matt Corallo Date: Thu, 31 Dec 2020 20:34:50 +0000 (-0500) Subject: [C++ bindings] Add move-assign operator, require rvalue for move X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=1c7af7fadf2ba7b3819441094494e3b8b6ad2c21;p=rust-lightning [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. --- 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();