Added firstAncestorOfType

This commit is contained in:
Magne Sjaastad
2015-08-12 11:02:50 +02:00
parent fb5fb51fc1
commit 711cc16da1

View File

@@ -31,6 +31,10 @@ public:
/// The field referencing this object as a child
PdmFieldHandle* parentField() const;
///
template <typename T>
void firstAncestorOfType(T*& ancestor) const;
// PtrReferences
/// The PdmPtrField's containing pointers to this PdmObjecthandle
/// Use ownerObject() on the fieldHandle to get the PdmObjectHandle
@@ -89,5 +93,26 @@ private:
std::set<PdmObjectHandle**> m_pointersReferencingMe;
};
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template <typename T>
void PdmObjectHandle::firstAncestorOfType(T*& ancestor) const
{
PdmObjectHandle* parentObject = this->parentField()->ownerObject();
while (parentObject)
{
T* objectOfType = dynamic_cast<T*>(parentObject);
if (objectOfType)
{
ancestor = objectOfType;
return;
}
parentObject = parentObject->parentField()->ownerObject();
}
}
} // End of namespace caf