summaryrefslogtreecommitdiff
path: root/starmath
diff options
context:
space:
mode:
authorTakeshi Abe <tabe@fixedpoint.jp>2018-01-16 18:40:23 +0900
committerNoel Grandin <noel.grandin@collabora.co.uk>2018-01-17 07:45:05 +0100
commit5306ae7ecd8ffa8bb78c95106b46322ae4ca1157 (patch)
tree064751022a381262924163960412ef3ecb77b42a /starmath
parent688b16df55afa1920f007b5bbe52c24f431c4ac2 (diff)
starmath: Make SmParser::ParseExpression() return std::unique_ptr
Change-Id: Ib8b65dced2af3ac7dca3fe9dd02e70f02c865f79 Reviewed-on: https://gerrit.libreoffice.org/47974 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'starmath')
-rw-r--r--starmath/inc/parse.hxx4
-rw-r--r--starmath/qa/cppunit/test_nodetotextvisitors.cxx33
-rw-r--r--starmath/source/ElementsDockingWindow.cxx2
-rw-r--r--starmath/source/cursor.cxx5
-rw-r--r--starmath/source/parse.cxx14
5 files changed, 25 insertions, 33 deletions
diff --git a/starmath/inc/parse.hxx b/starmath/inc/parse.hxx
index d5e014e6717b..5c4646e28eb8 100644
--- a/starmath/inc/parse.hxx
+++ b/starmath/inc/parse.hxx
@@ -80,7 +80,7 @@ class SmParser
// grammar
SmTableNode *DoTable();
SmLineNode *DoLine();
- SmNode *DoExpression(bool bUseExtraSpaces = true);
+ std::unique_ptr<SmNode> DoExpression(bool bUseExtraSpaces = true);
SmNode *DoRelation();
SmNode *DoSum();
SmNode *DoProduct();
@@ -116,7 +116,7 @@ public:
/** Parse rBuffer to formula tree */
SmTableNode *Parse(const OUString &rBuffer);
/** Parse rBuffer to formula subtree that constitutes an expression */
- SmNode *ParseExpression(const OUString &rBuffer);
+ std::unique_ptr<SmNode> ParseExpression(const OUString &rBuffer);
const OUString & GetText() const { return m_aBufferString; };
diff --git a/starmath/qa/cppunit/test_nodetotextvisitors.cxx b/starmath/qa/cppunit/test_nodetotextvisitors.cxx
index 6791e9d6270c..5399e68ec338 100644
--- a/starmath/qa/cppunit/test_nodetotextvisitors.cxx
+++ b/starmath/qa/cppunit/test_nodetotextvisitors.cxx
@@ -431,18 +431,17 @@ void Test::SimpleSpecialChars()
void Test::parseandparseagain(const char *formula, const char *test_name)
{
OUString output1, output2;
- SmNode *pNode1, *pNode2;
// parse 1
OUString input = OUString::createFromAscii(formula);
- pNode1 = SmParser().ParseExpression(input);
+ auto pNode1 = SmParser().ParseExpression(input);
pNode1->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
- SmNodeToTextVisitor(pNode1, output1);
+ SmNodeToTextVisitor(pNode1.get(), output1);
// parse 2
- pNode2 = SmParser().ParseExpression(output1);
+ auto pNode2 = SmParser().ParseExpression(output1);
pNode2->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
- SmNodeToTextVisitor(pNode2, output2);
+ SmNodeToTextVisitor(pNode2.get(), output2);
// compare
CPPUNIT_ASSERT_EQUAL_MESSAGE(test_name,
@@ -453,21 +452,17 @@ void Test::parseandparseagain(const char *formula, const char *test_name)
std::unique_ptr<MockVisitor> mv(new MockVisitor);
pNode1->Accept(mv.get());
pNode2->Accept(mv.get());
-
- delete pNode1;
- delete pNode2;
}
void Test::ParseAndCheck(const char *formula, const char * expected, const char *test_name)
{
OUString sOutput;
- SmNode *pNode;
// parse
OUString sInput = OUString::createFromAscii(formula);
- pNode = SmParser().ParseExpression(sInput);
+ auto pNode = SmParser().ParseExpression(sInput);
pNode->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
- SmNodeToTextVisitor(pNode, sOutput);
+ SmNodeToTextVisitor(pNode.get(), sOutput);
// compare
OUString sExpected = OUString::createFromAscii(expected);
@@ -478,27 +473,24 @@ void Test::ParseAndCheck(const char *formula, const char * expected, const char
// auxiliary test for Accept()
std::unique_ptr<MockVisitor> mv(new MockVisitor);
pNode->Accept(mv.get());
-
- delete pNode;
}
// Parse two formula commands and verify that they give the same output
void Test::ParseAndCompare(const char *formula1, const char *formula2, const char *test_name)
{
OUString sOutput1, sOutput2;
- SmNode *pNode1, *pNode2;
// parse formula1
OUString sInput1 = OUString(formula1, strlen(formula1), RTL_TEXTENCODING_UTF8);
- pNode1 = SmParser().ParseExpression(sInput1);
+ auto pNode1 = SmParser().ParseExpression(sInput1);
pNode1->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
- SmNodeToTextVisitor(pNode1, sOutput1);
+ SmNodeToTextVisitor(pNode1.get(), sOutput1);
// parse formula2
OUString sInput2 = OUString(formula2, strlen(formula2), RTL_TEXTENCODING_UTF8);
- pNode2 = SmParser().ParseExpression(sInput2);
+ auto pNode2 = SmParser().ParseExpression(sInput2);
pNode2->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
- SmNodeToTextVisitor(pNode2, sOutput2);
+ SmNodeToTextVisitor(pNode2.get(), sOutput2);
CPPUNIT_ASSERT_EQUAL_MESSAGE(test_name, sOutput1, sOutput2);
@@ -506,9 +498,6 @@ void Test::ParseAndCompare(const char *formula1, const char *formula2, const cha
std::unique_ptr<MockVisitor> mv(new MockVisitor);
pNode1->Accept(mv.get());
pNode2->Accept(mv.get());
-
- delete pNode1;
- delete pNode2;
}
void Test::testBinomInBinHor()
@@ -670,7 +659,7 @@ void Test::testParser()
OUString sOutput;
OUString sInput(u"{ \U0001D44E }"); // non-BMP Unicode
OUString sExpected(u"\U0001D44E");
- std::unique_ptr<SmNode> pNode(SmParser().ParseExpression(sInput));
+ auto pNode = SmParser().ParseExpression(sInput);
pNode->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
SmNodeToTextVisitor(pNode.get(), sOutput);
CPPUNIT_ASSERT_EQUAL(sExpected, sOutput);
diff --git a/starmath/source/ElementsDockingWindow.cxx b/starmath/source/ElementsDockingWindow.cxx
index 9ab977f489c0..c39833f051bc 100644
--- a/starmath/source/ElementsDockingWindow.cxx
+++ b/starmath/source/ElementsDockingWindow.cxx
@@ -523,7 +523,7 @@ void SmElementsControl::DoScroll(long nDelta)
void SmElementsControl::addElement(const OUString& aElementVisual, const OUString& aElementSource, const OUString& aHelpText)
{
- std::unique_ptr<SmNode> pNode(SmParser().ParseExpression(aElementVisual));
+ auto pNode = SmParser().ParseExpression(aElementVisual);
pNode->Prepare(maFormat, *mpDocShell, 0);
pNode->SetSize(Fraction(10,8));
diff --git a/starmath/source/cursor.cxx b/starmath/source/cursor.cxx
index 033231a28b3a..bc6d5227d043 100644
--- a/starmath/source/cursor.cxx
+++ b/starmath/source/cursor.cxx
@@ -1012,12 +1012,13 @@ void SmCursor::InsertSpecial(const OUString& _aString)
void SmCursor::InsertCommandText(const OUString& aCommandText) {
//Parse the sub expression
- SmNode* pSubExpr = SmParser().ParseExpression(aCommandText);
+ auto xSubExpr = SmParser().ParseExpression(aCommandText);
//Prepare the subtree
- pSubExpr->Prepare(mpDocShell->GetFormat(), *mpDocShell, 0);
+ xSubExpr->Prepare(mpDocShell->GetFormat(), *mpDocShell, 0);
//Convert subtree to list
+ SmNode* pSubExpr = xSubExpr.release();
SmNodeList* pLineList = NodeToList(pSubExpr);
BeginEdit();
diff --git a/starmath/source/parse.cxx b/starmath/source/parse.cxx
index 8994901fb5f8..6d299ce1fee5 100644
--- a/starmath/source/parse.cxx
+++ b/starmath/source/parse.cxx
@@ -989,7 +989,7 @@ SmNode *SmParser::DoAlign(bool bUseExtraSpaces)
return DoError(SmParseError::DoubleAlign);
}
- std::unique_ptr<SmNode> pNode(DoExpression(bUseExtraSpaces));
+ auto pNode = DoExpression(bUseExtraSpaces);
if (xSNode)
{
@@ -1015,7 +1015,7 @@ SmLineNode *SmParser::DoLine()
ExpressionArray.emplace_back(std::unique_ptr<SmNode>(DoAlign()));
while (m_aCurToken.eType != TEND && m_aCurToken.eType != TNEWLINE)
- ExpressionArray.emplace_back(std::unique_ptr<SmNode>(DoExpression()));
+ ExpressionArray.push_back(DoExpression());
//If there's no expression, add an empty one.
//this is to avoid a formula tree without any caret
@@ -1032,7 +1032,7 @@ SmLineNode *SmParser::DoLine()
return xSNode.release();
}
-SmNode *SmParser::DoExpression(bool bUseExtraSpaces)
+std::unique_ptr<SmNode> SmParser::DoExpression(bool bUseExtraSpaces)
{
DepthProtect aDepthGuard(m_nParseDepth);
if (aDepthGuard.TooDeep())
@@ -1048,12 +1048,14 @@ SmNode *SmParser::DoExpression(bool bUseExtraSpaces)
std::unique_ptr<SmExpressionNode> xSNode(new SmExpressionNode(m_aCurToken));
xSNode->SetSubNodes(buildNodeArray(RelationArray));
xSNode->SetUseExtraSpaces(bUseExtraSpaces);
- return xSNode.release();
+ // the following explicit move can be omitted since C++14:
+ // https://stackoverflow.com/questions/22018115/converting-stdunique-ptrderived-to-stdunique-ptrbase
+ return std::move(xSNode);
}
else
{
// This expression has only one node so just push this node.
- return RelationArray[0].release();
+ return std::move(RelationArray[0]);
}
}
@@ -2329,7 +2331,7 @@ SmTableNode *SmParser::Parse(const OUString &rBuffer)
return DoTable();
}
-SmNode *SmParser::ParseExpression(const OUString &rBuffer)
+std::unique_ptr<SmNode> SmParser::ParseExpression(const OUString &rBuffer)
{
m_aBufferString = convertLineEnd(rBuffer, LINEEND_LF);
m_nBufferIndex = 0;