Add schema file name accessor to each DOM type C++ class
#1,376 opened on Feb 29, 2024
Repository metrics
- Stars
- (216 stars)
- PR merge metrics
- (PR metrics pending)
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 <typename T>
struct SdfTypeToSchemaFile {
static const std::string kSchemaFile;
};
template <>
const std::string SdfTypeToSchemaFile<sdf::Surface>::kSchemaFile = "surface.sdf";
// Other definitions with template specialization.
template <typename T>
sdf::ElementPtr init_element_for_type(){
auto element = std::make_shared<::sdf::Element>();
sdf::initFile(SdfTypeToSchemaFile<T>::kSchemaFile, element);
return element;
}
Ideally, I hope to do something like this for simplicity.
template <typename T>
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