From f864108c23faadb422dbbed2669d4b6c3eb1297a Mon Sep 17 00:00:00 2001 From: Yuval Tassa Date: Fri, 16 Aug 2024 06:13:51 -0700 Subject: [PATCH] `using` for `std::{string, string_view}`, where helpful for readibility. PiperOrigin-RevId: 663707900 Change-Id: Icbea374d5f99c7d267140d83f541f0ee872de8c1 --- src/xml/xml_native_reader.cc | 77 +++++++++++++------------- src/xml/xml_native_writer.cc | 102 +++++++++++++++++------------------ 2 files changed, 89 insertions(+), 90 deletions(-) diff --git a/src/xml/xml_native_reader.cc b/src/xml/xml_native_reader.cc index 5881ba4f..d9576e7d 100644 --- a/src/xml/xml_native_reader.cc +++ b/src/xml/xml_native_reader.cc @@ -48,20 +48,21 @@ namespace { using std::string; +using std::string_view; using std::vector; using mujoco::user::FilePath; using tinyxml2::XMLElement; void ReadPluginConfigs(tinyxml2::XMLElement* elem, mjsPlugin* p) { - std::map> config_attribs; + std::map> config_attribs; XMLElement* child = FirstChildElement(elem); while (child) { - std::string_view name = child->Value(); + string_view name = child->Value(); if (name == "config") { - std::string key, value; + string key, value; mjXUtil::ReadAttrTxt(child, "key", key, /* required = */ true); if (config_attribs.find(key) != config_attribs.end()) { - std::string err = "duplicate config key: " + key; + string err = "duplicate config key: " + key; throw mjXError(child, "%s", err.c_str()); } mjXUtil::ReadAttrTxt(child, "value", value, /* required = */ true); @@ -79,10 +80,10 @@ void ReadPluginConfigs(tinyxml2::XMLElement* elem, mjsPlugin* p) { } } -static void UpdateString(std::string& psuffix, int count, int i) { +static void UpdateString(string& psuffix, int count, int i) { int ndigits = std::to_string(count).length(); - std::string i_string = std::to_string(i); - std::string prefix = ""; + string i_string = std::to_string(i); + string prefix = ""; while (ndigits-- > i_string.length()) { prefix += '0'; } @@ -996,7 +997,7 @@ void mjXReader::Compiler(XMLElement* section, mjSpec* spec) { mjs_setString(spec->texturedir, text.c_str()); } // meshdir and texturedir take precedence over assetdir - std::string meshdir, texturedir; + string meshdir, texturedir; if (ReadAttrTxt(section, "meshdir", meshdir)) { mjs_setString(spec->meshdir, meshdir.c_str()); }; @@ -1157,11 +1158,11 @@ void mjXReader::Size(XMLElement* section, mjSpec* spec) { } // trim entire string - std::string trimmed; + string trimmed; { - std::istringstream strm((std::string(pstr))); + std::istringstream strm((string(pstr))); strm >> trimmed; - std::string trailing; + string trailing; strm >> trailing; if (!trailing.empty() || !strm.eof()) { throw mjXError(section, "%s", err_msg); @@ -1649,7 +1650,7 @@ void mjXReader::OneJoint(XMLElement* elem, mjsJoint* joint) { void mjXReader::OneGeom(XMLElement* elem, mjsGeom* geom) { string text, name; std::vector userdata; - std::string hfieldname, meshname, material; + string hfieldname, meshname, material; int n; // read attributes @@ -1722,7 +1723,7 @@ void mjXReader::OneSite(XMLElement* elem, mjsSite* site) { int n; string text, name; std::vector userdata; - std::string material; + string material; // read attributes if (ReadAttrTxt(elem, "name", name)) { @@ -2328,7 +2329,7 @@ void mjXReader::OneComposite(XMLElement* elem, mjsBody* body, mjsDefault* def) { } // cable - std::string curves; + string curves; ReadAttrTxt(elem, "curve", curves); ReadAttrTxt(elem, "initial", comp.initial); ReadAttr(elem, "size", 3, comp.size, text, false, false); @@ -2381,7 +2382,7 @@ void mjXReader::OneComposite(XMLElement* elem, mjsBody* body, mjsDefault* def) { // geom XMLElement* egeom = FirstChildElement(elem, "geom"); if (egeom) { - std::string material; + string material; mjsGeom& dgeom = *comp.def[0].spec.geom; if (MapValue(egeom, "type", &n, geom_map, mjNGEOMTYPES)) { dgeom.type = (mjtGeom)n; @@ -2409,7 +2410,7 @@ void mjXReader::OneComposite(XMLElement* elem, mjsBody* body, mjsDefault* def) { // site XMLElement* esite = FirstChildElement(elem, "site"); if (esite) { - std::string material; + string material; mjsSite& dsite = *comp.def[0].spec.site; ReadAttr(esite, "size", 3, dsite.size, text, false, false); ReadAttrInt(esite, "group", &dsite.group); @@ -2486,7 +2487,7 @@ void mjXReader::OneComposite(XMLElement* elem, mjsBody* body, mjsDefault* def) { ReadAttr(eten, "solimpfix", mjNIMP, dequality.solimp, text, false, false); // tendon attributes - std::string material; + string material; MapValue(elem, "limited", &dtendon.limited, TFAuto_map, 3); ReadAttrInt(eten, "group", &dtendon.group); ReadAttr(eten, "solreflimit", mjNREF, dtendon.solref_limit, text, false, false); @@ -2672,8 +2673,8 @@ void mjXReader::OneFlexcomp(XMLElement* elem, mjsBody* body, const mjVFS* vfs) { // add plugin void mjXReader::OnePlugin(XMLElement* elem, mjsPlugin* plugin) { plugin->active = true; - std::string name = ""; - std::string instance_name = ""; + string name = ""; + string instance_name = ""; ReadAttrTxt(elem, "plugin", name); ReadAttrTxt(elem, "instance", instance_name); mjs_setString(plugin->name, name.c_str()); @@ -2793,10 +2794,10 @@ void mjXReader::Extension(XMLElement* section) { while (elem) { // get sub-element name - std::string_view name = elem->Value(); + string_view name = elem->Value(); if (name == "plugin") { - std::string plugin_name; + string plugin_name; int plugin_slot = -1; ReadAttrTxt(elem, "plugin", plugin_name, /* required = */ true); const mjpPlugin* plugin = mjp_getPlugin(plugin_name.c_str(), &plugin_slot); @@ -2817,7 +2818,7 @@ void mjXReader::Extension(XMLElement* section) { XMLElement* child = FirstChildElement(elem); while (child) { - if (std::string(child->Value())=="instance") { + if (string(child->Value())=="instance") { if (spec->hasImplicitPluginElem) { throw mjXError( child, "explicit plugin instance must appear before implicit plugin elements"); @@ -2925,7 +2926,7 @@ void mjXReader::Custom(XMLElement* section) { // read objects and add XMLElement* obj = FirstChildElement(elem); std::vector objtype; - std::string objname = ""; + string objname = ""; std::vector objprm; while (obj) { @@ -3178,8 +3179,8 @@ void mjXReader::Asset(XMLElement* section, const mjVFS* vfs) { } // separate files - std::vector cubefiles(6); - std::vector cubefile_names = {"fileright", "fileleft", + std::vector cubefiles(6); + std::vector cubefile_names = {"fileright", "fileleft", "fileup", "filedown", "filefront", "fileback"}; for (int i = 0; i < cubefiles.size(); i++) { @@ -3285,7 +3286,7 @@ void mjXReader::Asset(XMLElement* section, const mjVFS* vfs) { } // overwrite model name if given - std::string modelname = ""; + string modelname = ""; if (ReadAttrTxt(elem, "name", modelname)) { mjs_setString(child->modelname, modelname.c_str()); } @@ -3376,7 +3377,7 @@ void mjXReader::Body(XMLElement* section, mjsBody* body, mjsFrame* frame, mjs_setDefault(joint->element, def); // read attributes - std::string name; + string name; if (ReadAttrTxt(elem, "name", name)) { mjs_setString(joint->name, name.c_str()); } @@ -3450,7 +3451,7 @@ void mjXReader::Body(XMLElement* section, mjsBody* body, mjsFrame* frame, mjs_setDefault(pframe->element, childdef ? childdef : def); // read attributes - std::string name, childclass; + string name, childclass; if (ReadAttrTxt(elem, "name", name)) { mjs_setString(pframe->name, name.c_str()); } @@ -3469,7 +3470,7 @@ void mjXReader::Body(XMLElement* section, mjsBody* body, mjsFrame* frame, int count; double offset[3] = {0, 0, 0}; double euler[3] = {0, 0, 0}; - std::string separator = ""; + string separator = ""; ReadAttr(elem, "count", 1, &count, text, true); ReadAttr(elem, "offset", 3, offset, text); ReadAttr(elem, "euler", 3, euler, text); @@ -3520,7 +3521,7 @@ void mjXReader::Body(XMLElement* section, mjsBody* body, mjsFrame* frame, mjuu_setvec(pframe->quat, quat[0], quat[1], quat[2], quat[3]); // process suffix - std::string suffix = separator; + string suffix = separator; UpdateString(suffix, count, i); // attach to parent @@ -3547,13 +3548,13 @@ void mjXReader::Body(XMLElement* section, mjsBody* body, mjsFrame* frame, // create child body mjsBody* child = mjs_addBody(body, childdef); - mjs_setString(child->info, std::string("line " + std::to_string(elem->GetLineNum())).c_str()); + mjs_setString(child->info, string("line " + std::to_string(elem->GetLineNum())).c_str()); // set default from class or childclass mjs_setDefault(child->element, childdef ? childdef : def); // read attributes - std::string name, childclass; + string name, childclass; if (ReadAttrTxt(elem, "name", name)) { mjs_setString(child->name, name.c_str()); } @@ -3584,7 +3585,7 @@ void mjXReader::Body(XMLElement* section, mjsBody* body, mjsFrame* frame, // attachment else if (name=="attach") { - std::string model_name, body_name, prefix; + string model_name, body_name, prefix; ReadAttrTxt(elem, "model", model_name); ReadAttrTxt(elem, "body", body_name); ReadAttrTxt(elem, "prefix", prefix); @@ -4232,26 +4233,26 @@ mjsDefault* mjXReader::GetClass(XMLElement* section) { if (!def) { throw mjXError( section, - std::string("unknown default class name '" + text + "'").c_str()); + string("unknown default class name '" + text + "'").c_str()); } } return def; } -void mjXReader::SetModelFileDir(const std::string& modelfiledir) { +void mjXReader::SetModelFileDir(const string& modelfiledir) { modelfiledir_ = FilePath(modelfiledir); } -void mjXReader::SetAssetDir(const std::string& assetdir) { +void mjXReader::SetAssetDir(const string& assetdir) { assetdir_ = FilePath(assetdir); } -void mjXReader::SetMeshDir(const std::string& meshdir) { +void mjXReader::SetMeshDir(const string& meshdir) { meshdir_ = FilePath(meshdir); } -void mjXReader::SetTextureDir(const std::string& texturedir) { +void mjXReader::SetTextureDir(const string& texturedir) { texturedir_ = FilePath(texturedir); } diff --git a/src/xml/xml_native_writer.cc b/src/xml/xml_native_writer.cc index 993d1a52..8d48cbce 100644 --- a/src/xml/xml_native_writer.cc +++ b/src/xml/xml_native_writer.cc @@ -39,6 +39,8 @@ namespace { +using std::string; +using std::string_view; using tinyxml2::XMLComment; using tinyxml2::XMLDocument; using tinyxml2::XMLElement; @@ -62,7 +64,7 @@ class mj_XMLPrinter : public tinyxml2::XMLPrinter { // save XML file using custom 2-space indentation -static std::string WriteDoc(XMLDocument& doc, char *error, size_t error_sz) { +static string WriteDoc(XMLDocument& doc, char *error, size_t error_sz) { doc.ClearError(); mj_XMLPrinter stream(nullptr, /*compact=*/false); doc.Print(&stream); @@ -70,22 +72,22 @@ static std::string WriteDoc(XMLDocument& doc, char *error, size_t error_sz) { mjCopyError(error, doc.ErrorStr(), error_sz); return ""; } - std::string str = std::string(stream.CStr()); + string str = string(stream.CStr()); // top level sections - std::array sections = { + std::array sections = { "", "name); @@ -803,8 +803,8 @@ void mjXWriter::OneActuator(XMLElement* elem, const mjCActuator* actuator, mjCDe // write plugin void mjXWriter::OnePlugin(XMLElement* elem, const mjsPlugin* plugin) { - const std::string instance_name = std::string(mjs_getString(plugin->instance_name)); - const std::string plugin_name = std::string(mjs_getString(plugin->name)); + const string instance_name = string(mjs_getString(plugin->instance_name)); + const string plugin_name = string(mjs_getString(plugin->name)); if (!instance_name.empty()) { WriteAttrTxt(elem, "instance", instance_name); } else { @@ -813,7 +813,7 @@ void mjXWriter::OnePlugin(XMLElement* elem, const mjsPlugin* plugin) { 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); + string value(c); if (!value.empty()) { XMLElement* config_elem = InsertEnd(elem, "config"); WriteAttrTxt(config_elem, "key", pplugin->attributes[i]); @@ -844,7 +844,7 @@ void mjXWriter::SetModel(const mjSpec* spec) { // save existing model in MJCF canonical format, must be compiled -std::string mjXWriter::Write(char *error, size_t error_sz) { +string mjXWriter::Write(char *error, size_t error_sz) { // check model if (!model || !model->IsCompiled()) { mjCopyError(error, "XML Write error: Only compiled model can be written", error_sz); @@ -860,7 +860,7 @@ std::string mjXWriter::Write(char *error, size_t error_sz) { doc.InsertFirstChild(root); // write comment if present - std::string text = mjs_getString(model->comment); + string text = mjs_getString(model->comment); if (!text.empty()) { XMLComment* comment = doc.NewComment(text.c_str()); root->LinkEndChild(comment); @@ -1332,7 +1332,7 @@ void mjXWriter::Extension(XMLElement* root) { // write plugin config attributes const char* c = &pp->flattened_attributes[0]; for (int i = 0; i < plugin->nattribute; ++i) { - std::string value(c); + string value(c); if (!value.empty()) { XMLElement* config_elem = InsertEnd(elem, "config"); WriteAttrTxt(config_elem, "key", plugin->attributes[i]); @@ -1525,7 +1525,7 @@ void mjXWriter::Asset(XMLElement* root) { WriteAttrInt(elem, "nrow", hfield->nrow); WriteAttrInt(elem, "ncol", hfield->ncol); if (!hfield->get_userdata().empty()) { - std::string text; + string text; Vector2String(text, hfield->get_userdata(), hfield->ncol); WriteAttrTxt(elem, "elevation", text); } @@ -1555,8 +1555,7 @@ XMLElement* mjXWriter::OneFrame(XMLElement* elem, mjCFrame* frame) { // recursive body and frame writer -void mjXWriter::Body(XMLElement* elem, mjCBody* body, mjCFrame* frame, - std::string_view childclass) { +void mjXWriter::Body(XMLElement* elem, mjCBody* body, mjCFrame* frame, string_view childclass) { double unitq[4] = {1, 0, 0, 0}; if (!body) { @@ -1587,8 +1586,7 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body, mjCFrame* frame, WriteVector(elem, "user", body->get_userdata()); // write inertial - if (body->explicitinertial && - model->inertiafromgeom!=mjINERTIAFROMGEOM_TRUE) { + if (body->explicitinertial && model->inertiafromgeom!=mjINERTIAFROMGEOM_TRUE) { XMLElement* inertial = InsertEnd(elem, "inertial"); WriteAttr(inertial, "pos", 3, body->ipos); WriteAttr(inertial, "quat", 4, body->iquat, unitq); @@ -1602,9 +1600,9 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body, mjCFrame* frame, if (body->joints[i]->frame != frame) { continue; } - std::string classname = body->joints[i]->frame && !body->joints[i]->frame->classname.empty() - ? body->joints[i]->frame->classname - : body->classname; + string classname = body->joints[i]->frame && !body->joints[i]->frame->classname.empty() + ? body->joints[i]->frame->classname + : body->classname; OneJoint(InsertEnd(elem, "joint"), body->joints[i], model->def_map[body->joints[i]->classname], classname.empty() ? childclass : classname); @@ -1615,9 +1613,9 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body, mjCFrame* frame, if (body->geoms[i]->frame != frame) { continue; } - std::string classname = body->geoms[i]->frame && !body->geoms[i]->frame->classname.empty() - ? body->geoms[i]->frame->classname - : body->classname; + string classname = body->geoms[i]->frame && !body->geoms[i]->frame->classname.empty() + ? body->geoms[i]->frame->classname + : body->classname; OneGeom(InsertEnd(elem, "geom"), body->geoms[i], model->def_map[body->geoms[i]->classname], classname.empty() ? childclass : classname); @@ -1628,9 +1626,9 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body, mjCFrame* frame, if (body->sites[i]->frame != frame) { continue; } - std::string classname = body->sites[i]->frame && !body->sites[i]->frame->classname.empty() - ? body->sites[i]->frame->classname - : body->classname; + string classname = body->sites[i]->frame && !body->sites[i]->frame->classname.empty() + ? body->sites[i]->frame->classname + : body->classname; OneSite(InsertEnd(elem, "site"), body->sites[i], model->def_map[body->sites[i]->classname], classname.empty() ? childclass : classname); @@ -1641,9 +1639,9 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body, mjCFrame* frame, if (body->cameras[i]->frame != frame) { continue; } - std::string classname = body->cameras[i]->frame && !body->cameras[i]->frame->classname.empty() - ? body->cameras[i]->frame->classname - : body->classname; + string classname = body->cameras[i]->frame && !body->cameras[i]->frame->classname.empty() + ? body->cameras[i]->frame->classname + : body->classname; OneCamera(InsertEnd(elem, "camera"), body->cameras[i], model->def_map[body->cameras[i]->classname], classname.empty() ? childclass : classname); @@ -1654,9 +1652,9 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body, mjCFrame* frame, if (body->lights[i]->frame != frame) { continue; } - std::string classname = body->lights[i]->frame && !body->lights[i]->frame->classname.empty() - ? body->lights[i]->frame->classname - : body->classname; + string classname = body->lights[i]->frame && !body->lights[i]->frame->classname.empty() + ? body->lights[i]->frame->classname + : body->classname; OneLight(InsertEnd(elem, "light"), body->lights[i], model->def_map[body->lights[i]->classname], classname.empty() ? childclass : classname); @@ -1674,9 +1672,9 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body, mjCFrame* frame, // write body if its frame matches the current frame, avoid access if there are no bodies if (bframe == frame && !body->bodies.empty()) { - std::string classname = bframe && !bframe->classname.empty() - ? bframe->classname - : body->classname; + string classname = bframe && !bframe->classname.empty() + ? bframe->classname + : body->classname; Body(InsertEnd(elem, "body"), body->bodies[i], nullptr, classname.empty() ? childclass : classname); } @@ -1694,9 +1692,9 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body, mjCFrame* frame, // write frame if its frame matches the current frame if (fframe->frame == frame) { - std::string classname = fframe && !fframe->classname.empty() - ? fframe->classname - : body->classname; + string classname = fframe && !fframe->classname.empty() + ? fframe->classname + : body->classname; Body(OneFrame(elem, fframe), body, fframe, childclass); } @@ -1914,8 +1912,8 @@ void mjXWriter::Sensor(XMLElement* root) { for (int i=0; iSensors()[i]; - std::string instance_name = ""; - std::string plugin_name = ""; + string instance_name = ""; + string plugin_name = ""; // write sensor type and type-specific attributes switch (sensor->type) {