[Libreoffice-commits] core.git: starmath/inc starmath/source

Takeshi Abe tabe at fixedpoint.jp
Thu May 26 02:23:19 UTC 2016


 starmath/inc/cursor.hxx    |   26 +++++++---------------
 starmath/source/cursor.cxx |   52 +++++++++++++++------------------------------
 2 files changed, 27 insertions(+), 51 deletions(-)

New commits:
commit f5ea7c09c8a6924b72c9b756a8e435ff40dad6c6
Author: Takeshi Abe <tabe at fixedpoint.jp>
Date:   Wed May 25 20:00:35 2016 +0900

    starmath: nodes in the clipboard are owened by SmClipboard
    
    This also omits the bIsOnlyIfSelected parameter for former
    CloneLineToList(), which was true at its sole call site.
    
    Change-Id: Idb71323f68f13ecc90d430ec8e18e0eef766ae4b
    Reviewed-on: https://gerrit.libreoffice.org/25444
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Takeshi Abe <tabe at fixedpoint.jp>

diff --git a/starmath/inc/cursor.hxx b/starmath/inc/cursor.hxx
index 3f5c3df..9554fc7 100644
--- a/starmath/inc/cursor.hxx
+++ b/starmath/inc/cursor.hxx
@@ -69,6 +69,8 @@ enum SmBracketType {
 /** A list of nodes */
 typedef std::list<SmNode*> SmNodeList;
 
+typedef std::list<std::unique_ptr<SmNode>> SmClipboard;
+
 class SmDocShell;
 
 /** Formula cursor
@@ -84,7 +86,6 @@ public:
         , mpPosition(nullptr)
         , mpTree(tree)
         , mpDocShell(pShell)
-        , mpClipboard(nullptr)
         , mnEditSections(0)
         , mbIsEnabledSetModifiedSmDocShell(false)
     {
@@ -94,7 +95,6 @@ public:
 
     ~SmCursor()
     {
-        SetClipboard();
     }
 
     /** Get position */
@@ -231,7 +231,7 @@ private:
     /** Graph over caret position in the current tree */
     std::unique_ptr<SmCaretPosGraph> mpGraph;
     /** Clipboard holder */
-    SmNodeList* mpClipboard;
+    SmClipboard maClipboard;
 
     /** Returns a node that is selected, if any could be found */
     SmNode* FindSelectedNode(SmNode* pNode);
@@ -281,13 +281,12 @@ private:
         return pList;
     }
 
-    /** Clone a visual line to a list
+    /** Clone a visual line to a clipboard
      *
-     * Doesn't clone SmErrorNode's these are ignored, as they are context dependent metadata.
+     * ... but the selected part only.
+     * Doesn't clone SmErrorNodes, which are ignored as they are context dependent metadata.
      */
-    static SmNodeList* CloneLineToList(SmStructureNode* pLine,
-                                       bool bOnlyIfSelected = false,
-                                       SmNodeList* pList = new SmNodeList());
+    static void CloneLineToClipboard(SmStructureNode* pLine, SmClipboard* pClipboard);
 
     /** Build pGraph over caret positions */
     void BuildGraph();
@@ -304,15 +303,8 @@ private:
     /** Set selected on nodes of the tree */
     void AnnotateSelection();
 
-    /** Set the clipboard, and release current clipboard
-     *
-     * Call this method with NULL to reset the clipboard
-     * @remarks: This method takes ownership of pList.
-     */
-    void SetClipboard(SmNodeList* pList = nullptr);
-
-    /** Clone list of nodes (creates a deep clone) */
-    static SmNodeList* CloneList(SmNodeList* pList);
+    /** Clone list of nodes in a clipboard (creates a deep clone) */
+    static SmNodeList* CloneList(SmClipboard &rClipboard);
 
     /** Find an iterator pointing to the node in pLineList following aCaretPos
      *
diff --git a/starmath/source/cursor.cxx b/starmath/source/cursor.cxx
index b70b8ed..36bc43d 100644
--- a/starmath/source/cursor.cxx
+++ b/starmath/source/cursor.cxx
@@ -1168,67 +1168,52 @@ void SmCursor::Copy(){
     assert(pLine);
 
     //Clone selected nodes
-    SmNodeList* pList;
+    SmClipboard aClipboard;
     if(IsLineCompositionNode(pLine))
-        pList = CloneLineToList(static_cast<SmStructureNode*>(pLine), true);
+        CloneLineToClipboard(static_cast<SmStructureNode*>(pLine), &aClipboard);
     else{
-        pList = new SmNodeList();
         //Special care to only clone selected text
         if(pLine->GetType() == NTEXT) {
             SmTextNode *pText = static_cast<SmTextNode*>(pLine);
-            SmTextNode *pClone = new SmTextNode( pText->GetToken(), pText->GetFontDesc() );
+            std::unique_ptr<SmTextNode> pClone(new SmTextNode( pText->GetToken(), pText->GetFontDesc() ));
             int start  = pText->GetSelectionStart(),
                 length = pText->GetSelectionEnd() - pText->GetSelectionStart();
             pClone->ChangeText(pText->GetText().copy(start, length));
             pClone->SetScaleMode(pText->GetScaleMode());
-            pList->push_front(pClone);
+            aClipboard.push_front(std::move(pClone));
         } else {
             SmCloningVisitor aCloneFactory;
-            pList->push_front(aCloneFactory.Clone(pLine));
+            aClipboard.push_front(std::unique_ptr<SmNode>(aCloneFactory.Clone(pLine)));
         }
     }
 
     //Set clipboard
-    if (pList->size() > 0)
-        SetClipboard(pList);
-    else
-        delete pList;
+    if (aClipboard.size() > 0)
+        maClipboard = std::move(aClipboard);
 }
 
 void SmCursor::Paste() {
     BeginEdit();
     Delete();
 
-    if(mpClipboard && mpClipboard->size() > 0)
-        InsertNodes(CloneList(mpClipboard));
+    if(maClipboard.size() > 0)
+        InsertNodes(CloneList(maClipboard));
 
     EndEdit();
 }
 
-SmNodeList* SmCursor::CloneList(SmNodeList* pList){
+SmNodeList* SmCursor::CloneList(SmClipboard &rClipboard){
     SmCloningVisitor aCloneFactory;
     SmNodeList* pClones = new SmNodeList();
 
-    SmNodeList::iterator it;
-    for(it = pList->begin(); it != pList->end(); ++it){
-        SmNode *pClone = aCloneFactory.Clone(*it);
+    for(auto &xNode : rClipboard){
+        SmNode *pClone = aCloneFactory.Clone(xNode.get());
         pClones->push_back(pClone);
     }
 
     return pClones;
 }
 
-void SmCursor::SetClipboard(SmNodeList* pList){
-    if(mpClipboard){
-        //Delete all nodes on the clipboard
-        SmNodeList::iterator it;
-        for(it = mpClipboard->begin(); it != mpClipboard->end(); ++it)
-            delete (*it);
-        delete mpClipboard;
-    }
-    mpClipboard = pList;
-}
-
 SmNode* SmCursor::FindTopMostNodeInLine(SmNode* pSNode, bool MoveUpIfSelected){
     //If we haven't got a subnode
     if(!pSNode)
@@ -1288,27 +1273,26 @@ SmNodeList* SmCursor::LineToList(SmStructureNode* pLine, SmNodeList* list){
     return list;
 }
 
-SmNodeList* SmCursor::CloneLineToList(SmStructureNode* pLine, bool bOnlyIfSelected, SmNodeList* pList){
+void SmCursor::CloneLineToClipboard(SmStructureNode* pLine, SmClipboard* pClipboard){
     SmCloningVisitor aCloneFactory;
     SmNodeIterator it(pLine);
     while(it.Next()){
         if( IsLineCompositionNode( it.Current() ) )
-            CloneLineToList( static_cast<SmStructureNode*>(it.Current()), bOnlyIfSelected, pList );
-        else if( (!bOnlyIfSelected || it->IsSelected()) && it->GetType() != NERROR ) {
+            CloneLineToClipboard( static_cast<SmStructureNode*>(it.Current()), pClipboard );
+        else if( it->IsSelected() && it->GetType() != NERROR ) {
             //Only clone selected text from SmTextNode
             if(it->GetType() == NTEXT) {
                 SmTextNode *pText = static_cast<SmTextNode*>(it.Current());
-                SmTextNode *pClone = new SmTextNode( it->GetToken(), pText->GetFontDesc() );
+                std::unique_ptr<SmTextNode> pClone(new SmTextNode( it->GetToken(), pText->GetFontDesc() ));
                 int start = pText->GetSelectionStart(),
                     length = pText->GetSelectionEnd() - pText->GetSelectionStart();
                 pClone->ChangeText(pText->GetText().copy(start, length));
                 pClone->SetScaleMode(pText->GetScaleMode());
-                pList->push_back(pClone);
+                pClipboard->push_back(std::move(pClone));
             } else
-                pList->push_back(aCloneFactory.Clone(it.Current()));
+                pClipboard->push_back(std::unique_ptr<SmNode>(aCloneFactory.Clone(it.Current())));
         }
     }
-    return pList;
 }
 
 bool SmCursor::IsLineCompositionNode(SmNode* pNode){


More information about the Libreoffice-commits mailing list