From e4ffa5d7b91b30126d06f575f2655095e1acf502 Mon Sep 17 00:00:00 2001 From: Gaute Lindkvist Date: Fri, 24 Jan 2020 15:16:59 +0100 Subject: [PATCH] #5388 Add a CAF-feature to get the ancestor of a type (but not this) --- .../cafPdmCore/cafPdmObjectHandle.h | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmObjectHandle.h b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmObjectHandle.h index 40dc6f18e6..c16e84bfdc 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmObjectHandle.h +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmObjectHandle.h @@ -38,6 +38,11 @@ public: template void firstAncestorOrThisOfType(T*& ancestor) const; + /// Traverses parents recursively and returns first parent of the requested type. + /// Does NOT check _this_ object + template + void firstAncestorOfType(T*& ancestor) const; + /// Calls firstAncestorOrThisOfType, and asserts that a valid object is found template void firstAncestorOrThisOfTypeAsserted(T*& ancestor) const; @@ -137,36 +142,27 @@ void PdmObjectHandle::firstAncestorOrThisOfType(T*& ancestor) const return; } - // Search parents for first type match + this->firstAncestorOfType(ancestor); +} +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +template +void PdmObjectHandle::firstAncestorOfType(T*& ancestor) const +{ + ancestor = nullptr; + + // Search parents for first type match PdmObjectHandle* parent = nullptr; PdmFieldHandle* parentField = this->parentField(); if (parentField) parent = parentField->ownerObject(); - while (parent != nullptr) + if (parent != nullptr) { - T* objectOfType = dynamic_cast(parent); - if (objectOfType) - { - ancestor = objectOfType; - return; - } - - // Get next level parent - - PdmFieldHandle* nextParentField = parent->parentField(); - - if (nextParentField) - { - parent = nextParentField->ownerObject(); - } - else - { - parent = nullptr; - } + parent->firstAncestorOrThisOfType(ancestor); } } - //-------------------------------------------------------------------------------------------------- /// //--------------------------------------------------------------------------------------------------