gazebosim/sdformat

Add schema file name accessor to each DOM type C++ class

Aperta

#1376 aperta il 29 feb 2024

 (3 commenti) (0 reazioni) (0 assegnatari)C++ (122 fork)auto 404
enhancementgood first issuehelp wanted

Metriche repository

Star
 (216 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

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

Guida contributor