diff --git a/genericfileelement.h b/genericfileelement.h index 65b044b..ecf255b 100644 --- a/genericfileelement.h +++ b/genericfileelement.h @@ -166,6 +166,8 @@ public: const ImplementationType* nextSibling() const; ImplementationType* firstChild(); const ImplementationType* firstChild() const; + ImplementationType* lastChild(); + const ImplementationType* lastChild() const; ImplementationType* subelementByPath(const std::initializer_list &path); ImplementationType* subelementByPath(std::list &path); ImplementationType* childById(const IdentifierType &id); @@ -557,6 +559,39 @@ inline const ImplementationType *GenericFileElement::firstCh return m_firstChild.get(); } +/*! + * \brief Returns the last child of the element. + * + * The current element keeps ownership over the returned element. + * If no childs are present nullptr is returned. + * + * \remarks parse() needs to be called before. + */ +template +inline ImplementationType *GenericFileElement::lastChild() +{ + for(ImplementationType *child = firstChild(); child; child = child->nextSibling()) { + if(!child->m_nextSibling) { + return child; + } + } + return nullptr; +} + +/*! + * \brief Returns the last child of the element. + * + * The current element keeps ownership over the returned element. + * If no childs are present nullptr is returned. + * + * \remarks parse() needs to be called before. + */ +template +inline const ImplementationType *GenericFileElement::lastChild() const +{ + return const_cast *>(this)->lastChild(); +} + /*! * \brief Returns the sub element for the specified \a path. *