[PATCH] Cleanup isDerivedFrom function
Kevin Hunter
hunteke at earlham.edu
Tue Oct 4 09:51:10 PDT 2011
I don't know why it was written previously with a for-loop; my hunch is
that it was originally a mix of thoughts between a recursive and
non-recursive approach. Either way, this could be simplified, so here
is a non-recursive approach. (Why create stack overhead when another
algorithm would suffice?)
This solution also reduces the stack size of the function itself, and
reduces a variable scope.
---
stoc/source/inspect/introspection.cxx | 30 ++++++++----------------------
1 files changed, 8 insertions(+), 22 deletions(-)
diff --git a/stoc/source/inspect/introspection.cxx b/stoc/source/inspect/introspection.cxx
index 36f1acc..831802b 100644
--- a/stoc/source/inspect/introspection.cxx
+++ b/stoc/source/inspect/introspection.cxx
@@ -109,30 +109,16 @@ typedef WeakImplHelper3< XIntrospectionAccess, XMaterialHolder, XExactName > Int
sal_Bool isDerivedFrom( Reference<XIdlClass> xToTestClass, Reference<XIdlClass> xDerivedFromClass )
{
Sequence< Reference<XIdlClass> > aClassesSeq = xToTestClass->getSuperclasses();
- const Reference<XIdlClass>* pClassesArray = aClassesSeq.getConstArray();
- sal_Int32 nSuperClassCount = aClassesSeq.getLength();
- sal_Int32 i;
- for( i = 0 ;
- i < nSuperClassCount ;
- /* No "increment" expression needed as the body always
- * returns, and in fact MSVC warns about unreachable code if
- * we include one. On the other hand, what's the point in
- * using a for loop here then if all we ever will look at is
- * pClassesArray[0] ?
- */ )
- {
- const Reference<XIdlClass>& rxClass = pClassesArray[i];
- if( xDerivedFromClass->equals( rxClass ) )
- {
- // Treffer
+
+ while ( aClassesSeq.getLength() ) {
+ const Reference<XIdlClass>& rxClass = aClassesSeq.getConstArray()[0];
+
+ if ( xDerivedFromClass->equals( rxClass ) )
return sal_True;
- }
- else
- {
- // Rekursiv weitersuchen
- return isDerivedFrom( rxClass, xDerivedFromClass );
- }
+
+ aClassesSeq = rxClass->getSuperclasses();
}
+
return sal_False;
}
--
1.7.1
--------------050606080606010606040102--
More information about the LibreOffice
mailing list