From dfe2b7537893f1a3f2c2135a6109e07fbd9054a7 Mon Sep 17 00:00:00 2001 From: Alessio Quaglino Date: Wed, 27 Mar 2024 07:36:43 -0700 Subject: [PATCH] Convert mjElement to struct rather than opaque pointer. By making mjElement the parent of mjCBase, mjCModel, and mjCDef, this CL enables safer casting using static_cast instead of reinterpret_cast and C-style casting. PiperOrigin-RevId: 619526291 Change-Id: Ifa2b4bd3ecd95bade61f41a7005c62212f456c00 --- src/user/user_api.cc | 144 +++++++++++++++++------------------ src/user/user_api.h | 70 +++++++++-------- src/user/user_composite.cc | 10 +-- src/user/user_flexcomp.cc | 6 +- src/user/user_mesh.cc | 17 +++-- src/user/user_model.cc | 11 +-- src/user/user_model.h | 2 +- src/user/user_objects.cc | 94 ++++++++++++++--------- src/user/user_objects.h | 4 +- src/xml/xml_native_writer.cc | 4 +- 10 files changed, 199 insertions(+), 163 deletions(-) diff --git a/src/user/user_api.cc b/src/user/user_api.cc index a0ca98e6..6220d98a 100644 --- a/src/user/user_api.cc +++ b/src/user/user_api.cc @@ -58,7 +58,7 @@ mjSpec* mjs_createSpec() { // copy model mjSpec* mjs_copySpec(const mjSpec* s) { - mjCModel* modelC = new mjCModel(*reinterpret_cast(s->element)); + mjCModel* modelC = new mjCModel(*static_cast(s->element)); return &modelC->spec; } @@ -66,7 +66,7 @@ mjSpec* mjs_copySpec(const mjSpec* s) { // copy back model void mjs_copyBack(mjSpec* s, const mjModel* m) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); modelC->CopyBack(m); } @@ -74,7 +74,7 @@ void mjs_copyBack(mjSpec* s, const mjModel* m) { // compile model mjModel* mjs_compile(mjSpec* s, const mjVFS* vfs) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); return modelC->Compile(vfs); } @@ -83,8 +83,8 @@ mjModel* mjs_compile(mjSpec* s, const mjVFS* vfs) { // attach body to a frame of the parent int mjs_attachBody(mjsFrame* parent, const mjsBody* child, const char* prefix, const char* suffix) { - mjCFrame* frame_parent = reinterpret_cast(parent->element); - mjCBody* child_body = reinterpret_cast(child->element); + mjCFrame* frame_parent = static_cast(parent->element); + mjCBody* child_body = static_cast(child->element); *frame_parent += std::string(prefix) + *child_body + std::string(suffix); return 0; } @@ -93,7 +93,7 @@ int mjs_attachBody(mjsFrame* parent, const mjsBody* child, // get error message from model const char* mjs_getError(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); return modelC->GetError().message; } @@ -101,7 +101,7 @@ const char* mjs_getError(mjSpec* s) { // check if model has warnings int mjs_isWarning(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); return modelC->GetError().warning; } @@ -109,7 +109,7 @@ int mjs_isWarning(mjSpec* s) { // delete model void mjs_deleteSpec(mjSpec* s) { - mjCModel* model = reinterpret_cast(s->element); + mjCModel* model = static_cast(s->element); delete model; } @@ -117,8 +117,8 @@ void mjs_deleteSpec(mjSpec* s) { // add child body to body, return child spec mjsBody* mjs_addBody(mjsBody* bodyspec, mjsDefault* defspec) { - mjCDef* def = defspec ? reinterpret_cast(defspec->element) : 0; - mjCBody* body = reinterpret_cast(bodyspec->element)->AddBody(def); + mjCDef* def = defspec ? static_cast(defspec->element) : 0; + mjCBody* body = static_cast(bodyspec->element)->AddBody(def); return &body->spec; } @@ -126,8 +126,8 @@ mjsBody* mjs_addBody(mjsBody* bodyspec, mjsDefault* defspec) { // add site to body, return site spec mjsSite* mjs_addSite(mjsBody* bodyspec, mjsDefault* defspec) { - mjCDef* def = defspec ? reinterpret_cast(defspec->element) : 0; - mjCBody* body = reinterpret_cast(bodyspec->element); + mjCDef* def = defspec ? static_cast(defspec->element) : 0; + mjCBody* body = static_cast(bodyspec->element); mjCSite* site = body->AddSite(def); return &site->spec; } @@ -136,8 +136,8 @@ mjsSite* mjs_addSite(mjsBody* bodyspec, mjsDefault* defspec) { // add joint to body mjsJoint* mjs_addJoint(mjsBody* bodyspec, mjsDefault* defspec) { - mjCDef* def = defspec ? reinterpret_cast(defspec->element) : 0; - mjCBody* body = reinterpret_cast(bodyspec->element); + mjCDef* def = defspec ? static_cast(defspec->element) : 0; + mjCBody* body = static_cast(bodyspec->element); mjCJoint* joint = body->AddJoint(def); return &joint->spec; } @@ -146,7 +146,7 @@ mjsJoint* mjs_addJoint(mjsBody* bodyspec, mjsDefault* defspec) { // add free joint to body mjsJoint* mjs_addFreeJoint(mjsBody* bodyspec) { - mjCBody* body = reinterpret_cast(bodyspec->element); + mjCBody* body = static_cast(bodyspec->element); mjCJoint* joint = body->AddFreeJoint(); return &joint->spec; } @@ -155,8 +155,8 @@ mjsJoint* mjs_addFreeJoint(mjsBody* bodyspec) { // add geom to body mjsGeom* mjs_addGeom(mjsBody* bodyspec, mjsDefault* defspec) { - mjCDef* def = defspec ? reinterpret_cast(defspec->element) : 0; - mjCBody* body = reinterpret_cast(bodyspec->element); + mjCDef* def = defspec ? static_cast(defspec->element) : 0; + mjCBody* body = static_cast(bodyspec->element); mjCGeom* geom = body->AddGeom(def); return &geom->spec; } @@ -165,8 +165,8 @@ mjsGeom* mjs_addGeom(mjsBody* bodyspec, mjsDefault* defspec) { // add camera to body mjsCamera* mjs_addCamera(mjsBody* bodyspec, mjsDefault* defspec) { - mjCDef* def = defspec ? reinterpret_cast(defspec->element) : 0; - mjCBody* body = reinterpret_cast(bodyspec->element); + mjCDef* def = defspec ? static_cast(defspec->element) : 0; + mjCBody* body = static_cast(bodyspec->element); mjCCamera* camera = body->AddCamera(def); return &camera->spec; } @@ -175,8 +175,8 @@ mjsCamera* mjs_addCamera(mjsBody* bodyspec, mjsDefault* defspec) { // add light to body mjsLight* mjs_addLight(mjsBody* bodyspec, mjsDefault* defspec) { - mjCDef* def = defspec ? reinterpret_cast(defspec->element) : 0; - mjCBody* body = reinterpret_cast(bodyspec->element); + mjCDef* def = defspec ? static_cast(defspec->element) : 0; + mjCBody* body = static_cast(bodyspec->element); mjCLight* light = body->AddLight(def); return &light->spec; } @@ -185,7 +185,7 @@ mjsLight* mjs_addLight(mjsBody* bodyspec, mjsDefault* defspec) { // add flex to model mjsFlex* mjs_addFlex(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCFlex* flex = modelC->AddFlex(); return &flex->spec; } @@ -196,9 +196,9 @@ mjsFlex* mjs_addFlex(mjSpec* s) { mjsFrame* mjs_addFrame(mjsBody* bodyspec, mjsFrame* parentframe) { mjCFrame* parentframeC = 0; if (parentframe) { - parentframeC = reinterpret_cast(parentframe->element); + parentframeC = static_cast(parentframe->element); } - mjCBody* body = reinterpret_cast(bodyspec->element); + mjCBody* body = static_cast(bodyspec->element); mjCFrame* frameC = body->AddFrame(parentframeC); frameC->SetParent(body); return &frameC->spec; @@ -208,8 +208,8 @@ mjsFrame* mjs_addFrame(mjsBody* bodyspec, mjsFrame* parentframe) { // add mesh to model mjsMesh* mjs_addMesh(mjSpec* s, mjsDefault* defspec) { - mjCDef* def = defspec ? reinterpret_cast(defspec->element) : 0; - mjCModel* modelC = reinterpret_cast(s->element); + mjCDef* def = defspec ? static_cast(defspec->element) : 0; + mjCModel* modelC = static_cast(s->element); mjCMesh* mesh = modelC->AddMesh(def); return &mesh->spec; } @@ -218,7 +218,7 @@ mjsMesh* mjs_addMesh(mjSpec* s, mjsDefault* defspec) { // add height field to model mjsHField* mjs_addHField(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCHField* heightField = modelC->AddHField(); return &heightField->spec; } @@ -227,7 +227,7 @@ mjsHField* mjs_addHField(mjSpec* s) { // add skin to model mjsSkin* mjs_addSkin(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCSkin* skin = modelC->AddSkin(); return &skin->spec; } @@ -236,7 +236,7 @@ mjsSkin* mjs_addSkin(mjSpec* s) { // add texture to model mjsTexture* mjs_addTexture(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCTexture* texture = modelC->AddTexture(); return &texture->spec; } @@ -245,8 +245,8 @@ mjsTexture* mjs_addTexture(mjSpec* s) { // add material to model mjsMaterial* mjs_addMaterial(mjSpec* s, mjsDefault* defspec) { - mjCModel* modelC = reinterpret_cast(s->element); - mjCDef* def = defspec ? reinterpret_cast(defspec->element) : 0; + mjCModel* modelC = static_cast(s->element); + mjCDef* def = defspec ? static_cast(defspec->element) : 0; mjCMaterial* material = modelC->AddMaterial(def); return &material->spec; } @@ -255,8 +255,8 @@ mjsMaterial* mjs_addMaterial(mjSpec* s, mjsDefault* defspec) { // add pair to model mjsPair* mjs_addPair(mjSpec* s, mjsDefault* defspec) { - mjCModel* modelC = reinterpret_cast(s->element); - mjCDef* def = defspec ? reinterpret_cast(defspec->element) : 0; + mjCModel* modelC = static_cast(s->element); + mjCDef* def = defspec ? static_cast(defspec->element) : 0; mjCPair* pair = modelC->AddPair(def); return &pair->spec; } @@ -265,7 +265,7 @@ mjsPair* mjs_addPair(mjSpec* s, mjsDefault* defspec) { // add pair exclusion to model mjsExclude* mjs_addExclude(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCBodyPair* bodypair = modelC->AddExclude(); return &bodypair->spec; } @@ -274,8 +274,8 @@ mjsExclude* mjs_addExclude(mjSpec* s) { // add equality to model mjsEquality* mjs_addEquality(mjSpec* s, mjsDefault* defspec) { - mjCModel* modelC = reinterpret_cast(s->element); - mjCDef* def = defspec ? reinterpret_cast(defspec->element) : 0; + mjCModel* modelC = static_cast(s->element); + mjCDef* def = defspec ? static_cast(defspec->element) : 0; mjCEquality* equality = modelC->AddEquality(def); return &equality->spec; } @@ -284,8 +284,8 @@ mjsEquality* mjs_addEquality(mjSpec* s, mjsDefault* defspec) { // add tendon to model mjsTendon* mjs_addTendon(mjSpec* s, mjsDefault* defspec) { - mjCModel* modelC = reinterpret_cast(s->element); - mjCDef* def = defspec ? reinterpret_cast(defspec->element) : 0; + mjCModel* modelC = static_cast(s->element); + mjCDef* def = defspec ? static_cast(defspec->element) : 0; mjCTendon* tendon = modelC->AddTendon(def); return &tendon->spec; } @@ -294,7 +294,7 @@ mjsTendon* mjs_addTendon(mjSpec* s, mjsDefault* defspec) { // wrap site using tendon MJAPI mjsWrap* mjs_wrapSite(mjsTendon* tendonspec, const char* name) { - mjCTendon* tendon = reinterpret_cast(tendonspec->element); + mjCTendon* tendon = static_cast(tendonspec->element); tendon->WrapSite(name); return &tendon->path.back()->spec; } @@ -303,7 +303,7 @@ MJAPI mjsWrap* mjs_wrapSite(mjsTendon* tendonspec, const char* name) { // wrap geom using tendon mjsWrap* mjs_wrapGeom(mjsTendon* tendonspec, const char* name, const char* sidesite) { - mjCTendon* tendon = reinterpret_cast(tendonspec->element); + mjCTendon* tendon = static_cast(tendonspec->element); tendon->WrapGeom(name, sidesite); return &tendon->path.back()->spec; } @@ -312,7 +312,7 @@ mjsWrap* mjs_wrapGeom(mjsTendon* tendonspec, const char* name, const char* sides // wrap joint using tendon mjsWrap* mjs_wrapJoint(mjsTendon* tendonspec, const char* name, double coef) { - mjCTendon* tendon = reinterpret_cast(tendonspec->element); + mjCTendon* tendon = static_cast(tendonspec->element); tendon->WrapJoint(name, coef); return &tendon->path.back()->spec; } @@ -321,7 +321,7 @@ mjsWrap* mjs_wrapJoint(mjsTendon* tendonspec, const char* name, double coef) { // wrap pulley using tendon mjsWrap* mjs_wrapPulley(mjsTendon* tendonspec, double divisor) { - mjCTendon* tendon = reinterpret_cast(tendonspec->element); + mjCTendon* tendon = static_cast(tendonspec->element); tendon->WrapPulley(divisor); return &tendon->path.back()->spec; } @@ -330,8 +330,8 @@ mjsWrap* mjs_wrapPulley(mjsTendon* tendonspec, double divisor) { // add actuator to model mjsActuator* mjs_addActuator(mjSpec* s, mjsDefault* defspec) { - mjCModel* modelC = reinterpret_cast(s->element); - mjCDef* def = defspec ? reinterpret_cast(defspec->element) : 0; + mjCModel* modelC = static_cast(s->element); + mjCDef* def = defspec ? static_cast(defspec->element) : 0; mjCActuator* actuator = modelC->AddActuator(def); return &actuator->spec; } @@ -340,7 +340,7 @@ mjsActuator* mjs_addActuator(mjSpec* s, mjsDefault* defspec) { // add sensor to model mjsSensor* mjs_addSensor(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCSensor* sensor = modelC->AddSensor(); return &sensor->spec; } @@ -349,7 +349,7 @@ mjsSensor* mjs_addSensor(mjSpec* s) { // add numeric to model mjsNumeric* mjs_addNumeric(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCNumeric* numeric = modelC->AddNumeric(); return &numeric->spec; } @@ -358,7 +358,7 @@ mjsNumeric* mjs_addNumeric(mjSpec* s) { // add text to model mjsText* mjs_addText(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCText* text = modelC->AddText(); return &text->spec; } @@ -367,7 +367,7 @@ mjsText* mjs_addText(mjSpec* s) { // add tuple to model mjsTuple* mjs_addTuple(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCTuple* tuple = modelC->AddTuple(); return &tuple->spec; } @@ -376,7 +376,7 @@ mjsTuple* mjs_addTuple(mjSpec* s) { // add keyframe to model mjsKey* mjs_addKey(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCKey* key = modelC->AddKey(); return &key->spec; } @@ -385,9 +385,9 @@ mjsKey* mjs_addKey(mjSpec* s) { // add plugin to model mjsPlugin* mjs_addPlugin(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCPlugin* plugin = modelC->AddPlugin(); - plugin->spec.instance = (mjElement)plugin; + plugin->spec.instance = static_cast(plugin); return &plugin->spec; } @@ -395,7 +395,7 @@ mjsPlugin* mjs_addPlugin(mjSpec* s) { // add default to model mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, int parentid, int* id) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); *id = (int)modelC->defaults.size(); mjCDef* def = modelC->AddDef(classname, parentid); if (def) { @@ -409,21 +409,21 @@ mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, int parentid, int* // get objects mjSpec* mjs_getSpec(mjsBody* body) { - return &(reinterpret_cast(body->element)->model->spec); + return &(static_cast(body->element)->model->spec); } // get default -mjsDefault* mjs_getDefault(mjElement element) { - return &(reinterpret_cast(element)->def->spec); +mjsDefault* mjs_getDefault(mjElement* element) { + return &(static_cast(element)->def->spec); } // Find default with given name in model. mjsDefault* mjs_findDefault(mjSpec* s, const char* classname) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCDef* cdef = modelC->FindDef(classname); if (!cdef) { return nullptr; @@ -435,7 +435,7 @@ mjsDefault* mjs_findDefault(mjSpec* s, const char* classname) { // get default[0] from model mjsDefault* mjs_getSpecDefault(mjSpec* s) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); mjCDef* def = modelC->defaults[0]; if (!def) { return nullptr; @@ -447,7 +447,7 @@ mjsDefault* mjs_getSpecDefault(mjSpec* s) { // find body in model by name mjsBody* mjs_findBody(mjSpec* s, const char* name) { - mjCModel* model = reinterpret_cast(s->element); + mjCModel* model = static_cast(s->element); mjCBase* body = 0; if (model->IsCompiled()) { body = model->FindObject(mjOBJ_BODY, std::string(name)); // fast lookup @@ -461,7 +461,7 @@ mjsBody* mjs_findBody(mjSpec* s, const char* name) { // find child of a body by name mjsBody* mjs_findChild(mjsBody* bodyspec, const char* name) { - mjCBody* body = reinterpret_cast(bodyspec->element); + mjCBody* body = static_cast(bodyspec->element); mjCBase* child = body->FindObject(mjOBJ_BODY, std::string(name)); return child ? &(static_cast(child)->spec) : nullptr; } @@ -470,7 +470,7 @@ mjsBody* mjs_findChild(mjsBody* bodyspec, const char* name) { // find mesh by name mjsMesh* mjs_findMesh(mjSpec* s, const char* name) { - mjCModel* model = reinterpret_cast(s->element); + mjCModel* model = static_cast(s->element); mjCMesh* mesh = (mjCMesh*)model->FindObject(mjOBJ_MESH, std::string(name)); return mesh ? &(static_cast(mesh)->spec) : nullptr; } @@ -479,7 +479,7 @@ mjsMesh* mjs_findMesh(mjSpec* s, const char* name) { // find frame by name mjsFrame* mjs_findFrame(mjSpec* s, const char* name) { - mjCModel* model = reinterpret_cast(s->element); + mjCModel* model = static_cast(s->element); mjCFrame* frame = (mjCFrame*)model->FindFrame(model->GetWorld(), std::string(name)); return frame ? &(static_cast(frame)->spec) : nullptr; } @@ -487,12 +487,12 @@ mjsFrame* mjs_findFrame(mjSpec* s, const char* name) { // set frame -void mjs_setFrame(mjElement dest, mjsFrame* frame) { +void mjs_setFrame(mjElement* dest, mjsFrame* frame) { if (!frame) { return; } - mjCFrame* frameC = reinterpret_cast(frame->element); - mjCBase* baseC = reinterpret_cast(dest); + mjCFrame* frameC = static_cast(frame->element); + mjCBase* baseC = static_cast(dest); baseC->SetFrame(frameC); } @@ -507,16 +507,16 @@ const char* mjs_resolveOrientation(double quat[4], mjtByte degree, const char* s // get id -int mjs_getId(mjElement element) { - return reinterpret_cast(element)->id; +int mjs_getId(mjElement* element) { + return static_cast(element)->id; } // set default -void mjs_setDefault(mjElement element, mjsDefault* defspec) { - mjCBase* baseC = reinterpret_cast(element); - baseC->def = reinterpret_cast(defspec->element); +void mjs_setDefault(mjElement* element, mjsDefault* defspec) { + mjCBase* baseC = static_cast(element); + baseC->def = static_cast(defspec->element); } @@ -631,7 +631,7 @@ const double* mjs_getDouble(const mjDoubleVec source, int* size) { // set plugin attributes void mjs_setPluginAttributes(mjsPlugin* plugin, void* attributes) { - mjCPlugin* pluginC = reinterpret_cast(plugin->instance); + mjCPlugin* pluginC = static_cast(plugin->instance); std::map>* config_attribs = reinterpret_cast>*>(attributes); pluginC->config_attribs = std::move(*config_attribs); @@ -641,7 +641,7 @@ void mjs_setPluginAttributes(mjsPlugin* plugin, void* attributes) { // Set active plugins. void mjs_setActivePlugins(mjSpec* s, void* activeplugins) { - mjCModel* modelC = reinterpret_cast(s->element); + mjCModel* modelC = static_cast(s->element); std::vector>* active_plugins = reinterpret_cast>*>(activeplugins); modelC->active_plugins = std::move(*active_plugins); @@ -651,7 +651,7 @@ void mjs_setActivePlugins(mjSpec* s, void* activeplugins) { // compute full inertia const char* mjs_setFullInertia(mjsBody* bodyspec, double quat[4], double inertia[3]) { - mjCBody* body = reinterpret_cast(bodyspec->element); + mjCBody* body = static_cast(bodyspec->element); return body->FullInertia(quat, inertia); } diff --git a/src/user/user_api.h b/src/user/user_api.h index a9ac8bde..b448f3e4 100644 --- a/src/user/user_api.h +++ b/src/user/user_api.h @@ -29,7 +29,6 @@ extern "C" { //---------------------------------- handles to internal objects ----------------------------------- -typedef struct _mjElement* mjElement; typedef struct _mjString* mjString; typedef struct _mjStringVec* mjStringVec; typedef struct _mjIntVec* mjIntVec; @@ -77,10 +76,15 @@ typedef enum _mjtInertiaFromGeom { } mjtInertiaFromGeom; -//---------------------------------- attribute structs (mjm) --------------------------------------- +//---------------------------------- attribute structs (mjs) --------------------------------------- + +typedef struct _mjElement { // element type, do not modify + mjtObj elemtype; // element type +} mjElement; + typedef struct _mjSpec { // model specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjStatistic stat; // statistics override (if defined) // compiler settings @@ -144,7 +148,7 @@ typedef struct _mjsOrientation { // alternative orientation specifiers typedef struct _mjsPlugin { // plugin specification - mjElement instance; // internal, do not modify + mjElement* instance; // object type mjString name; // name mjString instance_name; // instance name int plugin_slot; // global registered slot number of the plugin @@ -154,7 +158,7 @@ typedef struct _mjsPlugin { // plugin specification typedef struct _mjsBody { // body specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString childclass; // childclass name @@ -182,7 +186,7 @@ typedef struct _mjsBody { // body specification typedef struct _mjsFrame { // frame specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString childclass; // childclass name double pos[3]; // position @@ -193,7 +197,7 @@ typedef struct _mjsFrame { // frame specification typedef struct _mjsJoint { // joint specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name mjtJoint type; // joint type @@ -233,7 +237,7 @@ typedef struct _mjsJoint { // joint specification typedef struct _mjsGeom { // geom specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // classname mjtGeom type; // geom type @@ -282,7 +286,7 @@ typedef struct _mjsGeom { // geom specification typedef struct _mjsSite { // site specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name @@ -306,7 +310,7 @@ typedef struct _mjsSite { // site specification typedef struct _mjsCamera { // camera specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name @@ -335,7 +339,7 @@ typedef struct _mjsCamera { // camera specification typedef struct _mjsLight { // light specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name @@ -362,7 +366,7 @@ typedef struct _mjsLight { // light specification typedef struct _mjsFlex { - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name @@ -403,7 +407,7 @@ typedef struct _mjsFlex { typedef struct _mjsMesh { // mesh specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name mjString content_type; // content type of file @@ -424,7 +428,7 @@ typedef struct _mjsMesh { // mesh specification typedef struct _mjsHField { // height field specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString content_type; // content type of file mjString file; // file: (nrow, ncol, [elevation data]) @@ -438,7 +442,7 @@ typedef struct _mjsHField { // height field specification typedef struct _mjsSkin { // skin specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name mjString file; // skin file @@ -465,7 +469,7 @@ typedef struct _mjsSkin { // skin specification typedef struct _mjsTexture { // texture specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name mjtTexture type; // texture type @@ -499,7 +503,7 @@ typedef struct _mjsTexture { // texture specification typedef struct _mjsMaterial { // material specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name mjString texture; // name of texture (empty: none) @@ -515,7 +519,7 @@ typedef struct _mjsMaterial { // material specification typedef struct _mjsPair { - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name mjString geomname1; // name of geom 1 @@ -534,7 +538,7 @@ typedef struct _mjsPair { typedef struct _mjsExclude { - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString bodyname1; // name of geom 1 mjString bodyname2; // name of geom 2 @@ -543,7 +547,7 @@ typedef struct _mjsExclude { typedef struct _mjsEquality { // equality specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name mjtEq type; // constraint type @@ -558,7 +562,7 @@ typedef struct _mjsEquality { // equality specification typedef struct _mjsTendon { // tendon specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name @@ -590,13 +594,13 @@ typedef struct _mjsTendon { // tendon specification typedef struct _mjsWrap { // wrapping object specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString info; // message appended to errors } mjsWrap; typedef struct _mjsActuator { // actuator specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name @@ -640,7 +644,7 @@ typedef struct _mjsActuator { // actuator specification typedef struct _mjsSensor { // sensor specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString classname; // class name @@ -668,7 +672,7 @@ typedef struct _mjsSensor { // sensor specification typedef struct _mjsNumeric { // custom numeric field specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjDoubleVec data; // initialization data int size; // array size, can be bigger than data size @@ -677,7 +681,7 @@ typedef struct _mjsNumeric { // custom numeric field specification typedef struct _mjsText { // custom text specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjString data; // text string mjString info; // message appended to compiler errors @@ -685,7 +689,7 @@ typedef struct _mjsText { // custom text specification typedef struct _mjsTuple { // tuple specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name mjIntVec objtype; // object types mjStringVec objname; // object names @@ -695,7 +699,7 @@ typedef struct _mjsTuple { // tuple specification typedef struct _mjsKey { // keyframe specification - mjElement element; // internal, do not modify + mjElement* element; // object type mjString name; // name double time; // time mjDoubleVec qpos; // qpos @@ -710,7 +714,7 @@ typedef struct _mjsKey { // keyframe specification typedef struct _mjsDefault { // default specification mjString name; // name - mjElement element; // internal, do not modify + mjElement* element; // object type mjsJoint* joint; // joint defaults mjsGeom* geom; // geom defaults mjsSite* site; // site defaults @@ -847,7 +851,7 @@ MJAPI mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, int parentid, MJAPI mjSpec* mjs_getSpec(mjsBody* body); // Get default corresponding to an mjElement. -MJAPI mjsDefault* mjs_getDefault(mjElement element); +MJAPI mjsDefault* mjs_getDefault(mjElement* element); // Find default in model by class name. MJAPI mjsDefault* mjs_findDefault(mjSpec* s, const char* classname); @@ -868,7 +872,7 @@ MJAPI mjsMesh* mjs_findMesh(mjSpec* s, const char* name); MJAPI mjsFrame* mjs_findFrame(mjSpec* s, const char* name); // Get element id. -MJAPI int mjs_getId(mjElement element); +MJAPI int mjs_getId(mjElement* element); // Copy text to string. MJAPI void mjs_setString(mjString dest, const char* text); @@ -910,10 +914,10 @@ MJAPI void mjs_setPluginAttributes(mjsPlugin* plugin, void* attributes); MJAPI void mjs_setActivePlugins(mjSpec* s, void* activeplugins); // Set default. -MJAPI void mjs_setDefault(mjElement element, mjsDefault* def); +MJAPI void mjs_setDefault(mjElement* element, mjsDefault* def); // Set frame. -MJAPI void mjs_setFrame(mjElement dest, mjsFrame* frame); +MJAPI void mjs_setFrame(mjElement* dest, mjsFrame* frame); // Resolve alternative orientations to quat. MJAPI const char* mjs_resolveOrientation(double quat[4], mjtByte degree, const char* sequence, diff --git a/src/user/user_composite.cc b/src/user/user_composite.cc index 4337557d..3a813b11 100644 --- a/src/user/user_composite.cc +++ b/src/user/user_composite.cc @@ -306,7 +306,7 @@ bool mjCComposite::Make(mjSpec* spec, mjsBody* body, char* error, int error_sz) // overwrite plugin name if (plugin_instance_name.empty() && plugin.active) { plugin_instance_name = "composite" + prefix; - ((mjCPlugin*)plugin.instance)->name = plugin_instance_name; + (static_cast(plugin.instance))->name = plugin_instance_name; } // dispatch @@ -432,7 +432,7 @@ bool mjCComposite::MakeParticle(mjCModel* model, mjsBody* body, char* error, int mjtNum t = 1; if (dim == 2 && plugin.active) { try { - mjCPlugin* pplugin = (mjCPlugin*)plugin.instance; + mjCPlugin* pplugin = static_cast(plugin.instance); t = std::stod(pplugin->config_attribs["thickness"], nullptr); } catch (const std::invalid_argument& e) { return comperr(error, "Invalid thickness attribute", error_sz); @@ -512,9 +512,9 @@ bool mjCComposite::MakeParticle(mjCModel* model, mjsBody* body, char* error, int // add plugin if (plugin.active) { mjsPlugin* pplugin = &b->plugin; - mjCPlugin* cplugin = (mjCPlugin*)plugin.instance; + mjCPlugin* cplugin = static_cast(plugin.instance); pplugin->active = true; - pplugin->instance = (mjElement)plugin.instance; + pplugin->instance = plugin.instance; mjs_setString(pplugin->instance_name, plugin_instance_name.c_str()); mjs_setString(pplugin->name, mjs_getString(plugin.name)); @@ -868,7 +868,7 @@ mjsBody* mjCComposite::AddCableBody(mjCModel* model, mjsBody* body, int ix, mjtN if (plugin.active) { mjsPlugin* pplugin = &body->plugin; pplugin->active = true; - pplugin->instance = (mjElement)plugin.instance; + pplugin->instance = plugin.instance; mjs_setString(pplugin->name, mjs_getString(plugin.name)); mjs_setString(pplugin->instance_name, plugin_instance_name.c_str()); } diff --git a/src/user/user_flexcomp.cc b/src/user/user_flexcomp.cc index e7cabbed..730f8ab1 100644 --- a/src/user/user_flexcomp.cc +++ b/src/user/user_flexcomp.cc @@ -412,7 +412,7 @@ bool mjCFlexcomp::Make(mjSpec* spec, mjsBody* body, char* error, int error_sz) { // overwrite plugin name if (plugin.active && plugin_instance_name.empty()) { plugin_instance_name = "flexcomp_" + name; - ((mjCPlugin*)plugin.instance)->name = plugin_instance_name; + static_cast(plugin.instance)->name = plugin_instance_name; } // create bodies, construct flex vert and vertbody @@ -430,7 +430,7 @@ bool mjCFlexcomp::Make(mjSpec* spec, mjsBody* body, char* error, int error_sz) { if (plugin.active) { mjsPlugin* pplugin = &body->plugin; pplugin->active = true; - pplugin->instance = (mjElement)plugin.instance; + pplugin->instance = static_cast(plugin.instance); mjs_setString(pplugin->name, mjs_getString(plugin.name)); mjs_setString(pplugin->instance_name, plugin_instance_name.c_str()); } @@ -494,7 +494,7 @@ bool mjCFlexcomp::Make(mjSpec* spec, mjsBody* body, char* error, int error_sz) { if (plugin.active) { mjsPlugin* pplugin = &pb->plugin; pplugin->active = true; - pplugin->instance = (mjElement)plugin.instance; + pplugin->instance = static_cast(plugin.instance); mjs_setString(pplugin->name, mjs_getString(plugin.name)); mjs_setString(pplugin->instance_name, plugin_instance_name.c_str()); } diff --git a/src/user/user_mesh.cc b/src/user/user_mesh.cc index 1bd6f5b4..cfed0213 100644 --- a/src/user/user_mesh.cc +++ b/src/user/user_mesh.cc @@ -131,6 +131,7 @@ static void ReadFromBuffer(T* dst, const char* src) { mjCMesh::mjCMesh(mjCModel* _model, mjCDef* _def) { mjs_defaultMesh(spec); + elemtype = mjOBJ_MESH; // clear internal variables mjuu_setvec(pos_surface_, 0, 0, 0); @@ -253,7 +254,7 @@ mjCMesh& mjCMesh::operator=(const mjCMesh& other) { this->graph_ = NULL; } if (other.plugin.instance) { - mjCPlugin* new_plugin = new mjCPlugin(*reinterpret_cast(other.plugin.instance)); + mjCPlugin* new_plugin = new mjCPlugin(*static_cast(other.plugin.instance)); plugin = new_plugin->spec; model->plugins.push_back(new_plugin); } @@ -265,7 +266,7 @@ mjCMesh& mjCMesh::operator=(const mjCMesh& other) { void mjCMesh::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.file = (mjString)&spec_file_; @@ -358,8 +359,9 @@ void mjCMesh::LoadSDF() { name.c_str(), id); } - mjCPlugin* plugin_instance = (mjCPlugin*)plugin.instance; + mjCPlugin* plugin_instance = static_cast(plugin.instance); model->ResolvePlugin(this, plugin_name, plugin_instance_name, &plugin_instance); + plugin.instance = plugin_instance; const mjpPlugin* pplugin = mjp_getPluginAtSlot(plugin_instance->spec.plugin_slot); if (!(pplugin->capabilityflags & mjPLUGIN_SDF)) { throw mjCError(this, "plugin '%s' does not support signed distance fields", pplugin->name); @@ -2056,6 +2058,7 @@ void mjCMesh::MakeCenter(void) { // constructor mjCSkin::mjCSkin(mjCModel* _model) { mjs_defaultSkin(spec); + elemtype = mjOBJ_SKIN; // set model pointer model = _model; @@ -2103,7 +2106,7 @@ mjCSkin& mjCSkin::operator=(const mjCSkin& other) { void mjCSkin::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.file = (mjString)&spec_file_; @@ -2480,6 +2483,7 @@ constexpr int eledge[3][6][2] = {{{ 0, 1}, {-1, -1}, {-1, -1}, // constructor mjCFlex::mjCFlex(mjCModel* _model) { mjs_defaultFlex(spec); + elemtype = mjOBJ_FLEX; // set model model = _model; @@ -2514,7 +2518,7 @@ mjCFlex& mjCFlex::operator=(const mjCFlex& other) { void mjCFlex::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.material = (mjString)&spec_material_; @@ -2756,7 +2760,8 @@ void mjCFlex::Compile(const mjVFS* vfs) { for (int i=0; i<(int)vertbodyid.size(); i++) { if (model->bodies[vertbodyid[i]]->plugin.instance) { - mjCPlugin* plugin_instance = (mjCPlugin*)model->bodies[vertbodyid[i]]->plugin.instance; + mjCPlugin* plugin_instance = + static_cast(model->bodies[vertbodyid[i]]->plugin.instance); plugin_instance->config_attribs["face"] = userface; plugin_instance->config_attribs["edge"] = useredge; } diff --git a/src/user/user_model.cc b/src/user/user_model.cc index c160a8a0..284f0260 100644 --- a/src/user/user_model.cc +++ b/src/user/user_model.cc @@ -84,6 +84,7 @@ static void copyvec(T1* dest, T2* src, int n) { // constructor mjCModel::mjCModel() { mjs_defaultSpec(spec); + elemtype = mjOBJ_UNKNOWN; spec_comment_.clear(); spec_modelfiledir_.clear(); spec_meshdir_.clear(); @@ -319,7 +320,7 @@ void mjCModel::CreateObjectLists() { void mjCModel::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.comment = (mjString)&spec_comment_; spec.modelfiledir = (mjString)&spec_modelfiledir_; spec.modelname = (mjString)&spec_modelname_; @@ -3226,7 +3227,7 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) { std::vector> plugin_to_actuators(nplugin); for (int i = 0; i < nu; ++i) { if (actuators[i]->plugin.active) { - int actuator_plugin = ((mjCPlugin*)actuators[i]->plugin.instance)->id; + int actuator_plugin = static_cast(actuators[i]->plugin.instance)->id; m->actuator_plugin[i] = actuator_plugin; plugin_to_actuators[actuator_plugin].push_back(i); } else { @@ -3236,7 +3237,7 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) { for (int i = 0; i < nbody; ++i) { if (bodies[i]->plugin.active) { - m->body_plugin[i] = ((mjCPlugin*)bodies[i]->plugin.instance)->id; + m->body_plugin[i] = static_cast(bodies[i]->plugin.instance)->id; } else { m->body_plugin[i] = -1; } @@ -3244,7 +3245,7 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) { for (int i = 0; i < ngeom; ++i) { if (geoms[i]->plugin.active) { - m->geom_plugin[i] = ((mjCPlugin*)geoms[i]->plugin.instance)->id; + m->geom_plugin[i] = static_cast(geoms[i]->plugin.instance)->id; } else { m->geom_plugin[i] = -1; } @@ -3253,7 +3254,7 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) { std::vector> plugin_to_sensors(nplugin); for (int i = 0; i < nsensor; ++i) { if (sensors[i]->type == mjSENS_PLUGIN) { - int sensor_plugin = ((mjCPlugin*)sensors[i]->plugin.instance)->id; + int sensor_plugin = static_cast(sensors[i]->plugin.instance)->id; m->sensor_plugin[i] = sensor_plugin; plugin_to_sensors[sensor_plugin].push_back(i); } else { diff --git a/src/user/user_model.h b/src/user/user_model.h index 167d3840..2256ea3b 100644 --- a/src/user/user_model.h +++ b/src/user/user_model.h @@ -33,7 +33,7 @@ typedef std::map > mjKeyMap; typedef std::array mjListKeyMap; -class mjCModel_ { +class mjCModel_ : public mjElement { public: // attach namespaces std::string prefix; diff --git a/src/user/user_objects.cc b/src/user/user_objects.cc index 6b5d41ee..e6383cc0 100644 --- a/src/user/user_objects.cc +++ b/src/user/user_objects.cc @@ -516,7 +516,7 @@ void mjCDef::PointToLocal() { equality.PointToLocal(); tendon.PointToLocal(); actuator.PointToLocal(); - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.joint = &joint.spec; spec.geom = &geom.spec; @@ -632,6 +632,7 @@ mjCBody::mjCBody(mjCModel* _model) { model = _model; mjs_defaultBody(spec); + elemtype = mjOBJ_BODY; parentid = -1; weldid = -1; dofnum = 0; @@ -745,7 +746,7 @@ mjCBody& mjCBody::operator+=(mjCBody& other) { void mjCBody::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.childclass = (mjString)&classname; spec.userdata = (mjDoubleVec)&spec_userdata_; @@ -1337,9 +1338,10 @@ void mjCBody::Compile(void) { name.c_str(), id); } - mjCPlugin** plugin_instance = (mjCPlugin**)&plugin.instance; - model->ResolvePlugin(this, plugin_name, plugin_instance_name, plugin_instance); - const mjpPlugin* pplugin = mjp_getPluginAtSlot((*plugin_instance)->spec.plugin_slot); + mjCPlugin* plugin_instance = static_cast(plugin.instance); + model->ResolvePlugin(this, plugin_name, plugin_instance_name, &plugin_instance); + plugin.instance = plugin_instance; + const mjpPlugin* pplugin = mjp_getPluginAtSlot(plugin_instance->spec.plugin_slot); if (!(pplugin->capabilityflags & mjPLUGIN_PASSIVE)) { throw mjCError(this, "plugin '%s' does not support passive forces", pplugin->name); } @@ -1365,6 +1367,7 @@ void mjCBody::Compile(void) { // initialize frame mjCFrame::mjCFrame(mjCModel* _model, mjCFrame* _frame) { mjs_defaultFrame(spec); + elemtype = mjOBJ_FRAME; compiled = false; model = _model; body = NULL; @@ -1417,7 +1420,7 @@ void mjCFrame::SetParent(mjCBody* _body) { void mjCFrame::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.childclass = (mjString)&classname; spec.info = (mjString)&info; @@ -1461,6 +1464,7 @@ void mjCFrame::Compile() { // initialize default joint mjCJoint::mjCJoint(mjCModel* _model, mjCDef* _def) { mjs_defaultJoint(spec); + elemtype = mjOBJ_JOINT; // clear internal variables spec_userdata_.clear(); @@ -1508,7 +1512,7 @@ bool mjCJoint::is_actfrclimited() const { return islimited(actfrclimited, actfrc void mjCJoint::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.userdata = (mjDoubleVec)&spec_userdata_; @@ -1658,6 +1662,7 @@ int mjCJoint::Compile(void) { // initialize default geom mjCGeom::mjCGeom(mjCModel* _model, mjCDef* _def) { mjs_defaultGeom(spec); + elemtype = mjOBJ_GEOM; mass_ = 0; body = 0; @@ -1715,7 +1720,7 @@ mjCGeom& mjCGeom::operator=(const mjCGeom& other) { // to be called after any default copy constructor void mjCGeom::PointToLocal(void) { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.info = (mjString)&info; spec.classname = (mjString)&classname; @@ -2263,9 +2268,10 @@ void mjCGeom::Compile(void) { name.c_str(), id); } - mjCPlugin** plugin_instance = (mjCPlugin**)&plugin.instance; - model->ResolvePlugin(this, plugin_name, plugin_instance_name, plugin_instance); - const mjpPlugin* pplugin = mjp_getPluginAtSlot((*plugin_instance)->spec.plugin_slot); + mjCPlugin* plugin_instance = static_cast(plugin.instance); + model->ResolvePlugin(this, plugin_name, plugin_instance_name, &plugin_instance); + plugin.instance = plugin_instance; + const mjpPlugin* pplugin = mjp_getPluginAtSlot(plugin_instance->spec.plugin_slot); if (!(pplugin->capabilityflags & mjPLUGIN_SDF)) { throw mjCError(this, "plugin '%s' does not support sign distance fields", pplugin->name); } @@ -2284,6 +2290,7 @@ void mjCGeom::Compile(void) { // initialize default site mjCSite::mjCSite(mjCModel* _model, mjCDef* _def) { mjs_defaultSite(spec); + elemtype = mjOBJ_SITE; // clear internal variables body = 0; @@ -2328,7 +2335,7 @@ mjCSite& mjCSite::operator=(const mjCSite& other) { void mjCSite::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.info = (mjString)&info; spec.classname = (mjString)&classname; @@ -2440,6 +2447,7 @@ void mjCSite::Compile(void) { // initialize defaults mjCCamera::mjCCamera(mjCModel* _model, mjCDef* _def) { mjs_defaultCamera(spec); + elemtype = mjOBJ_CAMERA; // clear private variables body = 0; @@ -2483,7 +2491,7 @@ mjCCamera& mjCCamera::operator=(const mjCCamera& other) { void mjCCamera::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.userdata = (mjDoubleVec)&spec_userdata_; @@ -2590,6 +2598,7 @@ void mjCCamera::Compile(void) { // initialize defaults mjCLight::mjCLight(mjCModel* _model, mjCDef* _def) { mjs_defaultLight(spec); + elemtype = mjOBJ_LIGHT; // clear private variables body = 0; @@ -2630,7 +2639,7 @@ mjCLight& mjCLight::operator=(const mjCLight& other) { void mjCLight::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.targetbody = (mjString)&spec_targetbody_; @@ -2686,6 +2695,7 @@ void mjCLight::Compile(void) { // constructor mjCHField::mjCHField(mjCModel* _model) { mjs_defaultHField(spec); + elemtype = mjOBJ_HFIELD; // set model pointer model = _model; @@ -2723,7 +2733,7 @@ mjCHField& mjCHField::operator=(const mjCHField& other) { void mjCHField::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.file = (mjString)&spec_file_; spec.content_type = (mjString)&spec_content_type_; @@ -2938,6 +2948,7 @@ void mjCHField::Compile(const mjVFS* vfs) { // initialize defaults mjCTexture::mjCTexture(mjCModel* _model) { mjs_defaultTexture(spec); + elemtype = mjOBJ_TEXTURE; // set model pointer model = _model; @@ -2979,7 +2990,7 @@ mjCTexture& mjCTexture::operator=(const mjCTexture& other) { void mjCTexture::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.file = (mjString)&spec_file_; @@ -3680,6 +3691,7 @@ void mjCTexture::Compile(const mjVFS* vfs) { // initialize defaults mjCMaterial::mjCMaterial(mjCModel* _model, mjCDef* _def) { mjs_defaultMaterial(spec); + elemtype = mjOBJ_MATERIAL; // clear internal spec_texture_.clear(); @@ -3722,7 +3734,7 @@ mjCMaterial& mjCMaterial::operator=(const mjCMaterial& other) { void mjCMaterial::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.texture = (mjString)&spec_texture_; @@ -3751,6 +3763,7 @@ void mjCMaterial::Compile(void) { // constructor mjCPair::mjCPair(mjCModel* _model, mjCDef* _def) { mjs_defaultPair(spec); + elemtype = mjOBJ_PAIR; // set defaults spec_geomname1_.clear(); @@ -3800,7 +3813,7 @@ mjCPair& mjCPair::operator=(const mjCPair& other) { void mjCPair::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.geomname1 = (mjString)&spec_geomname1_; @@ -4010,7 +4023,7 @@ mjCBodyPair& mjCBodyPair::operator=(const mjCBodyPair& other) { void mjCBodyPair::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.bodyname1 = (mjString)&spec_bodyname1_; spec.bodyname2 = (mjString)&spec_bodyname2_; @@ -4086,6 +4099,7 @@ void mjCBodyPair::Compile(void) { // initialize default constraint mjCEquality::mjCEquality(mjCModel* _model, mjCDef* _def) { mjs_defaultEquality(spec); + elemtype = mjOBJ_EQUALITY; // clear internal variables spec_name1_.clear(); @@ -4129,7 +4143,7 @@ mjCEquality& mjCEquality::operator=(const mjCEquality& other) { void mjCEquality::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.name1 = (mjString)&spec_name1_; @@ -4248,6 +4262,7 @@ void mjCEquality::Compile(void) { // constructor mjCTendon::mjCTendon(mjCModel* _model, mjCDef* _def) { mjs_defaultTendon(spec); + elemtype = mjOBJ_TENDON; // clear internal variables spec_material_.clear(); @@ -4299,7 +4314,7 @@ bool mjCTendon::is_limited() const { return islimited(limited, range); } void mjCTendon::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.material = (mjString)&spec_material_; @@ -4576,6 +4591,8 @@ void mjCTendon::Compile(void) { // constructor mjCWrap::mjCWrap(mjCModel* _model, mjCTendon* _tendon) { + elemtype = mjOBJ_UNKNOWN; + // set model and tendon pointer model = _model; tendon = _tendon; @@ -4613,7 +4630,7 @@ mjCWrap& mjCWrap::operator=(const mjCWrap& other) { void mjCWrap::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.info = (mjString)&info; } @@ -4705,6 +4722,7 @@ void mjCWrap::ResolveReferences(const mjCModel* m) { // initialize defaults mjCActuator::mjCActuator(mjCModel* _model, mjCDef* _def) { mjs_defaultActuator(spec); + elemtype = mjOBJ_ACTUATOR; // clear private variables ptarget = nullptr; @@ -4758,7 +4776,7 @@ bool mjCActuator::is_actlimited() const { return islimited(actlimited, actrange) void mjCActuator::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.userdata = (mjDoubleVec)&spec_userdata_; @@ -5032,9 +5050,10 @@ void mjCActuator::Compile(void) { name.c_str(), id); } - mjCPlugin** plugin_instance = (mjCPlugin**)&plugin.instance; - model->ResolvePlugin(this, plugin_name, plugin_instance_name, plugin_instance); - const mjpPlugin* pplugin = mjp_getPluginAtSlot((*plugin_instance)->spec.plugin_slot); + mjCPlugin* plugin_instance = static_cast(plugin.instance); + model->ResolvePlugin(this, plugin_name, plugin_instance_name, &plugin_instance); + plugin.instance = plugin_instance; + const mjpPlugin* pplugin = mjp_getPluginAtSlot(plugin_instance->spec.plugin_slot); if (!(pplugin->capabilityflags & mjPLUGIN_ACTUATOR)) { throw mjCError(this, "plugin '%s' does not support actuators", pplugin->name); } @@ -5048,6 +5067,7 @@ void mjCActuator::Compile(void) { // initialize defaults mjCSensor::mjCSensor(mjCModel* _model) { mjs_defaultSensor(spec); + elemtype = mjOBJ_SENSOR; // set model model = _model; @@ -5090,7 +5110,7 @@ mjCSensor& mjCSensor::operator=(const mjCSensor& other) { void mjCSensor::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.classname = (mjString)&classname; spec.userdata = (mjDoubleVec)&spec_userdata_; @@ -5504,9 +5524,10 @@ void mjCSensor::Compile(void) { // resolve plugin instance, or create one if using the "plugin" attribute shortcut { - mjCPlugin** plugin_instance = (mjCPlugin**)&plugin.instance; - model->ResolvePlugin(this, plugin_name, plugin_instance_name, plugin_instance); - const mjpPlugin* pplugin = mjp_getPluginAtSlot((*plugin_instance)->spec.plugin_slot); + mjCPlugin* plugin_instance = static_cast(plugin.instance); + model->ResolvePlugin(this, plugin_name, plugin_instance_name, &plugin_instance); + plugin.instance = plugin_instance; + const mjpPlugin* pplugin = mjp_getPluginAtSlot(plugin_instance->spec.plugin_slot); if (!(pplugin->capabilityflags & mjPLUGIN_SENSOR)) { throw mjCError(this, "plugin '%s' does not support sensors", pplugin->name); } @@ -5534,6 +5555,7 @@ void mjCSensor::Compile(void) { // constructor mjCNumeric::mjCNumeric(mjCModel* _model) { mjs_defaultNumeric(spec); + elemtype = mjOBJ_NUMERIC; // set model pointer model = _model; @@ -5569,7 +5591,7 @@ mjCNumeric& mjCNumeric::operator=(const mjCNumeric& other) { void mjCNumeric::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.data = (mjDoubleVec)&spec_data_; spec.info = (mjString)&info; @@ -5622,6 +5644,7 @@ void mjCNumeric::Compile(void) { // constructor mjCText::mjCText(mjCModel* _model) { mjs_defaultText(spec); + elemtype = mjOBJ_TEXT; // set model pointer model = _model; @@ -5657,7 +5680,7 @@ mjCText& mjCText::operator=(const mjCText& other) { void mjCText::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.data = (mjString)&spec_data_; spec.info = (mjString)&info; @@ -5698,6 +5721,7 @@ void mjCText::Compile(void) { // constructor mjCTuple::mjCTuple(mjCModel* _model) { mjs_defaultTuple(spec); + elemtype = mjOBJ_TUPLE; // set model pointer model = _model; @@ -5736,7 +5760,7 @@ mjCTuple& mjCTuple::operator=(const mjCTuple& other) { void mjCTuple::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.objtype = (mjIntVec)&spec_objtype_; spec.objname = (mjStringVec)&spec_objname_; @@ -5830,6 +5854,7 @@ void mjCTuple::Compile(void) { // constructor mjCKey::mjCKey(mjCModel* _model) { mjs_defaultKey(spec); + elemtype = mjOBJ_KEY; // set model pointer model = _model; @@ -5870,7 +5895,7 @@ mjCKey& mjCKey::operator=(const mjCKey& other) { void mjCKey::PointToLocal() { - spec.element = (mjElement)this; + spec.element = static_cast(this); spec.name = (mjString)&name; spec.qpos = (mjDoubleVec)&spec_qpos_; spec.qvel = (mjDoubleVec)&spec_qvel_; @@ -6013,6 +6038,7 @@ mjCPlugin::mjCPlugin(mjCModel* _model) { // public interface mjs_defaultPlugin(spec); + elemtype = mjOBJ_PLUGIN; spec.name = (mjString)&name; spec.instance_name = (mjString)&instance_name; spec.info = (mjString)&info; diff --git a/src/user/user_objects.h b/src/user/user_objects.h index 1e24ab34..df1ef8bb 100644 --- a/src/user/user_objects.h +++ b/src/user/user_objects.h @@ -167,7 +167,7 @@ class mjCBoundingVolumeHierarchy : public mjCBoundingVolumeHierarchy_ { //------------------------- class mjCBase ---------------------------------------------------------- // Generic functionality for all derived classes -class mjCBase_ { +class mjCBase_ : public mjElement { public: int id; // object id std::string name; // object name @@ -1606,7 +1606,7 @@ class mjCKey : public mjCKey_, private mjsKey { //------------------------- class mjCDef ----------------------------------------------------------- // Describes one set of defaults -class mjCDef { +class mjCDef : public mjElement { friend class mjXWriter; public: diff --git a/src/xml/xml_native_writer.cc b/src/xml/xml_native_writer.cc index 642a96d4..9a20b95a 100644 --- a/src/xml/xml_native_writer.cc +++ b/src/xml/xml_native_writer.cc @@ -739,8 +739,8 @@ void mjXWriter::OnePlugin(XMLElement* elem, mjsPlugin* plugin) { } else { WriteAttrTxt(elem, "plugin", plugin_name); const mjpPlugin* pplugin = mjp_getPluginAtSlot( - ((mjCPlugin*)plugin->instance)->spec.plugin_slot); - const char* c = &((mjCPlugin*)plugin->instance)->flattened_attributes[0]; + static_cast(plugin->instance)->spec.plugin_slot); + const char* c = &(static_cast(plugin->instance)->flattened_attributes[0]); for (int i = 0; i < pplugin->nattribute; ++i) { std::string value(c); if (!value.empty()) {