[Libreoffice-commits] core.git: starmath/source
Takeshi Abe
tabe at fixedpoint.jp
Mon Jun 22 04:24:36 PDT 2015
starmath/source/node.cxx | 96 ++++++++++++++---------------------------------
1 file changed, 30 insertions(+), 66 deletions(-)
New commits:
commit 1470e064eec546ab8d73ab537b773279ce85011c
Author: Takeshi Abe <tabe at fixedpoint.jp>
Date: Fri Jun 19 09:43:03 2015 +0900
cppcheck: variableScope
This reduces pNode's scope by introducing ForEachNonNull().
Change-Id: I2c82401da8d7d0919fdd0a0be9b2d82a70b2a7c6
Reviewed-on: https://gerrit.libreoffice.org/16208
Tested-by: Jenkins <ci at libreoffice.org>
Reviewed-by: David Tardon <dtardon at redhat.com>
diff --git a/starmath/source/node.cxx b/starmath/source/node.cxx
index 132c079..9e66810 100644
--- a/starmath/source/node.cxx
+++ b/starmath/source/node.cxx
@@ -37,6 +37,22 @@
#include <math.h>
#include <float.h>
#include <vector>
+#include <boost/checked_delete.hpp>
+
+namespace {
+
+template<typename F>
+void ForEachNonNull(SmNode *pNode, F && f)
+{
+ sal_uInt16 nSize = pNode->GetNumSubNodes();
+ for (sal_uInt16 i = 0; i < nSize; i++) {
+ SmNode *pSubNode = pNode->GetSubNode(i);
+ if (pSubNode != nullptr)
+ f(pSubNode);
+ }
+}
+
+}
SmNode::SmNode(SmNodeType eNodeType, const SmToken &rNodeToken)
: aNodeToken( rNodeToken )
@@ -93,11 +109,8 @@ void SmNode::SetPhantom(bool bIsPhantomP)
if (! (Flags() & FLG_VISIBLE))
bIsPhantom = bIsPhantomP;
- SmNode *pNode;
- sal_uInt16 nSize = GetNumSubNodes();
- for (sal_uInt16 i = 0; i < nSize; i++)
- if (NULL != (pNode = GetSubNode(i)))
- pNode->SetPhantom(bIsPhantom);
+ bool b = bIsPhantom;
+ ForEachNonNull(this, [b](SmNode *pNode){pNode->SetPhantom(b);});
}
@@ -106,11 +119,7 @@ void SmNode::SetColor(const Color& rColor)
if (! (Flags() & FLG_COLOR))
GetFont().SetColor(rColor);
- SmNode *pNode;
- sal_uInt16 nSize = GetNumSubNodes();
- for (sal_uInt16 i = 0; i < nSize; i++)
- if (NULL != (pNode = GetSubNode(i)))
- pNode->SetColor(rColor);
+ ForEachNonNull(this, [&rColor](SmNode *pNode){pNode->SetColor(rColor);});
}
@@ -124,11 +133,7 @@ void SmNode::SetAttribut(sal_uInt16 nAttrib)
nAttributes |= nAttrib;
}
- SmNode *pNode;
- sal_uInt16 nSize = GetNumSubNodes();
- for (sal_uInt16 i = 0; i < nSize; i++)
- if (NULL != (pNode = GetSubNode(i)))
- pNode->SetAttribut(nAttrib);
+ ForEachNonNull(this, [nAttrib](SmNode *pNode){pNode->SetAttribut(nAttrib);});
}
@@ -142,11 +147,7 @@ void SmNode::ClearAttribut(sal_uInt16 nAttrib)
nAttributes &= ~nAttrib;
}
- SmNode *pNode;
- sal_uInt16 nSize = GetNumSubNodes();
- for (sal_uInt16 i = 0; i < nSize; i++)
- if (NULL != (pNode = GetSubNode(i)))
- pNode->ClearAttribut(nAttrib);
+ ForEachNonNull(this, [nAttrib](SmNode *pNode){pNode->ClearAttribut(nAttrib);});
}
@@ -155,11 +156,7 @@ void SmNode::SetFont(const SmFace &rFace)
if (!(Flags() & FLG_FONT))
GetFont() = rFace;
- SmNode *pNode;
- sal_uInt16 nSize = GetNumSubNodes();
- for (sal_uInt16 i = 0; i < nSize; i++)
- if (NULL != (pNode = GetSubNode(i)))
- pNode->SetFont(rFace);
+ ForEachNonNull(this, [&rFace](SmNode *pNode){pNode->SetFont(rFace);});
}
@@ -210,11 +207,7 @@ void SmNode::SetFontSize(const Fraction &rSize, FontSizeType nType)
GetFont().SetSize(aFntSize);
}
- SmNode *pNode;
- sal_uInt16 nSize = GetNumSubNodes();
- for (sal_uInt16 i = 0; i < nSize; i++)
- if (NULL != (pNode = GetSubNode(i)))
- pNode->SetFontSize(rSize, nType);
+ ForEachNonNull(this, [&rSize, &nType](SmNode *pNode){pNode->SetFontSize(rSize, nType);});
}
@@ -222,11 +215,7 @@ void SmNode::SetSize(const Fraction &rSize)
{
GetFont() *= rSize;
- SmNode *pNode;
- sal_uInt16 nSize = GetNumSubNodes();
- for (sal_uInt16 i = 0; i < nSize; i++)
- if (NULL != (pNode = GetSubNode(i)))
- pNode->SetSize(rSize);
+ ForEachNonNull(this, [&rSize](SmNode *pNode){pNode->SetSize(rSize);});
}
@@ -236,13 +225,7 @@ void SmNode::SetRectHorAlign(RectHorAlign eHorAlign, bool bApplyToSubTree )
eRectHorAlign = eHorAlign;
if (bApplyToSubTree)
- {
- SmNode *pNode;
- sal_uInt16 nSize = GetNumSubNodes();
- for (sal_uInt16 i = 0; i < nSize; i++)
- if (NULL != (pNode = GetSubNode(i)))
- pNode->SetRectHorAlign(eHorAlign);
- }
+ ForEachNonNull(this, [eHorAlign](SmNode *pNode){pNode->SetRectHorAlign(eHorAlign);});
}
@@ -271,11 +254,7 @@ void SmNode::Prepare(const SmFormat &rFormat, const SmDocShell &rDocShell)
GetFont().SetWeight(WEIGHT_NORMAL);
GetFont().SetItalic(ITALIC_NONE);
- SmNode *pNode;
- sal_uInt16 nSize = GetNumSubNodes();
- for (sal_uInt16 i = 0; i < nSize; i++)
- if (NULL != (pNode = GetSubNode(i)))
- pNode->Prepare(rFormat, rDocShell);
+ ForEachNonNull(this, [&rFormat, &rDocShell](SmNode *pNode){pNode->Prepare(rFormat, rDocShell);});
}
sal_uInt16 SmNode::FindIndex() const
@@ -301,32 +280,21 @@ void SmNode::Move(const Point& rPosition)
SmRect::Move(rPosition);
- SmNode *pNode;
- sal_uInt16 nSize = GetNumSubNodes();
- for (sal_uInt16 i = 0; i < nSize; i++)
- if (NULL != (pNode = GetSubNode(i)))
- pNode->Move(rPosition);
+ ForEachNonNull(this, [&rPosition](SmNode *pNode){pNode->Move(rPosition);});
}
void SmNode::Arrange(const OutputDevice &rDev, const SmFormat &rFormat)
{
- SmNode *pNode;
- sal_uInt16 nSize = GetNumSubNodes();
- for (sal_uInt16 i = 0; i < nSize; i++)
- if (NULL != (pNode = GetSubNode(i)))
- pNode->Arrange(rDev, rFormat);
+ ForEachNonNull(this, [&rDev, &rFormat](SmNode *pNode){pNode->Arrange(rDev, rFormat);});
}
void SmNode::CreateTextFromNode(OUString &rText)
{
- SmNode *pNode;
sal_uInt16 nSize = GetNumSubNodes();
if (nSize > 1)
rText += "{";
- for (sal_uInt16 i = 0; i < nSize; i++)
- if (NULL != (pNode = GetSubNode(i)))
- pNode->CreateTextFromNode(rText);
+ ForEachNonNull(this, [&rText](SmNode *pNode){pNode->CreateTextFromNode(rText);});
if (nSize > 1)
{
rText = comphelper::string::stripEnd(rText, ' ');
@@ -571,11 +539,7 @@ SmStructureNode::SmStructureNode( const SmStructureNode &rNode ) :
SmStructureNode::~SmStructureNode()
{
- SmNode *pNode;
-
- for (sal_uInt16 i = 0; i < GetNumSubNodes(); i++)
- if (NULL != (pNode = GetSubNode(i)))
- delete pNode;
+ ForEachNonNull(this, boost::checked_deleter<SmNode>());
}
More information about the Libreoffice-commits
mailing list