using for std::{string, string_view}, where helpful for readibility.

PiperOrigin-RevId: 663707900
Change-Id: Icbea374d5f99c7d267140d83f541f0ee872de8c1
This commit is contained in:
Yuval Tassa
2024-08-16 06:13:51 -07:00
committed by Copybara-Service
parent e0e08aa597
commit f864108c23
2 changed files with 89 additions and 90 deletions
+39 -38
View File
@@ -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<std::string, std::string, std::less<>> config_attribs;
std::map<string, string, std::less<>> 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<double> 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<double> 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<int> objtype;
std::string objname = "";
string objname = "";
std::vector<double> objprm;
while (obj) {
@@ -3178,8 +3179,8 @@ void mjXReader::Asset(XMLElement* section, const mjVFS* vfs) {
}
// separate files
std::vector<std::string> cubefiles(6);
std::vector<std::string> cubefile_names = {"fileright", "fileleft",
std::vector<string> cubefiles(6);
std::vector<string> 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);
}
+50 -52
View File
@@ -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<std::string, 17> sections = {
std::array<string, 17> sections = {
"<actuator", "<asset", "<compiler", "<contact", "<custom",
"<default>", "<deformable", "<equality", "<extension", "<keyframe",
"<option", "<sensor", "<size", "<statistic", "<tendon",
"<visual", "<worldbody"};
// position of newline before first section
size_t first_pos = std::string::npos;
size_t first_pos = string::npos;
// insert newlines before section headers
for (const std::string& section : sections) {
for (const string& section : sections) {
std::size_t pos = 0;
while ((pos = str.find(section, pos)) != std::string::npos) {
while ((pos = str.find(section, pos)) != string::npos) {
// find newline before this section
std::size_t line_pos = str.rfind('\n', pos);
@@ -93,7 +95,7 @@ static std::string WriteDoc(XMLDocument& doc, char *error, size_t error_sz) {
if (line_pos < first_pos) first_pos = line_pos;
// insert another newline
if (line_pos != std::string::npos) {
if (line_pos != string::npos) {
str.insert(line_pos + 1, "\n");
pos++; // account for inserted newline
}
@@ -104,7 +106,7 @@ static std::string WriteDoc(XMLDocument& doc, char *error, size_t error_sz) {
}
// remove added newline before the first section
if (first_pos != std::string::npos) {
if (first_pos != string::npos) {
str.erase(first_pos, 1);
}
@@ -125,7 +127,7 @@ XMLElement* mjXWriter::InsertEnd(XMLElement* parent, const char* name) {
// write flex
void mjXWriter::OneFlex(XMLElement* elem, const mjCFlex* flex) {
std::string text;
string text;
mjCFlex defflex;
// common attributes
@@ -193,7 +195,7 @@ void mjXWriter::OneFlex(XMLElement* elem, const mjCFlex* flex) {
// write mesh
void mjXWriter::OneMesh(XMLElement* elem, const mjCMesh* mesh, mjCDef* def) {
std::string text;
string text;
// regular
if (!writingdefaults) {
@@ -241,7 +243,7 @@ void mjXWriter::OneMesh(XMLElement* elem, const mjCMesh* mesh, mjCDef* def) {
// write skin
void mjXWriter::OneSkin(XMLElement* elem, const mjCSkin* skin) {
std::string text;
string text;
mjCDef mydef;
float zero = 0;
@@ -333,7 +335,7 @@ void mjXWriter::OneMaterial(XMLElement* elem, const mjCMaterial* material, mjCDe
// write joint
void mjXWriter::OneJoint(XMLElement* elem, const mjCJoint* joint, mjCDef* def,
std::string_view classname) {
string_view classname) {
double zero = 0;
// regular
@@ -384,8 +386,7 @@ void mjXWriter::OneJoint(XMLElement* elem, const mjCJoint* joint, mjCDef* def,
}
// write geom
void mjXWriter::OneGeom(XMLElement* elem, const mjCGeom* geom, mjCDef* def,
std::string_view classname) {
void mjXWriter::OneGeom(XMLElement* elem, const mjCGeom* geom, mjCDef* def, string_view classname) {
double unitq[4] = {1, 0, 0, 0};
double mass = 0;
@@ -480,8 +481,7 @@ void mjXWriter::OneGeom(XMLElement* elem, const mjCGeom* geom, mjCDef* def,
}
// write site
void mjXWriter::OneSite(XMLElement* elem, const mjCSite* site, mjCDef* def,
std::string_view classname) {
void mjXWriter::OneSite(XMLElement* elem, const mjCSite* site, mjCDef* def, string_view classname) {
double unitq[4] = {1, 0, 0, 0};
// regular
@@ -517,7 +517,7 @@ void mjXWriter::OneSite(XMLElement* elem, const mjCSite* site, mjCDef* def,
// write camera
void mjXWriter::OneCamera(XMLElement* elem, const mjCCamera* camera, mjCDef* def,
std::string_view classname) {
string_view classname) {
double unitq[4] = {1, 0, 0, 0};
// regular
@@ -558,7 +558,7 @@ void mjXWriter::OneCamera(XMLElement* elem, const mjCCamera* camera, mjCDef* def
// write light
void mjXWriter::OneLight(XMLElement* elem, const mjCLight* light, mjCDef* def,
std::string_view classname) {
string_view classname) {
// regular
if (!writingdefaults) {
WriteAttrTxt(elem, "name", light->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<mjCPlugin*>(plugin->instance)->spec.plugin_slot);
const char* c = &(static_cast<mjCPlugin*>(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; i<num; i++) {
XMLElement* elem = 0;
mjCSensor* sensor = model->Sensors()[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) {