Skip to content

Commit

Permalink
string to string_view, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aagrawal05 committed May 19, 2024
1 parent 2a1d038 commit 792c5f4
Show file tree
Hide file tree
Showing 32 changed files with 113 additions and 97 deletions.
83 changes: 83 additions & 0 deletions format_schema.py
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion src/Actor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/AirPressure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(
_errors, this->ReferenceAltitude());
Expand Down
2 changes: 1 addition & 1 deletion src/AirSpeed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/Altimeter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Atmosphere.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/Camera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(this->Name());
sdf::ElementPtr poseElem = elem->GetElement("pose");
Expand Down
2 changes: 1 addition & 1 deletion src/Collision.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/ForceTorque.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/Frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Geometry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Gui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Imu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Joint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(this->Name());
sdf::ElementPtr poseElem = elem->GetElement("pose");
Expand Down
2 changes: 1 addition & 1 deletion src/Lidar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/Light.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/Link.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion src/Magnetometer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion src/Material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/Model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/Noise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/ParticleEmitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Physics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Projector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/Root.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion src/Scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/Sensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(this->TypeStr(), _errors);
elem->GetAttribute("name")->Set<std::string>(this->Name(), _errors);
Expand Down
2 changes: 1 addition & 1 deletion src/Surface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/Visual.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion src/World.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading

0 comments on commit 792c5f4

Please sign in to comment.