gazebosim / gazebosim/sdformat
Add schema file name accessor to each DOM type C++ class
- Dominant language
- C++
- Stars
- 216
- Forks
- 125
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 14
Description
## Desired behavior
Getting the schema file for each SDF DOM type element should be possible.
This should make initializing an element for loading standalone DOM types easier.
Without this format. I am doing something like this in my code to initialize a DOM type element so that I can get type checking
```
// Schema file type
template
struct SdfTypeToSchemaFile {
static const std::string kSchemaFile;
};
template <>
const std::string SdfTypeToSchemaFile::kSchemaFile = "surface.sdf";
// Other definitions with template specialization.
template
sdf::ElementPtr init_element_for_type(){
auto element = std::make_shared<::sdf::Element>();
sdf::initFile(SdfTypeToSchemaFile::kSchemaFile, element);
return element;
}
```
Ideally, I hope to do something like this for simplicity.
```
template
sdf::ElementPtr init_element_for_type(){
auto element = std::make_shared<::sdf::Element>();
sdf::initFile(T::SchemaFile(), element);
return element;
}
```
## Alternatives considered
## Implementation suggestion
Implement a static function for each DOM type. For example for `Surface`:
```
// sdformat/include/sdf/Surface.hh
class Surface{
// Omitted details
public: static const std::string& SchemaFile();
};
// sdformat/src/Surface.cc
static const std::string& Surface::SchemaFile(){
static const std::string* kSchemaFile = new std::string("surface.sdf");
return *kSchemaFile;
}
```
## Additional context
Currently the schema file is hard coded in it's `T::ToElement()` for example:
https://github.com/gazebosim/sdformat/blob/c4bfe35bdaca8e667089bf9adf99b7930192ccaf/src/Surface.cc#L412
Contributor guide
Assessment
This issue has not been assessed yet.