Cleanup WASM bindings struct generation
PiperOrigin-RevId: 827949686 Change-Id: I4115e397f64963244e3c30a7eaff5be7534aadf2
This commit is contained in:
committed by
Copybara-Service
parent
4b79a58fa0
commit
776fc32eb4
@@ -294,6 +294,11 @@ class StructConstructorCodeBuilderTest(absltest.TestCase):
|
||||
wrapped_structs["mjLROpt"].wrapped_source,
|
||||
"""
|
||||
MjLROpt::MjLROpt(mjLROpt *ptr) : ptr_(ptr) {}
|
||||
MjLROpt::~MjLROpt() {
|
||||
if (owned_ && ptr_) {
|
||||
delete ptr_;
|
||||
}
|
||||
}
|
||||
MjLROpt::MjLROpt() : ptr_(new mjLROpt) {
|
||||
owned_ = true;
|
||||
mj_defaultLROpt(ptr_);
|
||||
@@ -308,14 +313,15 @@ MjLROpt& MjLROpt::operator=(const MjLROpt &other) {
|
||||
*ptr_ = *other.get();
|
||||
return *this;
|
||||
}
|
||||
MjLROpt::~MjLROpt() {
|
||||
if (owned_ && ptr_) {
|
||||
delete ptr_;
|
||||
}
|
||||
}
|
||||
std::unique_ptr<MjLROpt> MjLROpt::copy() {
|
||||
return std::make_unique<MjLROpt>(*this);
|
||||
}
|
||||
mjLROpt* MjLROpt::get() const {
|
||||
return ptr_;
|
||||
}
|
||||
void MjLROpt::set(mjLROpt* ptr) {
|
||||
ptr_ = ptr;
|
||||
}
|
||||
""".strip(),
|
||||
)
|
||||
|
||||
@@ -326,8 +332,11 @@ std::unique_ptr<MjLROpt> MjLROpt::copy() {
|
||||
"""
|
||||
MjsElement::MjsElement(mjsElement *ptr) : ptr_(ptr) {}
|
||||
MjsElement::~MjsElement() {}
|
||||
std::unique_ptr<MjsElement> MjsElement::copy() {
|
||||
return std::make_unique<MjsElement>(*this);
|
||||
mjsElement* MjsElement::get() const {
|
||||
return ptr_;
|
||||
}
|
||||
void MjsElement::set(mjsElement* ptr) {
|
||||
ptr_ = ptr;
|
||||
}
|
||||
""".strip(),
|
||||
)
|
||||
@@ -351,6 +360,12 @@ std::unique_ptr<MjsElement> MjsElement::copy() {
|
||||
"""
|
||||
MjsTexture::MjsTexture(mjsTexture *ptr) : ptr_(ptr), element(ptr_->element) {}
|
||||
MjsTexture::~MjsTexture() {}
|
||||
mjsTexture* MjsTexture::get() const {
|
||||
return ptr_;
|
||||
}
|
||||
void MjsTexture::set(mjsTexture* ptr) {
|
||||
ptr_ = ptr;
|
||||
}
|
||||
""".strip(),
|
||||
)
|
||||
|
||||
@@ -358,6 +373,11 @@ MjsTexture::~MjsTexture() {}
|
||||
self.assertEqual(
|
||||
structs.build_struct_source("mjvLight", []),
|
||||
"""MjvLight::MjvLight(mjvLight *ptr) : ptr_(ptr) {}
|
||||
MjvLight::~MjvLight() {
|
||||
if (owned_ && ptr_) {
|
||||
delete ptr_;
|
||||
}
|
||||
}
|
||||
MjvLight::MjvLight() : ptr_(new mjvLight) {
|
||||
owned_ = true;
|
||||
}
|
||||
@@ -371,14 +391,16 @@ MjvLight& MjvLight::operator=(const MjvLight &other) {
|
||||
*ptr_ = *other.get();
|
||||
return *this;
|
||||
}
|
||||
MjvLight::~MjvLight() {
|
||||
if (owned_ && ptr_) {
|
||||
delete ptr_;
|
||||
}
|
||||
}
|
||||
std::unique_ptr<MjvLight> MjvLight::copy() {
|
||||
return std::make_unique<MjvLight>(*this);
|
||||
}""".strip(),
|
||||
}
|
||||
mjvLight* MjvLight::get() const {
|
||||
return ptr_;
|
||||
}
|
||||
void MjvLight::set(mjvLight* ptr) {
|
||||
ptr_ = ptr;
|
||||
}
|
||||
""".strip(),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -459,28 +459,25 @@ def _build_struct_header_internal(
|
||||
|
||||
builder = code_builder.CodeBuilder()
|
||||
with builder.struct(f"{w}"):
|
||||
if not is_mjs:
|
||||
builder.line(f"{w}();")
|
||||
builder.line(f"{w}(const {w} &);")
|
||||
builder.line(f"{w} &operator=(const {w} &);")
|
||||
|
||||
builder.line(f"explicit {w}({s} *ptr);")
|
||||
builder.line(f"~{w}();")
|
||||
|
||||
if shallow_copy:
|
||||
if not is_mjs:
|
||||
builder.line(f"{w}();")
|
||||
|
||||
if shallow_copy and not is_mjs:
|
||||
builder.line(f"{w}(const {w} &);")
|
||||
builder.line(f"{w} &operator=(const {w} &);")
|
||||
builder.line(f"std::unique_ptr<{w}> copy();")
|
||||
|
||||
builder.line(f"{s}* get() const;")
|
||||
builder.line(f"void set({s}* ptr);")
|
||||
|
||||
for field in wrapped_fields:
|
||||
if field.definition and field not in fields_with_init:
|
||||
for line in field.definition.splitlines():
|
||||
builder.line(line)
|
||||
|
||||
with builder.function(f"{s}* get() const"):
|
||||
builder.line("return ptr_;")
|
||||
|
||||
with builder.function(f"void set({s}* ptr)"):
|
||||
builder.line("ptr_ = ptr;")
|
||||
|
||||
builder.private()
|
||||
builder.line(f"{s}* ptr_;")
|
||||
if not is_mjs:
|
||||
@@ -491,6 +488,7 @@ def _build_struct_header_internal(
|
||||
for field in fields_with_init:
|
||||
if field.definition:
|
||||
builder.line(f"{field.definition}")
|
||||
|
||||
return builder.to_string() + ";"
|
||||
|
||||
|
||||
@@ -600,8 +598,15 @@ def build_struct_source(
|
||||
with builder.function(f"{w}::{w}({s} *ptr) : ptr_(ptr){fields_init}"):
|
||||
pass
|
||||
|
||||
# constructor with default values
|
||||
# destructor
|
||||
with builder.function(f"{w}::~{w}()"):
|
||||
if not is_mjs:
|
||||
with builder.block("if (owned_ && ptr_)"):
|
||||
delete_ptr = _delete_ptr_statement(s)
|
||||
builder.line(delete_ptr)
|
||||
|
||||
if not is_mjs:
|
||||
# default constructor
|
||||
with builder.function(f"{w}::{w}() : ptr_(new {s}){fields_init}"):
|
||||
builder.line("owned_ = true;")
|
||||
default_func = _default_function_statement(s)
|
||||
@@ -626,18 +631,16 @@ def build_struct_source(
|
||||
builder.line(field_with_init.ptr_copy_reset)
|
||||
builder.line("return *this;")
|
||||
|
||||
# destructor
|
||||
with builder.function(f"{w}::~{w}()"):
|
||||
if not is_mjs:
|
||||
with builder.block("if (owned_ && ptr_)"):
|
||||
delete_ptr = _delete_ptr_statement(s)
|
||||
builder.line(delete_ptr)
|
||||
|
||||
# copy function
|
||||
if shallow_copy:
|
||||
# explicit copy function
|
||||
with builder.function(f"std::unique_ptr<{w}> {w}::copy()"):
|
||||
builder.line(f"return std::make_unique<{w}>(*this);")
|
||||
|
||||
with builder.function(f"{s}* {w}::get() const"):
|
||||
builder.line("return ptr_;")
|
||||
|
||||
with builder.function(f"void {w}::set({s}* ptr)"):
|
||||
builder.line("ptr_ = ptr;")
|
||||
|
||||
return builder.to_string()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user