gazebosim/sdformat

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

開放

#1,376 建立於 2024年2月29日

 (3 則留言) (0 個反應) (0 位負責人)C++ (122 個分叉)auto 404
enhancementgood first issuehelp wanted

倉庫指標

星標
 (216 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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

貢獻者指南