cellml / libcellml

Repository for libCellML development.
https://libcellml.org
Apache License 2.0
16 stars 21 forks source link

How to determine parent class of Component? #917

Closed Moop204 closed 3 years ago

Moop204 commented 3 years ago

If I needed to take the parent of a Component, how would I be able to determine if it is a Model or another Component?

hsorby commented 3 years ago

I'll answer for C++,

auto component = std::dynamic_pointer_cast<libcellml::Component>(entity->parent())
auto model = std::dynamic_pointer_cast<libcellml::Model>(entity->parent())
if (component != nullptr) {
  // it's a component.
} else if (model != nullptr) {
  // it's a model.
}
Moop204 commented 3 years ago

Thank you, to be complete I will also include the javascript solution.

component = new libcellml.Component();
...
parent = component.parent(); 
if (parent instanceof libcellml.Component) {
  // Component
} else {
  // Model
}