From da60312f485fef948b1f38578a8aa13f397e102d Mon Sep 17 00:00:00 2001 From: Jonathan Stone Date: Mon, 14 Oct 2024 17:19:57 -0700 Subject: [PATCH] Move templated accessors to Element.cpp (#2069) This changelist moves the templated accessors `getChildOfType` and `getChildrenOfType` from Element.h to Element.cpp, allowing upcoming features to be implemented in a more straightforward fashion. --- source/MaterialXCore/Element.cpp | 29 ++++++++++++++++++++++++++--- source/MaterialXCore/Element.h | 21 ++------------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/source/MaterialXCore/Element.cpp b/source/MaterialXCore/Element.cpp index 149b7bc757..1a48142962 100644 --- a/source/MaterialXCore/Element.cpp +++ b/source/MaterialXCore/Element.cpp @@ -284,6 +284,27 @@ ElementPtr Element::changeChildCategory(ElementPtr child, const string& category return newChild; } +template shared_ptr Element::getChildOfType(const string& name) const +{ + ElementPtr child = getChild(name); + return child ? child->asA() : shared_ptr(); +} + +template vector> Element::getChildrenOfType(const string& category) const +{ + vector> children; + for (ElementPtr child : _childOrder) + { + shared_ptr instance = child->asA(); + if (!instance) + continue; + if (!category.empty() && child->getCategory() != category) + continue; + children.push_back(instance); + } + return children; +} + ElementPtr Element::getRoot() { ElementPtr root = _root.lock(); @@ -866,9 +887,11 @@ template class ElementRegistry // Template instantiations // -#define INSTANTIATE_SUBCLASS(T) \ - template MX_CORE_API shared_ptr Element::asA(); \ - template MX_CORE_API shared_ptr Element::asA() const; +#define INSTANTIATE_SUBCLASS(T) \ + template MX_CORE_API shared_ptr Element::asA(); \ + template MX_CORE_API shared_ptr Element::asA() const; \ + template MX_CORE_API shared_ptr Element::getChildOfType(const string& name) const; \ + template MX_CORE_API vector> Element::getChildrenOfType(const string& category) const; INSTANTIATE_SUBCLASS(Element) INSTANTIATE_SUBCLASS(GeomElement) diff --git a/source/MaterialXCore/Element.h b/source/MaterialXCore/Element.h index 2b92dd8094..a80bc3a49f 100644 --- a/source/MaterialXCore/Element.h +++ b/source/MaterialXCore/Element.h @@ -439,11 +439,7 @@ class MX_CORE_API Element : public std::enable_shared_from_this /// Return the child element, if any, with the given name and subclass. /// If a child with the given name exists, but belongs to a different /// subclass, then an empty shared pointer is returned. - template shared_ptr getChildOfType(const string& name) const - { - ElementPtr child = getChild(name); - return child ? child->asA() : shared_ptr(); - } + template shared_ptr getChildOfType(const string& name) const; /// Return a constant vector of all child elements. /// The returned vector maintains the order in which children were added. @@ -455,20 +451,7 @@ class MX_CORE_API Element : public std::enable_shared_from_this /// Return a vector of all child elements that are instances of the given /// subclass, optionally filtered by the given category string. The returned /// vector maintains the order in which children were added. - template vector> getChildrenOfType(const string& category = EMPTY_STRING) const - { - vector> children; - for (ElementPtr child : _childOrder) - { - shared_ptr instance = child->asA(); - if (!instance) - continue; - if (!category.empty() && child->getCategory() != category) - continue; - children.push_back(instance); - } - return children; - } + template vector> getChildrenOfType(const string& category = EMPTY_STRING) const; /// Set the index of the child, if any, with the given name. /// If the given index is out of bounds, then an exception is thrown.