1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <mathml/iterator.hxx>
12 /** The purpose of this iterator is to be able to iterate threw an infinite element tree
13 * infinite -> as much as your memory can hold
14 * No call-backs that will end up in out of stack
19 static inline void deleteElement(SmMlElement
* aSmMlElement
, void*) { delete aSmMlElement
; }
21 static inline void cloneElement(SmMlElement
* aSmMlElement
, void* aData
)
24 SmMlElement
* aNewSmMlElement
= new SmMlElement(*aSmMlElement
);
25 SmMlElement
* aCopyTree
= *static_cast<SmMlElement
**>(aData
);
28 aCopyTree
->setSubElement(aCopyTree
->getSubElementsCount(), aNewSmMlElement
);
30 // Prepare for following
31 // If it has sub elements, then it will be the next
32 if (aSmMlElement
->getSubElementsCount() != 0)
33 aCopyTree
= aNewSmMlElement
;
34 else // Otherwise remounts up to where it should be
36 while (aSmMlElement
->getParentElement() != nullptr)
39 SmMlElement
* pParent
= aSmMlElement
->getParentElement();
40 aCopyTree
= aCopyTree
->getParentElement();
41 // was this the last branch ?
42 if (aSmMlElement
->getSubElementId() + 1 != pParent
->getSubElementsCount())
43 break; // no -> stop going up
44 // Prepare for next round
45 aSmMlElement
= pParent
;
50 *static_cast<SmMlElement
**>(aData
) = aCopyTree
;
53 void SmMlIteratorFree(SmMlElement
* pMlElementTree
)
55 if (pMlElementTree
== nullptr)
57 for (size_t i
= 0; i
< pMlElementTree
->getSubElementsCount(); ++i
)
59 SmMlIteratorFree(pMlElementTree
->getSubElement(i
));
61 deleteElement(pMlElementTree
, nullptr);
64 SmMlElement
* SmMlIteratorCopy(SmMlElement
* pMlElementTree
)
66 if (pMlElementTree
== nullptr)
68 SmMlElement
* aDummyElement
= new SmMlElement();
69 SmMlIteratorTopToBottom(pMlElementTree
, cloneElement
, &aDummyElement
);
70 SmMlElement
* aResultElement
= aDummyElement
->getSubElement(0);
72 return aResultElement
;
75 } // end namespace mathml
77 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */