From 579a27e9d27459fdb492495fd46d91cca0b29b1a Mon Sep 17 00:00:00 2001 From: Sam Haves Date: Tue, 5 May 2026 12:34:58 -0700 Subject: [PATCH] Make mjSpec related getter/find utilities take const parameters. PiperOrigin-RevId: 910848529 Change-Id: I1c966a6299fe47317cbf0bfb8f8b835cbcd4ed65 --- doc/includes/references.h | 42 +++++----- include/mujoco/mujoco.h | 42 +++++----- python/mujoco/introspect/functions.py | 46 +++++------ src/user/user_api.cc | 112 ++++++++++++-------------- src/user/user_api.h | 40 ++++----- src/user/user_model.cc | 14 ++-- src/user/user_model.h | 6 +- src/user/user_objects.cc | 8 +- src/user/user_objects.h | 4 +- wasm/codegen/generated/bindings.cc | 42 +++++----- 10 files changed, 175 insertions(+), 181 deletions(-) diff --git a/doc/includes/references.h b/doc/includes/references.h index edc8b0c4..c96db9b3 100644 --- a/doc/includes/references.h +++ b/doc/includes/references.h @@ -3686,27 +3686,27 @@ mjsSkin* mjs_addSkin(mjSpec* s); mjsTexture* mjs_addTexture(mjSpec* s); mjsMaterial* mjs_addMaterial(mjSpec* s, const mjsDefault* def); int mjs_makeMesh(mjsMesh* mesh, mjtMeshBuiltin builtin, double* params, int nparams); -mjSpec* mjs_getSpec(mjsElement* element); -mjsCompiler* mjs_getCompiler(mjsElement* element); -mjSpec* mjs_findSpec(mjSpec* spec, const char* name); -mjsBody* mjs_findBody(mjSpec* s, const char* name); -mjsElement* mjs_findElement(mjSpec* s, mjtObj type, const char* name); -mjsBody* mjs_findChild(mjsBody* body, const char* name); -mjsBody* mjs_getParent(mjsElement* element); -mjsFrame* mjs_getFrame(mjsElement* element); -mjsFrame* mjs_findFrame(mjSpec* s, const char* name); -mjsDefault* mjs_getDefault(mjsElement* element); -mjsDefault* mjs_findDefault(mjSpec* s, const char* classname); -mjsDefault* mjs_getSpecDefault(mjSpec* s); -int mjs_getId(mjsElement* element); -mjsElement* mjs_firstChild(mjsBody* body, mjtObj type, int recurse); -mjsElement* mjs_nextChild(mjsBody* body, mjsElement* child, int recurse); -mjsElement* mjs_firstElement(mjSpec* s, mjtObj type); -mjsElement* mjs_nextElement(mjSpec* s, mjsElement* element); -mjsElement* mjs_getWrapTarget(mjsWrap* wrap); -mjsSite* mjs_getWrapSideSite(mjsWrap* wrap); -double mjs_getWrapDivisor(mjsWrap* wrap); -double mjs_getWrapCoef(mjsWrap* wrap); +mjSpec* mjs_getSpec(const mjsElement* element); +mjsCompiler* mjs_getCompiler(const mjsElement* element); +mjSpec* mjs_findSpec(const mjSpec* spec, const char* name); +mjsBody* mjs_findBody(const mjSpec* s, const char* name); +mjsElement* mjs_findElement(const mjSpec* s, mjtObj type, const char* name); +mjsBody* mjs_findChild(const mjsBody* body, const char* name); +mjsBody* mjs_getParent(const mjsElement* element); +mjsFrame* mjs_getFrame(const mjsElement* element); +mjsFrame* mjs_findFrame(const mjSpec* s, const char* name); +mjsDefault* mjs_getDefault(const mjsElement* element); +mjsDefault* mjs_findDefault(const mjSpec* s, const char* classname); +mjsDefault* mjs_getSpecDefault(const mjSpec* s); +int mjs_getId(const mjsElement* element); +mjsElement* mjs_firstChild(const mjsBody* body, mjtObj type, int recurse); +mjsElement* mjs_nextChild(const mjsBody* body, const mjsElement* child, int recurse); +mjsElement* mjs_firstElement(const mjSpec* s, mjtObj type); +mjsElement* mjs_nextElement(const mjSpec* s, const mjsElement* element); +mjsElement* mjs_getWrapTarget(const mjsWrap* wrap); +mjsSite* mjs_getWrapSideSite(const mjsWrap* wrap); +double mjs_getWrapDivisor(const mjsWrap* wrap); +double mjs_getWrapCoef(const mjsWrap* wrap); int mjs_setName(mjsElement* element, const char* name); void mjs_setBuffer(mjByteVec* dest, const void* array, int size); void mjs_setString(mjString* dest, const char* text); diff --git a/include/mujoco/mujoco.h b/include/mujoco/mujoco.h index 45b56bb1..c9600ba6 100644 --- a/include/mujoco/mujoco.h +++ b/include/mujoco/mujoco.h @@ -1774,68 +1774,68 @@ MJAPI int mjs_makeMesh(mjsMesh* mesh, mjtMeshBuiltin builtin, double* params, in //---------------------------------- Find and get utilities ---------------------------------------- // Get spec from body. -MJAPI mjSpec* mjs_getSpec(mjsElement* element); +MJAPI mjSpec* mjs_getSpec(const mjsElement* element); // Get compiler associated with element's origin spec. -MJAPI mjsCompiler* mjs_getCompiler(mjsElement* element); +MJAPI mjsCompiler* mjs_getCompiler(const mjsElement* element); // Find spec (model asset) by name. -MJAPI mjSpec* mjs_findSpec(mjSpec* spec, const char* name); +MJAPI mjSpec* mjs_findSpec(const mjSpec* spec, const char* name); // Find body in spec by name. -MJAPI mjsBody* mjs_findBody(mjSpec* s, const char* name); +MJAPI mjsBody* mjs_findBody(const mjSpec* s, const char* name); // Find element in spec by name. -MJAPI mjsElement* mjs_findElement(mjSpec* s, mjtObj type, const char* name); +MJAPI mjsElement* mjs_findElement(const mjSpec* s, mjtObj type, const char* name); // Find child body by name. -MJAPI mjsBody* mjs_findChild(mjsBody* body, const char* name); +MJAPI mjsBody* mjs_findChild(const mjsBody* body, const char* name); // Get parent body. -MJAPI mjsBody* mjs_getParent(mjsElement* element); +MJAPI mjsBody* mjs_getParent(const mjsElement* element); // Get parent frame. -MJAPI mjsFrame* mjs_getFrame(mjsElement* element); +MJAPI mjsFrame* mjs_getFrame(const mjsElement* element); // Find frame by name. -MJAPI mjsFrame* mjs_findFrame(mjSpec* s, const char* name); +MJAPI mjsFrame* mjs_findFrame(const mjSpec* s, const char* name); // Get default corresponding to an element. -MJAPI mjsDefault* mjs_getDefault(mjsElement* element); +MJAPI mjsDefault* mjs_getDefault(const mjsElement* element); // Find default in model by class name. -MJAPI mjsDefault* mjs_findDefault(mjSpec* s, const char* classname); +MJAPI mjsDefault* mjs_findDefault(const mjSpec* s, const char* classname); // Get global default from model. -MJAPI mjsDefault* mjs_getSpecDefault(mjSpec* s); +MJAPI mjsDefault* mjs_getSpecDefault(const mjSpec* s); // Get element id. -MJAPI int mjs_getId(mjsElement* element); +MJAPI int mjs_getId(const mjsElement* element); // Return body's first child of given type. If recurse is nonzero, also search the body's subtree. -MJAPI mjsElement* mjs_firstChild(mjsBody* body, mjtObj type, int recurse); +MJAPI mjsElement* mjs_firstChild(const mjsBody* body, mjtObj type, int recurse); // Return body's next child of the same type; return NULL if child is last. // If recurse is nonzero, also search the body's subtree. -MJAPI mjsElement* mjs_nextChild(mjsBody* body, mjsElement* child, int recurse); +MJAPI mjsElement* mjs_nextChild(const mjsBody* body, const mjsElement* child, int recurse); // Return spec's first element of selected type. -MJAPI mjsElement* mjs_firstElement(mjSpec* s, mjtObj type); +MJAPI mjsElement* mjs_firstElement(const mjSpec* s, mjtObj type); // Return spec's next element; return NULL if element is last. -MJAPI mjsElement* mjs_nextElement(mjSpec* s, mjsElement* element); +MJAPI mjsElement* mjs_nextElement(const mjSpec* s, const mjsElement* element); // Get wrapped element in tendon path. -MJAPI mjsElement* mjs_getWrapTarget(mjsWrap* wrap); +MJAPI mjsElement* mjs_getWrapTarget(const mjsWrap* wrap); // Get wrapped element side site in tendon path if it has one, nullptr otherwise. -MJAPI mjsSite* mjs_getWrapSideSite(mjsWrap* wrap); +MJAPI mjsSite* mjs_getWrapSideSite(const mjsWrap* wrap); // Get divisor of mjsWrap wrapping a puller. -MJAPI double mjs_getWrapDivisor(mjsWrap* wrap); +MJAPI double mjs_getWrapDivisor(const mjsWrap* wrap); // Get coefficient of mjsWrap wrapping a joint. -MJAPI double mjs_getWrapCoef(mjsWrap* wrap); +MJAPI double mjs_getWrapCoef(const mjsWrap* wrap); //---------------------------------- Attribute setters --------------------------------------------- diff --git a/python/mujoco/introspect/functions.py b/python/mujoco/introspect/functions.py index ff12151e..0bfc82b9 100644 --- a/python/mujoco/introspect/functions.py +++ b/python/mujoco/introspect/functions.py @@ -11120,7 +11120,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='element', type=PointerType( - inner_type=ValueType(name='mjsElement'), + inner_type=ValueType(name='mjsElement', is_const=True), ), ), ), @@ -11136,7 +11136,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='element', type=PointerType( - inner_type=ValueType(name='mjsElement'), + inner_type=ValueType(name='mjsElement', is_const=True), ), ), ), @@ -11152,7 +11152,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='spec', type=PointerType( - inner_type=ValueType(name='mjSpec'), + inner_type=ValueType(name='mjSpec', is_const=True), ), ), FunctionParameterDecl( @@ -11174,7 +11174,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='s', type=PointerType( - inner_type=ValueType(name='mjSpec'), + inner_type=ValueType(name='mjSpec', is_const=True), ), ), FunctionParameterDecl( @@ -11196,7 +11196,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='s', type=PointerType( - inner_type=ValueType(name='mjSpec'), + inner_type=ValueType(name='mjSpec', is_const=True), ), ), FunctionParameterDecl( @@ -11222,7 +11222,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='body', type=PointerType( - inner_type=ValueType(name='mjsBody'), + inner_type=ValueType(name='mjsBody', is_const=True), ), ), FunctionParameterDecl( @@ -11244,7 +11244,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='element', type=PointerType( - inner_type=ValueType(name='mjsElement'), + inner_type=ValueType(name='mjsElement', is_const=True), ), ), ), @@ -11260,7 +11260,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='element', type=PointerType( - inner_type=ValueType(name='mjsElement'), + inner_type=ValueType(name='mjsElement', is_const=True), ), ), ), @@ -11276,7 +11276,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='s', type=PointerType( - inner_type=ValueType(name='mjSpec'), + inner_type=ValueType(name='mjSpec', is_const=True), ), ), FunctionParameterDecl( @@ -11298,7 +11298,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='element', type=PointerType( - inner_type=ValueType(name='mjsElement'), + inner_type=ValueType(name='mjsElement', is_const=True), ), ), ), @@ -11314,7 +11314,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='s', type=PointerType( - inner_type=ValueType(name='mjSpec'), + inner_type=ValueType(name='mjSpec', is_const=True), ), ), FunctionParameterDecl( @@ -11336,7 +11336,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='s', type=PointerType( - inner_type=ValueType(name='mjSpec'), + inner_type=ValueType(name='mjSpec', is_const=True), ), ), ), @@ -11350,7 +11350,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='element', type=PointerType( - inner_type=ValueType(name='mjsElement'), + inner_type=ValueType(name='mjsElement', is_const=True), ), ), ), @@ -11366,7 +11366,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='body', type=PointerType( - inner_type=ValueType(name='mjsBody'), + inner_type=ValueType(name='mjsBody', is_const=True), ), ), FunctionParameterDecl( @@ -11390,13 +11390,13 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='body', type=PointerType( - inner_type=ValueType(name='mjsBody'), + inner_type=ValueType(name='mjsBody', is_const=True), ), ), FunctionParameterDecl( name='child', type=PointerType( - inner_type=ValueType(name='mjsElement'), + inner_type=ValueType(name='mjsElement', is_const=True), ), ), FunctionParameterDecl( @@ -11416,7 +11416,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='s', type=PointerType( - inner_type=ValueType(name='mjSpec'), + inner_type=ValueType(name='mjSpec', is_const=True), ), ), FunctionParameterDecl( @@ -11436,13 +11436,13 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='s', type=PointerType( - inner_type=ValueType(name='mjSpec'), + inner_type=ValueType(name='mjSpec', is_const=True), ), ), FunctionParameterDecl( name='element', type=PointerType( - inner_type=ValueType(name='mjsElement'), + inner_type=ValueType(name='mjsElement', is_const=True), ), ), ), @@ -11458,7 +11458,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='wrap', type=PointerType( - inner_type=ValueType(name='mjsWrap'), + inner_type=ValueType(name='mjsWrap', is_const=True), ), ), ), @@ -11474,7 +11474,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='wrap', type=PointerType( - inner_type=ValueType(name='mjsWrap'), + inner_type=ValueType(name='mjsWrap', is_const=True), ), ), ), @@ -11488,7 +11488,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='wrap', type=PointerType( - inner_type=ValueType(name='mjsWrap'), + inner_type=ValueType(name='mjsWrap', is_const=True), ), ), ), @@ -11502,7 +11502,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ FunctionParameterDecl( name='wrap', type=PointerType( - inner_type=ValueType(name='mjsWrap'), + inner_type=ValueType(name='mjsWrap', is_const=True), ), ), ), diff --git a/src/user/user_api.cc b/src/user/user_api.cc index f03b5e85..5b91a772 100644 --- a/src/user/user_api.cc +++ b/src/user/user_api.cc @@ -1302,61 +1302,56 @@ const char* mjs_setToDCMotor(mjsActuator* actuator, double motorconst[2], double // get spec from body -mjSpec* mjs_getSpec(mjsElement* element) { - return &(static_cast(element)->model->spec); +mjSpec* mjs_getSpec(const mjsElement* element) { + return &(static_cast(element)->model->spec); } -mjsCompiler* mjs_getCompiler(mjsElement* element) { - return static_cast(element)->compiler; +mjsCompiler* mjs_getCompiler(const mjsElement* element) { + return static_cast(element)->compiler; } // find spec (model asset) by name -mjSpec* mjs_findSpec(mjSpec* s, const char* name) { - mjCModel* model = static_cast(s->element); +mjSpec* mjs_findSpec(const mjSpec* s, const char* name) { + const mjCModel* model = static_cast(s->element); return model->FindSpec(name); } // get default -mjsDefault* mjs_getDefault(mjsElement* element) { - mjCModel* model = static_cast(element)->model; - std::string classname = static_cast(element)->classname; - return &(model->def_map[classname]->spec); +mjsDefault* mjs_getDefault(const mjsElement* element) { + const mjCModel* model = static_cast(element)->model; + std::string classname = static_cast(element)->classname; + auto it = model->def_map.find(classname); + return (it != model->def_map.end()) ? &it->second->spec : nullptr; } // Find default with given name in model. -mjsDefault* mjs_findDefault(mjSpec* s, const char* classname) { - mjCModel* modelC = static_cast(s->element); +mjsDefault* mjs_findDefault(const mjSpec* s, const char* classname) { + const mjCModel* modelC = static_cast(s->element); mjCDef* cdef = modelC->FindDefault(classname); - if (!cdef) { - return nullptr; - } - return &cdef->spec; + return cdef ? &cdef->spec : nullptr; } // get default[0] from model -mjsDefault* mjs_getSpecDefault(mjSpec* s) { - mjCModel* modelC = static_cast(s->element); +mjsDefault* mjs_getSpecDefault(const mjSpec* s) { + const mjCModel* modelC = static_cast(s->element); mjCDef* def = modelC->Default(); - if (!def) { - return nullptr; - } - return &def->spec; + return def ? &def->spec : nullptr; } // find body in model by name -mjsBody* mjs_findBody(mjSpec* s, const char* name) { +mjsBody* mjs_findBody(const mjSpec* s, const char* name) { mjsElement* body = mjs_findElement(s, mjOBJ_BODY, name); return body ? &(static_cast(body)->spec) : nullptr; } @@ -1364,7 +1359,7 @@ mjsBody* mjs_findBody(mjSpec* s, const char* name) { // find element in spec by name -mjsElement* mjs_findElement(mjSpec* s, mjtObj type, const char* name) { +mjsElement* mjs_findElement(const mjSpec* s, mjtObj type, const char* name) { mjCModel* model = static_cast(s->element); if (model->IsCompiled() && type != mjOBJ_FRAME) { return model->FindObject(type, std::string(name)); // fast lookup @@ -1390,8 +1385,8 @@ mjsElement* mjs_findElement(mjSpec* s, mjtObj type, const char* name) { // find child of a body by name -mjsBody* mjs_findChild(mjsBody* bodyspec, const char* name) { - mjCBody* body = static_cast(bodyspec->element); +mjsBody* mjs_findChild(const mjsBody* bodyspec, const char* name) { + const mjCBody* body = static_cast(bodyspec->element); mjCBase* child = body->FindObject(mjOBJ_BODY, std::string(name)); return child ? &(static_cast(child)->spec) : nullptr; } @@ -1399,22 +1394,22 @@ mjsBody* mjs_findChild(mjsBody* bodyspec, const char* name) { // get parent body -mjsBody* mjs_getParent(mjsElement* element) { +mjsBody* mjs_getParent(const mjsElement* element) { switch (element->elemtype) { case mjOBJ_BODY: - return &(static_cast(element)->GetParent()->spec); + return &(static_cast(element)->GetParent()->spec); case mjOBJ_FRAME: - return &(static_cast(element)->GetParent()->spec); + return &(static_cast(element)->GetParent()->spec); case mjOBJ_JOINT: - return &(static_cast(element)->GetParent()->spec); + return &(static_cast(element)->GetParent()->spec); case mjOBJ_GEOM: - return &(static_cast(element)->GetParent()->spec); + return &(static_cast(element)->GetParent()->spec); case mjOBJ_SITE: - return &(static_cast(element)->GetParent()->spec); + return &(static_cast(element)->GetParent()->spec); case mjOBJ_CAMERA: - return &(static_cast(element)->GetParent()->spec); + return &(static_cast(element)->GetParent()->spec); case mjOBJ_LIGHT: - return &(static_cast(element)->GetParent()->spec); + return &(static_cast(element)->GetParent()->spec); default: return nullptr; } @@ -1423,8 +1418,8 @@ mjsBody* mjs_getParent(mjsElement* element) { // get parent frame -mjsFrame* mjs_getFrame(mjsElement* element) { - mjCBase* base = static_cast(element); +mjsFrame* mjs_getFrame(const mjsElement* element) { + const mjCBase* base = static_cast(element); switch (element->elemtype) { case mjOBJ_BODY: case mjOBJ_FRAME: @@ -1442,7 +1437,7 @@ mjsFrame* mjs_getFrame(mjsElement* element) { // find frame by name -mjsFrame* mjs_findFrame(mjSpec* s, const char* name) { +mjsFrame* mjs_findFrame(const mjSpec* s, const char* name) { mjsElement* frame = mjs_findElement(s, mjOBJ_FRAME, name); return frame ? &(static_cast(frame)->spec) : nullptr; } @@ -1601,11 +1596,11 @@ int mjs_sensorDim(const mjsSensor* sensor) { // get id -int mjs_getId(mjsElement* element) { +int mjs_getId(const mjsElement* element) { if (!element) { return -1; } - return static_cast(element)->id; + return static_cast(element)->id; } @@ -1619,8 +1614,8 @@ void mjs_setDefault(mjsElement* element, const mjsDefault* defspec) { // return first child of selected type -mjsElement* mjs_firstChild(mjsBody* body, mjtObj type, int recurse) { - mjCBody* bodyC = static_cast(body->element); +mjsElement* mjs_firstChild(const mjsBody* body, mjtObj type, int recurse) { + const mjCBody* bodyC = static_cast(body->element); try { return bodyC->NextChild(NULL, type, recurse); } catch (mjCError& e) { @@ -1632,8 +1627,8 @@ mjsElement* mjs_firstChild(mjsBody* body, mjtObj type, int recurse) { // return body's next child; return NULL if child is last -mjsElement* mjs_nextChild(mjsBody* body, mjsElement* child, int recurse) { - mjCBody* bodyC = static_cast(body->element); +mjsElement* mjs_nextChild(const mjsBody* body, const mjsElement* child, int recurse) { + const mjCBody* bodyC = static_cast(body->element); try { return bodyC->NextChild(child, child->elemtype, recurse); } catch(mjCError& e) { @@ -1645,23 +1640,23 @@ mjsElement* mjs_nextChild(mjsBody* body, mjsElement* child, int recurse) { // return spec's first element of selected type -mjsElement* mjs_firstElement(mjSpec* s, mjtObj type) { - mjCModel* modelC = static_cast(s->element); +mjsElement* mjs_firstElement(const mjSpec* s, mjtObj type) { + const mjCModel* modelC = static_cast(s->element); return modelC->NextObject(NULL, type); } // return spec's next element; return NULL if element is last -mjsElement* mjs_nextElement(mjSpec* s, mjsElement* element) { - mjCModel* modelC = static_cast(s->element); +mjsElement* mjs_nextElement(const mjSpec* s, const mjsElement* element) { + const mjCModel* modelC = static_cast(s->element); return modelC->NextObject(element); } -mjsElement* mjs_getWrapTarget(mjsWrap* wrap) { - mjCWrap* cwrap = static_cast(wrap->element); +mjsElement* mjs_getWrapTarget(const mjsWrap* wrap) { + const mjCWrap* cwrap = static_cast(wrap->element); mjtObj type = mjOBJ_UNKNOWN; switch (cwrap->Type()) { case mjWRAP_SPHERE: @@ -1680,15 +1675,14 @@ mjsElement* mjs_getWrapTarget(mjsWrap* wrap) { default: return nullptr; } - mjSpec* spec = mjs_getSpec(wrap->element); - mjsElement* target = mjs_findElement(spec, type, cwrap->name.c_str()); - return target; + const mjSpec* spec = mjs_getSpec(wrap->element); + return mjs_findElement(spec, type, cwrap->name.c_str()); } -mjsSite* mjs_getWrapSideSite(mjsWrap* wrap) { - mjCWrap* cwrap = static_cast(wrap->element); +mjsSite* mjs_getWrapSideSite(const mjsWrap* wrap) { + const mjCWrap* cwrap = static_cast(wrap->element); // only sphere and cylinder (geoms) have side sites if ((cwrap->Type() != mjWRAP_SPHERE && cwrap->Type() != mjWRAP_CYLINDER) || @@ -1696,7 +1690,7 @@ mjsSite* mjs_getWrapSideSite(mjsWrap* wrap) { return nullptr; } - mjSpec* spec = mjs_getSpec(wrap->element); + const mjSpec* spec = mjs_getSpec(wrap->element); mjsElement* site = mjs_findElement(spec, mjOBJ_SITE, cwrap->sidesite.c_str()); if (site == nullptr) { mju_warning("Could not find side site %s for wrap %s in spec", @@ -1708,8 +1702,8 @@ mjsSite* mjs_getWrapSideSite(mjsWrap* wrap) { -double mjs_getWrapDivisor(mjsWrap* wrap) { - mjCWrap* cwrap = static_cast(wrap->element); +double mjs_getWrapDivisor(const mjsWrap* wrap) { + const mjCWrap* cwrap = static_cast(wrap->element); if (cwrap->Type() != mjWRAP_PULLEY) { mju_warning("Querying divisor attribute of non-pulley wrap: %s", cwrap->name.c_str()); return 1.0; @@ -1719,8 +1713,8 @@ double mjs_getWrapDivisor(mjsWrap* wrap) { -double mjs_getWrapCoef(mjsWrap* wrap) { - mjCWrap* cwrap = static_cast(wrap->element); +double mjs_getWrapCoef(const mjsWrap* wrap) { + const mjCWrap* cwrap = static_cast(wrap->element); if (cwrap->Type() != mjWRAP_JOINT) { mju_warning("Querying coef attribute of non-joint wrap: %s", cwrap->name.c_str()); return 1.0; diff --git a/src/user/user_api.h b/src/user/user_api.h index 2b73c12e..878f9045 100644 --- a/src/user/user_api.h +++ b/src/user/user_api.h @@ -222,68 +222,68 @@ MJAPI int mjs_makeMesh(mjsMesh* mesh, mjtMeshBuiltin builtin, double* params, in //---------------------------------- Find/get utilities -------------------------------------------- // Get spec from body. -MJAPI mjSpec* mjs_getSpec(mjsElement* element); +MJAPI mjSpec* mjs_getSpec(const mjsElement* element); // Find spec (model asset) by name. -MJAPI mjSpec* mjs_findSpec(mjSpec* spec, const char* name); +MJAPI mjSpec* mjs_findSpec(const mjSpec* spec, const char* name); // Find body in spec by name. -MJAPI mjsBody* mjs_findBody(mjSpec* s, const char* name); +MJAPI mjsBody* mjs_findBody(const mjSpec* s, const char* name); // Find element in spec by name. -MJAPI mjsElement* mjs_findElement(mjSpec* s, mjtObj type, const char* name); +MJAPI mjsElement* mjs_findElement(const mjSpec* s, mjtObj type, const char* name); // Find child body by name. -MJAPI mjsBody* mjs_findChild(mjsBody* body, const char* name); +MJAPI mjsBody* mjs_findChild(const mjsBody* body, const char* name); // Get parent body. -MJAPI mjsBody* mjs_getParent(mjsElement* element); +MJAPI mjsBody* mjs_getParent(const mjsElement* element); // Get parent frame. -MJAPI mjsFrame* mjs_getFrame(mjsElement* element); +MJAPI mjsFrame* mjs_getFrame(const mjsElement* element); // Find frame by name. -MJAPI mjsFrame* mjs_findFrame(mjSpec* s, const char* name); +MJAPI mjsFrame* mjs_findFrame(const mjSpec* s, const char* name); // Get default corresponding to an element. -MJAPI mjsDefault* mjs_getDefault(mjsElement* element); +MJAPI mjsDefault* mjs_getDefault(const mjsElement* element); // Find default in model by class name. -MJAPI mjsDefault* mjs_findDefault(mjSpec* s, const char* classname); +MJAPI mjsDefault* mjs_findDefault(const mjSpec* s, const char* classname); // Get global default from model. -MJAPI mjsDefault* mjs_getSpecDefault(mjSpec* s); +MJAPI mjsDefault* mjs_getSpecDefault(const mjSpec* s); // Get element id. -MJAPI int mjs_getId(mjsElement* element); +MJAPI int mjs_getId(const mjsElement* element); //---------------------------------- Tree traversal ------------------------------------------------ // Return body's first child of given type. If recurse is nonzero, also search the body's subtree. -MJAPI mjsElement* mjs_firstChild(mjsBody* body, mjtObj type, int recurse); +MJAPI mjsElement* mjs_firstChild(const mjsBody* body, mjtObj type, int recurse); // Return body's next child of the same type; return NULL if child is last. // If recurse is nonzero, also search the body's subtree. -MJAPI mjsElement* mjs_nextChild(mjsBody* body, mjsElement* child, int recurse); +MJAPI mjsElement* mjs_nextChild(const mjsBody* body, const mjsElement* child, int recurse); // Return spec's first element of selected type. -MJAPI mjsElement* mjs_firstElement(mjSpec* s, mjtObj type); +MJAPI mjsElement* mjs_firstElement(const mjSpec* s, mjtObj type); // Return spec's next element; return NULL if element is last. -MJAPI mjsElement* mjs_nextElement(mjSpec* s, mjsElement* element); +MJAPI mjsElement* mjs_nextElement(const mjSpec* s, const mjsElement* element); // Get wrapped element in tendon path. -MJAPI mjsElement* mjs_getWrapTarget(mjsWrap* wrap); +MJAPI mjsElement* mjs_getWrapTarget(const mjsWrap* wrap); // Get wrapped element in tendon path. -MJAPI mjsSite* mjs_getWrapSideSite(mjsWrap* wrap); +MJAPI mjsSite* mjs_getWrapSideSite(const mjsWrap* wrap); // Get divisor of mjsWrap wrapping a puller. -MJAPI double mjs_getWrapDivisor(mjsWrap* wrap); +MJAPI double mjs_getWrapDivisor(const mjsWrap* wrap); // Get coefficient of mjsWrap wrapping a joint. -MJAPI double mjs_getWrapCoef(mjsWrap* wrap); +MJAPI double mjs_getWrapCoef(const mjsWrap* wrap); // Safely cast an element as mjsBody, or return NULL if the element is not an mjsBody. MJAPI mjsBody* mjs_asBody(mjsElement* element); diff --git a/src/user/user_model.cc b/src/user/user_model.cc index e85cbe64..47d9a289 100644 --- a/src/user/user_model.cc +++ b/src/user/user_model.cc @@ -1408,7 +1408,7 @@ mjCBase* mjCModel::GetObject(mjtObj type, int id) { template -static mjsElement* GetNext(std::vector& list, mjsElement* child) { +static mjsElement* GetNext(const std::vector& list, const mjsElement* child) { if (!child) { if (list.empty()) { return nullptr; @@ -1428,7 +1428,7 @@ static mjsElement* GetNext(std::vector& list, mjsElement* child) { // next object of specified type -mjsElement* mjCModel::NextObject(mjsElement* object, mjtObj type) { +mjsElement* mjCModel::NextObject(const mjsElement* object, mjtObj type) const { if (type == mjOBJ_UNKNOWN) { if (!object) { throw mjCError(nullptr, "type must be specified if no element is given"); @@ -1519,7 +1519,7 @@ mjCBody* mjCModel::GetWorld() { // find default class name in array -mjCDef* mjCModel::FindDefault(string name) { +mjCDef* mjCModel::FindDefault(const string& name) const { for (int i=0; i < (int)defaults_.size(); i++) { if (defaults_[i]->name == name) { return defaults_[i]; @@ -1697,13 +1697,13 @@ mjSpec* mjCModel::FindSpec(std::string name) const { // find spec by mjsCompiler pointer -mjSpec* mjCModel::FindSpec(const mjsCompiler* compiler_) { +mjSpec* mjCModel::FindSpec(const mjsCompiler* compiler_) const { if (compiler_ == &spec.compiler) { - return &spec; + return &const_cast(this)->spec; } - if (compiler2spec_.find(compiler_) != compiler2spec_.end()) { - return compiler2spec_[compiler_]; + if (auto it = compiler2spec_.find(compiler_); it != compiler2spec_.end()) { + return it->second; } for (auto s : specs_) { diff --git a/src/user/user_model.h b/src/user/user_model.h index b22e0dcb..70cc39fc 100644 --- a/src/user/user_model.h +++ b/src/user/user_model.h @@ -242,19 +242,19 @@ class mjCModel : public mjCModel_, private mjSpec { // API for access to model elements (outside tree) int NumObjects(mjtObj type); // number of objects in specified list mjCBase* GetObject(mjtObj type, int id); // pointer to specified object - mjsElement* NextObject(mjsElement* object, mjtObj type = mjOBJ_UNKNOWN); // next object of specified type + mjsElement* NextObject(const mjsElement* object, mjtObj type = mjOBJ_UNKNOWN) const; // next object of specified type // API for access to other variables bool IsCompiled() const; // is model already compiled const mjCError& GetError() const; // get reference of error object void SetError(const mjCError& error) { errInfo = error; } // set value of error object mjCBody* GetWorld(); // pointer to world body - mjCDef* FindDefault(std::string name); // find defaults class name + mjCDef* FindDefault(const std::string& name) const; // find defaults class name mjCDef* AddDefault(std::string name, mjCDef* parent = nullptr); // add defaults class to array mjCBase* FindObject(mjtObj type, std::string name) const; // find object given type and name mjCBase* FindTree(mjCBody* body, mjtObj type, std::string name); // find tree object given name mjSpec* FindSpec(std::string name) const; // find spec given name - mjSpec* FindSpec(const mjsCompiler* compiler_); // find spec given mjsCompiler + mjSpec* FindSpec(const mjsCompiler* compiler_) const; // find spec given mjsCompiler void ActivatePlugin(const mjpPlugin* plugin, int slot); // activate plugin // find asset given name checking both name and filename diff --git a/src/user/user_objects.cc b/src/user/user_objects.cc index 75a7e679..d4f54ec6 100644 --- a/src/user/user_objects.cc +++ b/src/user/user_objects.cc @@ -2251,7 +2251,7 @@ mjCBase* mjCBody::GetObject(mjtObj type, int i) { // find object by name in given list template -static T* findobject(std::string name, std::vector& list) { +static T* findobject(const std::string& name, const std::vector& list) { for (unsigned int i=0; i < list.size(); i++) { if (list[i]->name == name) { return list[i]; @@ -2264,12 +2264,12 @@ static T* findobject(std::string name, std::vector& list) { // recursive find by name -mjCBase* mjCBody::FindObject(mjtObj type, std::string _name, bool recursive) { +mjCBase* mjCBody::FindObject(mjtObj type, const std::string& _name, bool recursive) const { mjCBase* res = 0; // check self: just in case if (name == _name) { - return this; + return const_cast(this); } // search elements of this body @@ -2406,7 +2406,7 @@ static mjsElement* GetNextBody(const mjCBody* body, const mjsElement* child, // get next child of given type -mjsElement* mjCBody::NextChild(const mjsElement* child, mjtObj type, bool recursive) { +mjsElement* mjCBody::NextChild(const mjsElement* child, mjtObj type, bool recursive) const { if (type == mjOBJ_UNKNOWN) { if (!child) { throw mjCError(this, "child type must be specified if no child element is given"); diff --git a/src/user/user_objects.h b/src/user/user_objects.h index c8e63938..c0d1be94 100644 --- a/src/user/user_objects.h +++ b/src/user/user_objects.h @@ -531,7 +531,7 @@ class mjCBody : public mjCBody_, private mjsBody { // API for accessing objects int NumObjects(mjtObj type); mjCBase* GetObject(mjtObj type, int id); - mjCBase* FindObject(mjtObj type, std::string name, bool recursive = true); + mjCBase* FindObject(mjtObj type, const std::string& name, bool recursive = true) const; // Propagate suffix and prefix to the whole tree void NameSpace(const mjCModel* m); @@ -556,7 +556,7 @@ class mjCBody : public mjCBody_, private mjsBody { // returns nullptr if the next child is not found or if `child` is the last element, returns // the next child after the input `child` otherwise mjsElement* NextChild(const mjsElement* child, mjtObj type = mjOBJ_UNKNOWN, - bool recursive = false); + bool recursive = false) const; // reset keyframe references for allowing self-attach void ForgetKeyframes() const; diff --git a/wasm/codegen/generated/bindings.cc b/wasm/codegen/generated/bindings.cc index eddd2024..bbd29e12 100644 --- a/wasm/codegen/generated/bindings.cc +++ b/wasm/codegen/generated/bindings.cc @@ -9687,7 +9687,7 @@ void mjs_deleteUserValue_wrapper(MjsElement& element, const String& key) { mjs_deleteUserValue(element.get(), key.as().data()); } -std::optional mjs_findBody_wrapper(MjSpec& s, const String& name) { +std::optional mjs_findBody_wrapper(const MjSpec& s, const String& name) { CHECK_VAL(name); mjsBody* result = mjs_findBody(s.get(), name.as().data()); if (result == nullptr) { @@ -9696,7 +9696,7 @@ std::optional mjs_findBody_wrapper(MjSpec& s, const String& name) { return MjsBody(result); } -std::optional mjs_findChild_wrapper(MjsBody& body, const String& name) { +std::optional mjs_findChild_wrapper(const MjsBody& body, const String& name) { CHECK_VAL(name); mjsBody* result = mjs_findChild(body.get(), name.as().data()); if (result == nullptr) { @@ -9705,7 +9705,7 @@ std::optional mjs_findChild_wrapper(MjsBody& body, const String& name) return MjsBody(result); } -std::optional mjs_findDefault_wrapper(MjSpec& s, const String& classname) { +std::optional mjs_findDefault_wrapper(const MjSpec& s, const String& classname) { CHECK_VAL(classname); mjsDefault* result = mjs_findDefault(s.get(), classname.as().data()); if (result == nullptr) { @@ -9714,7 +9714,7 @@ std::optional mjs_findDefault_wrapper(MjSpec& s, const String& class return MjsDefault(result); } -std::optional mjs_findElement_wrapper(MjSpec& s, mjtObj type, const String& name) { +std::optional mjs_findElement_wrapper(const MjSpec& s, mjtObj type, const String& name) { CHECK_VAL(name); mjsElement* result = mjs_findElement(s.get(), type, name.as().data()); if (result == nullptr) { @@ -9723,7 +9723,7 @@ std::optional mjs_findElement_wrapper(MjSpec& s, mjtObj type, const return MjsElement(result); } -std::optional mjs_findFrame_wrapper(MjSpec& s, const String& name) { +std::optional mjs_findFrame_wrapper(const MjSpec& s, const String& name) { CHECK_VAL(name); mjsFrame* result = mjs_findFrame(s.get(), name.as().data()); if (result == nullptr) { @@ -9732,7 +9732,7 @@ std::optional mjs_findFrame_wrapper(MjSpec& s, const String& name) { return MjsFrame(result); } -std::optional mjs_findSpec_wrapper(MjSpec& spec, const String& name) { +std::optional mjs_findSpec_wrapper(const MjSpec& spec, const String& name) { CHECK_VAL(name); mjSpec* result = mjs_findSpec(spec.get(), name.as().data()); if (result == nullptr) { @@ -9741,7 +9741,7 @@ std::optional mjs_findSpec_wrapper(MjSpec& spec, const String& name) { return MjSpec(result); } -std::optional mjs_firstChild_wrapper(MjsBody& body, mjtObj type, int recurse) { +std::optional mjs_firstChild_wrapper(const MjsBody& body, mjtObj type, int recurse) { mjsElement* result = mjs_firstChild(body.get(), type, recurse); if (result == nullptr) { return std::nullopt; @@ -9749,7 +9749,7 @@ std::optional mjs_firstChild_wrapper(MjsBody& body, mjtObj type, int return MjsElement(result); } -std::optional mjs_firstElement_wrapper(MjSpec& s, mjtObj type) { +std::optional mjs_firstElement_wrapper(const MjSpec& s, mjtObj type) { mjsElement* result = mjs_firstElement(s.get(), type); if (result == nullptr) { return std::nullopt; @@ -9757,7 +9757,7 @@ std::optional mjs_firstElement_wrapper(MjSpec& s, mjtObj type) { return MjsElement(result); } -std::optional mjs_getCompiler_wrapper(MjsElement& element) { +std::optional mjs_getCompiler_wrapper(const MjsElement& element) { mjsCompiler* result = mjs_getCompiler(element.get()); if (result == nullptr) { return std::nullopt; @@ -9765,7 +9765,7 @@ std::optional mjs_getCompiler_wrapper(MjsElement& element) { return MjsCompiler(result); } -std::optional mjs_getDefault_wrapper(MjsElement& element) { +std::optional mjs_getDefault_wrapper(const MjsElement& element) { mjsDefault* result = mjs_getDefault(element.get()); if (result == nullptr) { return std::nullopt; @@ -9777,7 +9777,7 @@ std::string mjs_getError_wrapper(MjSpec& s) { return std::string(mjs_getError(s.get())); } -std::optional mjs_getFrame_wrapper(MjsElement& element) { +std::optional mjs_getFrame_wrapper(const MjsElement& element) { mjsFrame* result = mjs_getFrame(element.get()); if (result == nullptr) { return std::nullopt; @@ -9785,7 +9785,7 @@ std::optional mjs_getFrame_wrapper(MjsElement& element) { return MjsFrame(result); } -int mjs_getId_wrapper(MjsElement& element) { +int mjs_getId_wrapper(const MjsElement& element) { return mjs_getId(element.get()); } @@ -9793,7 +9793,7 @@ std::string mjs_getName_wrapper(MjsElement& element) { return *mjs_getName(element.get()); } -std::optional mjs_getParent_wrapper(MjsElement& element) { +std::optional mjs_getParent_wrapper(const MjsElement& element) { mjsBody* result = mjs_getParent(element.get()); if (result == nullptr) { return std::nullopt; @@ -9801,7 +9801,7 @@ std::optional mjs_getParent_wrapper(MjsElement& element) { return MjsBody(result); } -std::optional mjs_getSpec_wrapper(MjsElement& element) { +std::optional mjs_getSpec_wrapper(const MjsElement& element) { mjSpec* result = mjs_getSpec(element.get()); if (result == nullptr) { return std::nullopt; @@ -9809,7 +9809,7 @@ std::optional mjs_getSpec_wrapper(MjsElement& element) { return MjSpec(result); } -std::optional mjs_getSpecDefault_wrapper(MjSpec& s) { +std::optional mjs_getSpecDefault_wrapper(const MjSpec& s) { mjsDefault* result = mjs_getSpecDefault(s.get()); if (result == nullptr) { return std::nullopt; @@ -9825,11 +9825,11 @@ std::optional mjs_getWrap_wrapper(const MjsTendon& tendonspec, int i) { return MjsWrap(result); } -double mjs_getWrapCoef_wrapper(MjsWrap& wrap) { +double mjs_getWrapCoef_wrapper(const MjsWrap& wrap) { return mjs_getWrapCoef(wrap.get()); } -double mjs_getWrapDivisor_wrapper(MjsWrap& wrap) { +double mjs_getWrapDivisor_wrapper(const MjsWrap& wrap) { return mjs_getWrapDivisor(wrap.get()); } @@ -9837,7 +9837,7 @@ int mjs_getWrapNum_wrapper(const MjsTendon& tendonspec) { return mjs_getWrapNum(tendonspec.get()); } -std::optional mjs_getWrapSideSite_wrapper(MjsWrap& wrap) { +std::optional mjs_getWrapSideSite_wrapper(const MjsWrap& wrap) { mjsSite* result = mjs_getWrapSideSite(wrap.get()); if (result == nullptr) { return std::nullopt; @@ -9845,7 +9845,7 @@ std::optional mjs_getWrapSideSite_wrapper(MjsWrap& wrap) { return MjsSite(result); } -std::optional mjs_getWrapTarget_wrapper(MjsWrap& wrap) { +std::optional mjs_getWrapTarget_wrapper(const MjsWrap& wrap) { mjsElement* result = mjs_getWrapTarget(wrap.get()); if (result == nullptr) { return std::nullopt; @@ -9862,7 +9862,7 @@ int mjs_makeMesh_wrapper(MjsMesh& mesh, mjtMeshBuiltin builtin, const val& param return mjs_makeMesh(mesh.get(), builtin, params_.data(), nparams); } -std::optional mjs_nextChild_wrapper(MjsBody& body, MjsElement& child, int recurse) { +std::optional mjs_nextChild_wrapper(const MjsBody& body, const MjsElement& child, int recurse) { mjsElement* result = mjs_nextChild(body.get(), child.get(), recurse); if (result == nullptr) { return std::nullopt; @@ -9870,7 +9870,7 @@ std::optional mjs_nextChild_wrapper(MjsBody& body, MjsElement& child return MjsElement(result); } -std::optional mjs_nextElement_wrapper(MjSpec& s, MjsElement& element) { +std::optional mjs_nextElement_wrapper(const MjSpec& s, const MjsElement& element) { mjsElement* result = mjs_nextElement(s.get(), element.get()); if (result == nullptr) { return std::nullopt;