From bf31c9e89ffb6ff82fcb26fc0d6dc01e9d3c2ff5 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 18 Aug 2017 00:07:42 +0200 Subject: [PATCH] Add methods to get n-th parent of file element --- genericfileelement.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/genericfileelement.h b/genericfileelement.h index e767d9d..65b044b 100644 --- a/genericfileelement.h +++ b/genericfileelement.h @@ -160,6 +160,8 @@ public: byte level() const; ImplementationType* parent(); const ImplementationType* parent() const; + ImplementationType* parent(byte n); + const ImplementationType* parent(byte n) const; ImplementationType* nextSibling(); const ImplementationType* nextSibling() const; ImplementationType* firstChild(); @@ -473,6 +475,32 @@ inline const ImplementationType *GenericFileElement::parent( return m_parent; } +/*! + * \brief Returns the n-th parent of the element. + * \remarks + * - The returned element has ownership (at least indirect) over the current instance. + * - Returns nullptr if level() < \a n. + */ +template +ImplementationType *GenericFileElement::parent(byte n) +{ + ImplementationType *parent = static_cast(this); + for(; n && parent; --n, parent = parent->m_parent); + return parent; +} + +/*! + * \brief Returns the n-th parent of the element. + * \remarks + * - The returned element has ownership (at least indirect) over the current instance. + * - Returns nullptr if level() < \a n. + */ +template +inline const ImplementationType *GenericFileElement::parent(byte n) const +{ + return const_cast *>(this)->parent(n); +} + /*! * \brief Returns the next sibling of the element. *