From 792c5f4d4131ffd993378a588364683a371943dd Mon Sep 17 00:00:00 2001 From: Aditya Agrawal Date: Sun, 19 May 2024 22:55:58 +0800 Subject: [PATCH] string to string_view, refactoring --- format_schema.py | 83 ++++++++++++++++++++++++++++++++++++++++++ src/Actor.cc | 2 +- src/AirPressure.cc | 2 +- src/AirSpeed.cc | 2 +- src/Altimeter.cc | 2 +- src/Atmosphere.cc | 2 +- src/Camera.cc | 2 +- src/Collision.cc | 2 +- src/ForceTorque.cc | 2 +- src/Frame.cc | 2 +- src/Geometry.cc | 2 +- src/Gui.cc | 2 +- src/Imu.cc | 2 +- src/Joint.cc | 2 +- src/Lidar.cc | 2 +- src/Light.cc | 2 +- src/Link.cc | 2 +- src/Magnetometer.cc | 2 +- src/Material.cc | 2 +- src/Model.cc | 2 +- src/Noise.cc | 2 +- src/ParticleEmitter.cc | 2 +- src/Physics.cc | 2 +- src/Plugin.cc | 2 +- src/Projector.cc | 2 +- src/Root.cc | 2 +- src/Scene.cc | 2 +- src/Sensor.cc | 2 +- src/Surface.cc | 2 +- src/Visual.cc | 2 +- src/World.cc | 2 +- tools/format_schema.py | 67 ---------------------------------- 32 files changed, 113 insertions(+), 97 deletions(-) create mode 100644 format_schema.py delete mode 100644 tools/format_schema.py diff --git a/format_schema.py b/format_schema.py new file mode 100644 index 000000000..b7ae61978 --- /dev/null +++ b/format_schema.py @@ -0,0 +1,83 @@ +import os + +lines = os.listdir('./sdf/1.11/') +# Only keep the files that end with .sdf +lines = [line for line in lines if line.endswith('.sdf')] +for line in lines: + # Remove .sdf extension + snake_case = line.strip().split('.')[0] + + # Convert from snake_case to CamelCase + camel_case = ''.join([word.capitalize() for word in snake_case.split('_')]) + + addHeader = """ + /// \\brief Get the schema file name accessor + public: static inline std::string_view SchemaFile(); + """ + + addImpl = f""" +///////////////////////////////////////////////// +inline std::string_view {camel_case}::SchemaFile() +{{ + static const char kSchemaFile[] = "{line}"; + return kSchemaFile; +}}\n\n""" + + # Debug print statements + # print(addHeader) + # print(addImpl) + # print(addTest) + # print("\n") + + print(line) + + # # Edit './include/sdf/{camel_case}.hh' + # # Find the line with 'class SDFFORMAT_VISIBLE {camel_case}` + # try: + # f = open('./include/sdf/' + camel_case + '.hh', 'r') + # read_lines = f.readlines() + # f.close() + # line_number = 0 + # for i, line_raw in enumerate(read_lines): + # if line_raw == " class SDFORMAT_VISIBLE " + camel_case + '\n': + # line_number = i+3 # After the class line there is `\n{\n` and then the constructor + # break + # if line_number == 0: + # print("Error: Could not find class declaration in " + camel_case + ".hh") + # exit(1) + # try: + # with open('./include/sdf/' + camel_case + '.hh', 'w') as file: + # for i, line_raw in enumerate(read_lines): + # file.write(line_raw) + # if i == line_number: + # file.write(addHeader) + # except: + # print("Unexpected error while writing to: " + camel_case + ".hh.") + # except: + # print("Error while writing to: " + camel_case + ".hh." + " Check if file exists.") + + # Replace all instances of `line` with `std::string(this->SchemaFile())` + if (os.path.exists('./src/' + camel_case + '.cc')): + try: + f = open('./src/' + camel_case + '.cc', 'r') + read_lines = f.readlines() + f.close() + + with open('./src/' + camel_case + '.cc', 'w') as file: + for i, line_raw in enumerate(read_lines): + if "kSchemaFile[]" not in line_raw: + file.write(line_raw.replace(f'"{line}"', 'std::string(this->SchemaFile())')) + else: + file.write(line_raw) + except: + print("Unexpected error while reading from: " + camel_case + ".cc.") + + # # Edit './src/{camel_case}.cc' if it exists + # # Add implementation to end of document + # if os.path.exists('./src/' + camel_case + '.cc'): + # with open('./src/' + camel_case + '.cc', 'a') as file: + # file.write(addImpl) + # else: + # print("Error: Could not find " + camel_case + ".cc") + + print("Changes written to source successfully") diff --git a/src/Actor.cc b/src/Actor.cc index 595ef1f8b..0ca834536 100644 --- a/src/Actor.cc +++ b/src/Actor.cc @@ -726,7 +726,7 @@ void Actor::ClearJoints() sdf::ElementPtr Actor::ToElement() const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("actor.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("name")->Set(this->Name()); // Set pose diff --git a/src/AirPressure.cc b/src/AirPressure.cc index fdbdc5b35..1f576ed59 100644 --- a/src/AirPressure.cc +++ b/src/AirPressure.cc @@ -129,7 +129,7 @@ sdf::ElementPtr AirPressure::ToElement() const sdf::ElementPtr AirPressure::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("air_pressure.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetElement("reference_altitude", _errors)->Set( _errors, this->ReferenceAltitude()); diff --git a/src/AirSpeed.cc b/src/AirSpeed.cc index 0045f6ac4..fd0dec211 100644 --- a/src/AirSpeed.cc +++ b/src/AirSpeed.cc @@ -98,7 +98,7 @@ void AirSpeed::SetPressureNoise(const Noise &_noise) sdf::ElementPtr AirSpeed::ToElement() const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("air_speed.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); sdf::ElementPtr pressureElem = elem->GetElement("pressure"); sdf::ElementPtr noiseElem = pressureElem->GetElement("noise"); diff --git a/src/Altimeter.cc b/src/Altimeter.cc index 638ebcc88..f7556c485 100644 --- a/src/Altimeter.cc +++ b/src/Altimeter.cc @@ -151,7 +151,7 @@ sdf::ElementPtr Altimeter::ToElement() const sdf::ElementPtr Altimeter::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("altimeter.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); sdf::ElementPtr verticalPosElem = elem->GetElement( "vertical_position", _errors); diff --git a/src/Atmosphere.cc b/src/Atmosphere.cc index 9ff3e14b4..1b4459430 100644 --- a/src/Atmosphere.cc +++ b/src/Atmosphere.cc @@ -161,7 +161,7 @@ sdf::ElementPtr Atmosphere::ToElement() const sdf::ElementPtr Atmosphere::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("atmosphere.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("type")->Set("adiabatic", _errors); elem->GetElement("temperature", _errors)->Set( diff --git a/src/Camera.cc b/src/Camera.cc index 78ed66de7..fceb6f1f2 100644 --- a/src/Camera.cc +++ b/src/Camera.cc @@ -1213,7 +1213,7 @@ bool Camera::HasLensProjection() const sdf::ElementPtr Camera::ToElement() const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("camera.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("name")->Set(this->Name()); sdf::ElementPtr poseElem = elem->GetElement("pose"); diff --git a/src/Collision.cc b/src/Collision.cc index e78481e60..5a3fde531 100644 --- a/src/Collision.cc +++ b/src/Collision.cc @@ -364,7 +364,7 @@ sdf::ElementPtr Collision::ToElement() const sdf::ElementPtr Collision::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("collision.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("name")->Set(this->Name(), _errors); diff --git a/src/ForceTorque.cc b/src/ForceTorque.cc index e48e0c39b..e55f56c06 100644 --- a/src/ForceTorque.cc +++ b/src/ForceTorque.cc @@ -296,7 +296,7 @@ sdf::ElementPtr ForceTorque::ToElement() const sdf::ElementPtr ForceTorque::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("forcetorque.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); std::string frame; switch (this->Frame()) diff --git a/src/Frame.cc b/src/Frame.cc index a12e1f7bc..bf5f4d243 100644 --- a/src/Frame.cc +++ b/src/Frame.cc @@ -232,7 +232,7 @@ sdf::ElementPtr Frame::ToElement() const sdf::ElementPtr Frame::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("frame.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("name")->Set(this->dataPtr->name, _errors); diff --git a/src/Geometry.cc b/src/Geometry.cc index d4f73508b..75bcca03f 100644 --- a/src/Geometry.cc +++ b/src/Geometry.cc @@ -377,7 +377,7 @@ sdf::ElementPtr Geometry::ToElement() const sdf::ElementPtr Geometry::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("geometry.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); switch (this->dataPtr->type) { diff --git a/src/Gui.cc b/src/Gui.cc index f48b7dd8c..6f61d738e 100644 --- a/src/Gui.cc +++ b/src/Gui.cc @@ -97,7 +97,7 @@ sdf::ElementPtr Gui::Element() const sdf::ElementPtr Gui::ToElement() const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("gui.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("fullscreen")->Set(this->dataPtr->fullscreen); diff --git a/src/Imu.cc b/src/Imu.cc index 644ea095c..f239bcf1a 100644 --- a/src/Imu.cc +++ b/src/Imu.cc @@ -389,7 +389,7 @@ sdf::ElementPtr Imu::ToElement() const sdf::ElementPtr Imu::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("imu.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); sdf::ElementPtr orientationRefFrameElem = elem->GetElement("orientation_reference_frame", _errors); diff --git a/src/Joint.cc b/src/Joint.cc index ae4a79398..4532b5b41 100644 --- a/src/Joint.cc +++ b/src/Joint.cc @@ -538,7 +538,7 @@ Sensor *Joint::SensorByName(const std::string &_name) sdf::ElementPtr Joint::ToElement() const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("joint.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("name")->Set(this->Name()); sdf::ElementPtr poseElem = elem->GetElement("pose"); diff --git a/src/Lidar.cc b/src/Lidar.cc index 314501a2b..98b5caffe 100644 --- a/src/Lidar.cc +++ b/src/Lidar.cc @@ -395,7 +395,7 @@ bool Lidar::operator!=(const Lidar &_lidar) const sdf::ElementPtr Lidar::ToElement() const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("lidar.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); sdf::ElementPtr scanElem = elem->GetElement("scan"); sdf::ElementPtr horElem = scanElem->GetElement("horizontal"); diff --git a/src/Light.cc b/src/Light.cc index fde792633..ca44f3db1 100644 --- a/src/Light.cc +++ b/src/Light.cc @@ -500,7 +500,7 @@ sdf::ElementPtr Light::ToElement() const sdf::ElementPtr Light::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("light.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); std::string lightTypeStr = "point"; switch (this->Type()) diff --git a/src/Link.cc b/src/Link.cc index 2d88af046..cc6522966 100644 --- a/src/Link.cc +++ b/src/Link.cc @@ -959,7 +959,7 @@ void Link::ClearProjectors() sdf::ElementPtr Link::ToElement() const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("link.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("name")->Set(this->Name()); diff --git a/src/Magnetometer.cc b/src/Magnetometer.cc index 452d6acd1..580b41129 100644 --- a/src/Magnetometer.cc +++ b/src/Magnetometer.cc @@ -136,7 +136,7 @@ bool Magnetometer::operator==(const Magnetometer &_mag) const sdf::ElementPtr Magnetometer::ToElement() const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("magnetometer.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); sdf::ElementPtr magnetometerXElem = elem->GetElement("x"); sdf::ElementPtr magnetometerXNoiseElem = diff --git a/src/Material.cc b/src/Material.cc index 30939e0b5..0d908aa1e 100644 --- a/src/Material.cc +++ b/src/Material.cc @@ -411,7 +411,7 @@ sdf::ElementPtr Material::ToElement() const sdf::ElementPtr Material::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("material.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetElement("ambient", _errors)->Set(_errors, this->Ambient()); elem->GetElement("diffuse", _errors)->Set(_errors, this->Diffuse()); diff --git a/src/Model.cc b/src/Model.cc index 2d846b56b..0cb50476a 100644 --- a/src/Model.cc +++ b/src/Model.cc @@ -1115,7 +1115,7 @@ sdf::ElementPtr Model::ToElement(const OutputConfig &_config) const } sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("model.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("name")->Set(this->Name()); if (!this->dataPtr->canonicalLink.empty()) diff --git a/src/Noise.cc b/src/Noise.cc index a971b672c..4f6c8dc11 100644 --- a/src/Noise.cc +++ b/src/Noise.cc @@ -265,7 +265,7 @@ sdf::ElementPtr Noise::ToElement() const sdf::ElementPtr Noise::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("noise.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); std::string noiseType; switch (this->Type()) diff --git a/src/ParticleEmitter.cc b/src/ParticleEmitter.cc index dfc36aea8..86bb64ab7 100644 --- a/src/ParticleEmitter.cc +++ b/src/ParticleEmitter.cc @@ -533,7 +533,7 @@ sdf::ElementPtr ParticleEmitter::ToElement() const sdf::ElementPtr ParticleEmitter::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("particle_emitter.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); // Set pose sdf::ElementPtr poseElem = elem->GetElement("pose", _errors); diff --git a/src/Physics.cc b/src/Physics.cc index 1af423242..c1ca03488 100644 --- a/src/Physics.cc +++ b/src/Physics.cc @@ -215,7 +215,7 @@ sdf::ElementPtr Physics::ToElement() const sdf::ElementPtr Physics::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("physics.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("name")->Set(this->Name(), _errors); elem->GetAttribute("default")->Set(this->IsDefault(), _errors); diff --git a/src/Plugin.cc b/src/Plugin.cc index d0b5d7572..637f12334 100644 --- a/src/Plugin.cc +++ b/src/Plugin.cc @@ -178,7 +178,7 @@ sdf::ElementPtr Plugin::ToElement() const sdf::ElementPtr Plugin::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("plugin.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("name")->Set(this->Name(), _errors); elem->GetAttribute("filename")->Set(this->Filename(), _errors); diff --git a/src/Projector.cc b/src/Projector.cc index 7b851e973..8dbbf6457 100644 --- a/src/Projector.cc +++ b/src/Projector.cc @@ -307,7 +307,7 @@ void Projector::AddPlugin(const Plugin &_plugin) sdf::ElementPtr Projector::ToElement() const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("projector.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); // Set pose sdf::ElementPtr poseElem = elem->GetElement("pose"); diff --git a/src/Root.cc b/src/Root.cc index 0d3210684..2449b5a49 100644 --- a/src/Root.cc +++ b/src/Root.cc @@ -619,7 +619,7 @@ void Root::ResolveAutoInertials(sdf::Errors &_errors, sdf::ElementPtr Root::ToElement(const OutputConfig &_config) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("root.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("version")->Set(this->Version()); diff --git a/src/Scene.cc b/src/Scene.cc index 52e53373b..cf4fcfcd9 100644 --- a/src/Scene.cc +++ b/src/Scene.cc @@ -198,7 +198,7 @@ sdf::ElementPtr Scene::ToElement() const sdf::ElementPtr Scene::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("scene.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetElement("ambient", _errors)->Set(_errors, this->Ambient()); elem->GetElement("background", _errors)->Set(_errors, this->Background()); diff --git a/src/Sensor.cc b/src/Sensor.cc index 15fcdd455..4a2ad4864 100644 --- a/src/Sensor.cc +++ b/src/Sensor.cc @@ -738,7 +738,7 @@ sdf::ElementPtr Sensor::ToElement() const sdf::ElementPtr Sensor::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("sensor.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("type")->Set(this->TypeStr(), _errors); elem->GetAttribute("name")->Set(this->Name(), _errors); diff --git a/src/Surface.cc b/src/Surface.cc index 22b48e198..e8958a0da 100644 --- a/src/Surface.cc +++ b/src/Surface.cc @@ -409,7 +409,7 @@ sdf::ElementPtr Surface::ToElement() const sdf::ElementPtr Surface::ToElement(sdf::Errors &_errors) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("surface.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); sdf::ElementPtr contactElem = elem->GetElement("contact", _errors); contactElem->GetElement("collide_bitmask", _errors)->Set( diff --git a/src/Visual.cc b/src/Visual.cc index 685c26e9b..9682439ec 100644 --- a/src/Visual.cc +++ b/src/Visual.cc @@ -322,7 +322,7 @@ void Visual::SetPoseRelativeToGraph( sdf::ElementPtr Visual::ToElement() const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("visual.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("name")->Set(this->Name()); diff --git a/src/World.cc b/src/World.cc index fa1f939ce..4a884f6fb 100644 --- a/src/World.cc +++ b/src/World.cc @@ -1067,7 +1067,7 @@ Errors World::Implementation::LoadSphericalCoordinates( sdf::ElementPtr World::ToElement(const OutputConfig &_config) const { sdf::ElementPtr elem(new sdf::Element); - sdf::initFile("world.sdf", elem); + sdf::initFile(std::string(this->SchemaFile()), elem); elem->GetAttribute("name")->Set(this->Name()); elem->GetElement("gravity")->Set(this->Gravity()); diff --git a/tools/format_schema.py b/tools/format_schema.py deleted file mode 100644 index ebaa97faa..000000000 --- a/tools/format_schema.py +++ /dev/null @@ -1,67 +0,0 @@ -import os - -lines = os.listdir('./sdf/1.11/') -# Only keep the files that end with .sdf -lines = [line for line in lines if line.endswith('.sdf')] - -for line in lines: - # Remove .sdf extension - snake_case = line.strip().split('.')[0] - - # Convert from snake_case to CamelCase - camel_case = ''.join([word.capitalize() for word in snake_case.split('_')]) - - addHeader = """ - /// \\brief Get the schema file name accessor - public: static inline std::string_view SchemaFile(); - """ - - addImpl = f""" -///////////////////////////////////////////////// -inline std::string_view {camel_case}::SchemaFile() -{{ - static const char kSchemaFile[] = "{line}"; - return kSchemaFile; -}}\n\n""" - - # Debug print statements - # print(addHeader) - # print(addImpl) - # print(addTest) - # print("\n") - - # Edit './include/sdf/{camel_case}.hh' - # Find the line with 'class SDFFORMAT_VISIBLE {camel_case}` - try: - f = open('./include/sdf/' + camel_case + '.hh', 'r') - lines = f.readlines() - f.close() - line_number = 0 - for i, line in enumerate(lines): - if line == " class SDFORMAT_VISIBLE " + camel_case + '\n': - line_number = i+3 # After the class line there is `\n{\n` and then the constructor - break - if line_number == 0: - print("Error: Could not find class declaration in " + camel_case + ".hh") - exit(1) - - try: - with open('./include/sdf/' + camel_case + '.hh', 'w') as file: - for i, line in enumerate(lines): - file.write(line) - if i == line_number: - file.write(addHeader) - except: - print("Unexpected error while writing to: " + camel_case + ".hh.") - except: - print("Error while writing to: " + camel_case + ".hh." + " Check if file exists.") - - # Edit './src/{camel_case}.cc' if it exists - # Add implementation to end of document - if os.path.exists('./src/' + camel_case + '.cc'): - with open('./src/' + camel_case + '.cc', 'a') as file: - file.write(addImpl) - else: - print("Error: Could not find " + camel_case + ".cc") - - print("Changes written to source successfully")