Use nsCOMArray instead of nsIArray in nsIXFormsControl and get rid of nsXFormsMDGSet. Bug 278369, r=smaug/doronr

This commit is contained in:
allan%beaufour.dk 2005-02-24 14:35:27 +00:00
parent b27dd1520a
commit 7d3aed3043
12 changed files with 126 additions and 115 deletions

View File

@ -105,7 +105,6 @@ CPPSRCS = \
nsXFormsAtoms.cpp \
nsXFormsModule.cpp \
nsXFormsMDGEngine.cpp \
nsXFormsMDGSet.cpp \
nsXFormsXPathAnalyzer.cpp \
nsXFormsXPathNode.cpp \
nsXFormsXPathParser.cpp \

View File

@ -38,9 +38,13 @@
#include "nsIXFormsContextControl.idl"
interface nsIArray;
interface nsIDOMNode;
interface nsIDOMElement;
%{C++
#include "nsCOMArray.h"
%}
[ptr] native nsCOMArrayPtr(nsCOMArray<nsIDOMNode>);
/**
* Interface implemented by all XForms form control classes.
@ -79,11 +83,8 @@ interface nsIXFormsControl : nsIXFormsContextControl
* nodes, all @owned attributes on /share nodes, and all /me nodes.
*
* @note The array MUST be sorted, pointer-order, ascending.
*
* @bug Should use nsCOMArray
* @see https://bugzilla.mozilla.org/show_bug.cgi?id=278369
*/
readonly attribute nsIArray dependencies;
readonly attribute nsCOMArrayPtr dependencies;
/**
* Get the element that should receive events for the control.

View File

@ -92,9 +92,10 @@ nsXFormsControlStub::GetBoundNode(nsIDOMNode **aBoundNode)
}
NS_IMETHODIMP
nsXFormsControlStub::GetDependencies(nsIArray **aDependencies)
nsXFormsControlStub::GetDependencies(nsCOMArray<nsIDOMNode> **aDependencies)
{
NS_IF_ADDREF(*aDependencies = mDependencies);
if (aDependencies)
*aDependencies = &mDependencies;
return NS_OK;
}
@ -153,15 +154,9 @@ nsXFormsControlStub::ProcessNodeBinding(const nsString &aBindingAttr,
nsIDOMXPathResult **aResult,
nsIModelElementPrivate **aModel)
{
nsresult rv;
mDependencies.Clear();
if (!mDependencies) {
rv = NS_NewArray(getter_AddRefs(mDependencies));
} else {
rv = mDependencies->Clear();
}
NS_ENSURE_SUCCESS(rv, rv);
nsresult rv;
rv = nsXFormsUtils::EvaluateNodeBinding(mElement,
kElementFlags,
aBindingAttr,
@ -169,7 +164,7 @@ nsXFormsControlStub::ProcessNodeBinding(const nsString &aBindingAttr,
aResultType,
getter_AddRefs(mModel),
aResult,
mDependencies);
&mDependencies);
if (mModel) {
mModel->AddFormControl(this);

View File

@ -39,7 +39,6 @@
#ifndef nsXFormsControlStub_h_
#define nsXFormsControlStub_h_
#include "nsArray.h"
#include "nsCOMPtr.h"
#include "nsString.h"
@ -86,7 +85,7 @@ public:
// nsIXFormsControl
NS_IMETHOD GetBoundNode(nsIDOMNode **aBoundNode);
NS_IMETHOD GetDependencies(nsIArray **aDependencies);
NS_IMETHOD GetDependencies(nsCOMArray<nsIDOMNode> **aDependencies);
NS_IMETHOD GetElement(nsIDOMElement **aElement);
NS_IMETHOD ResetBoundNode();
NS_IMETHOD Bind();
@ -126,7 +125,7 @@ protected:
nsCOMPtr<nsIDOMNode> mBoundNode;
/** Array of nsIDOMNodes that the control depends on. */
nsCOMPtr<nsIMutableArray> mDependencies;
nsCOMArray<nsIDOMNode> mDependencies;
/** The model for the control */
nsCOMPtr<nsIModelElementPrivate> mModel;

View File

@ -46,6 +46,7 @@
#include "nsIDOMXPathResult.h"
#include "nsDeque.h"
#include "nsIModelElementPrivate.h"
#include "nsXFormsUtils.h"
#ifdef DEBUG
//# define DEBUG_XF_MDG
@ -149,13 +150,13 @@ nsXFormsMDGEngine::Init(nsIModelElementPrivate *aModel)
}
nsresult
nsXFormsMDGEngine::AddMIP(ModelItemPropName aType,
nsIDOMXPathExpression *aExpression,
nsXFormsMDGSet *aDependencies,
PRBool aDynFunc,
nsIDOMNode *aContextNode,
PRInt32 aContextPos,
PRInt32 aContextSize)
nsXFormsMDGEngine::AddMIP(ModelItemPropName aType,
nsIDOMXPathExpression *aExpression,
nsCOMArray<nsIDOMNode> *aDependencies,
PRBool aDynFunc,
nsIDOMNode *aContextNode,
PRInt32 aContextPos,
PRInt32 aContextSize)
{
NS_ENSURE_ARG(aContextNode);
@ -193,7 +194,7 @@ nsXFormsMDGEngine::AddMIP(ModelItemPropName aType,
nsCOMPtr<nsIDOMNode> dep_domnode;
nsXFormsMDGNode* dep_gnode;
for (PRInt32 i = 0; i < aDependencies->Count(); ++i) {
dep_domnode = aDependencies->GetNode(i);
dep_domnode = aDependencies->ObjectAt(i);
if (!dep_domnode) {
return NS_ERROR_NULL_POINTER;
}
@ -241,9 +242,7 @@ nsXFormsMDGEngine::MarkNodeAsChanged(nsIDOMNode* aContextNode)
NS_ENSURE_TRUE(mGraph.AppendElement(n), NS_ERROR_OUT_OF_MEMORY);
}
NS_ENSURE_TRUE(mMarkedNodes.AddNode(aContextNode), NS_ERROR_FAILURE);
return NS_OK;
return mMarkedNodes.AppendObject(aContextNode);
}
#ifdef DEBUG_beaufour
@ -292,7 +291,7 @@ nsXFormsMDGEngine::PrintDot(const char* aFile)
#endif
nsresult
nsXFormsMDGEngine::Recalculate(nsXFormsMDGSet* aChangedNodes)
nsXFormsMDGEngine::Recalculate(nsCOMArray<nsIDOMNode> *aChangedNodes)
{
NS_ENSURE_ARG(aChangedNodes);
@ -301,7 +300,7 @@ nsXFormsMDGEngine::Recalculate(nsXFormsMDGSet* aChangedNodes)
aChangedNodes->Count());
#endif
NS_ENSURE_TRUE(aChangedNodes->AddSet(mMarkedNodes), NS_ERROR_OUT_OF_MEMORY);
NS_ENSURE_TRUE(aChangedNodes->AppendObjects(mMarkedNodes), NS_ERROR_OUT_OF_MEMORY);
mMarkedNodes.Clear();
@ -372,7 +371,7 @@ nsXFormsMDGEngine::Recalculate(nsXFormsMDGSet* aChangedNodes)
PR_FALSE,
PR_TRUE);
if (NS_SUCCEEDED(rv)) {
NS_ENSURE_TRUE(aChangedNodes->AddNode(g->mContextNode),
NS_ENSURE_TRUE(aChangedNodes->AppendObject(g->mContextNode),
NS_ERROR_FAILURE);
}
}
@ -393,7 +392,7 @@ nsXFormsMDGEngine::Recalculate(nsXFormsMDGSet* aChangedNodes)
if (ns->IsConstraint() != constraint) {
ns->Set(eFlag_CONSTRAINT, constraint);
ns->Set(eFlag_DISPATCH_VALID_CHANGED, PR_TRUE);
NS_ENSURE_TRUE(aChangedNodes->AddNode(g->mContextNode),
NS_ENSURE_TRUE(aChangedNodes->AppendObject(g->mContextNode),
NS_ERROR_FAILURE);
}
@ -431,7 +430,7 @@ nsXFormsMDGEngine::Recalculate(nsXFormsMDGSet* aChangedNodes)
NS_ENSURE_SUCCESS(rv, rv);
if (didChange) {
NS_ENSURE_TRUE(aChangedNodes->AddNode(g->mContextNode),
NS_ENSURE_TRUE(aChangedNodes->AppendObject(g->mContextNode),
NS_ERROR_FAILURE);
}
}
@ -456,7 +455,7 @@ nsXFormsMDGEngine::Recalculate(nsXFormsMDGSet* aChangedNodes)
g->MarkClean();
}
aChangedNodes->MakeUnique();
nsXFormsUtils::MakeUniqueAndSort(aChangedNodes);
#ifdef DEBUG_XF_MDG
printf("\taChangedNodes: %d\n", aChangedNodes->Count());
@ -950,11 +949,11 @@ nsXFormsMDGEngine::ComputeMIP(eFlag_t aStateFlag,
}
nsresult
nsXFormsMDGEngine::ComputeMIPWithInheritance(eFlag_t aStateFlag,
eFlag_t aDispatchFlag,
eFlag_t aInheritanceFlag,
nsXFormsMDGNode *aNode,
nsXFormsMDGSet *aSet)
nsXFormsMDGEngine::ComputeMIPWithInheritance(eFlag_t aStateFlag,
eFlag_t aDispatchFlag,
eFlag_t aInheritanceFlag,
nsXFormsMDGNode *aNode,
nsCOMArray<nsIDOMNode> *aSet)
{
nsresult rv;
PRBool didChange;
@ -967,7 +966,7 @@ nsXFormsMDGEngine::ComputeMIPWithInheritance(eFlag_t aStateFlag,
if ( !(aStateFlag == eFlag_READONLY && ns->Test(aInheritanceFlag))
|| (aStateFlag == eFlag_RELEVANT && ns->Test(aInheritanceFlag)) )
{
NS_ENSURE_TRUE(aSet->AddNode(aNode->mContextNode),
NS_ENSURE_TRUE(aSet->AppendObject(aNode->mContextNode),
NS_ERROR_FAILURE);
rv = AttachInheritance(aSet,
aNode->mContextNode,
@ -980,10 +979,10 @@ nsXFormsMDGEngine::ComputeMIPWithInheritance(eFlag_t aStateFlag,
}
nsresult
nsXFormsMDGEngine::AttachInheritance(nsXFormsMDGSet *aSet,
nsIDOMNode *aSrc,
PRBool aState,
eFlag_t aStateFlag)
nsXFormsMDGEngine::AttachInheritance(nsCOMArray<nsIDOMNode> *aSet,
nsIDOMNode *aSrc,
PRBool aState,
eFlag_t aStateFlag)
{
NS_ENSURE_ARG(aSrc);
@ -1043,7 +1042,7 @@ nsXFormsMDGEngine::AttachInheritance(nsXFormsMDGSet *aSet,
if (updateNode) {
rv = AttachInheritance(aSet, node, aState, aStateFlag);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(aSet->AddNode(node), NS_ERROR_FAILURE);
NS_ENSURE_TRUE(aSet->AppendObject(node), NS_ERROR_FAILURE);
updateNode = PR_FALSE;
}
}

View File

@ -42,13 +42,13 @@
#include "nscore.h"
#include "nsClassHashtable.h"
#include "nsCOMArray.h"
#include "nsCOMPtr.h"
#include "nsVoidArray.h"
#include "nsIDOMNode.h"
#include "nsXFormsTypes.h"
#include "nsXFormsMDGSet.h"
#include "nsXFormsNodeState.h"
#include "nsIModelElementPrivate.h"
@ -177,10 +177,9 @@ protected:
nsIModelElementPrivate *mModel;
/**
* Set of nodes that are marked as changed, and should be included in
* recalculation
* Nodes that are marked as changed, and should be included in recalculation
*/
nsXFormsMDGSet mMarkedNodes;
nsCOMArray<nsIDOMNode> mMarkedNodes;
/** Number of nodes in the graph */
PRInt32 mNodesInGraph;
@ -289,11 +288,11 @@ protected:
* @param aNode The context node
* @param aSet Set of the nodes influenced by operation
*/
nsresult ComputeMIPWithInheritance(eFlag_t aStateFlag,
eFlag_t aDispatchFlag,
eFlag_t aInheritanceFlag,
nsXFormsMDGNode *aNode,
nsXFormsMDGSet *aSet);
nsresult ComputeMIPWithInheritance(eFlag_t aStateFlag,
eFlag_t aDispatchFlag,
eFlag_t aInheritanceFlag,
nsXFormsMDGNode *aNode,
nsCOMArray<nsIDOMNode> *aSet);
/**
* Attaches inheritance to all children of a given node
@ -303,10 +302,10 @@ protected:
* @param aState The state of the flag
* @param aStateFlag The flag
*/
nsresult AttachInheritance(nsXFormsMDGSet *aSet,
nsIDOMNode *aSrc,
PRBool aState,
eFlag_t aStateFlag);
nsresult AttachInheritance(nsCOMArray<nsIDOMNode> *aSet,
nsIDOMNode *aSrc,
PRBool aState,
eFlag_t aStateFlag);
/**
* Invalidate the information, ie. mark all nodes as dirty.
@ -356,13 +355,13 @@ public:
* @param aContextPos The context positions of aExpression
* @param aContextSize The context size for aExpression
*/
nsresult AddMIP(ModelItemPropName aType,
nsIDOMXPathExpression *aExpression,
nsXFormsMDGSet *aDependencies,
PRBool aDynFunc,
nsIDOMNode *aContextNode,
PRInt32 aContextPos,
PRInt32 aContextSize);
nsresult AddMIP(ModelItemPropName aType,
nsIDOMXPathExpression *aExpression,
nsCOMArray<nsIDOMNode> *aDependencies,
PRBool aDynFunc,
nsIDOMNode *aContextNode,
PRInt32 aContextPos,
PRInt32 aContextSize);
/**
* Recalculate the MDG.
@ -371,7 +370,7 @@ public:
*
* @note aChangedNodes are unique and sorted in pointer-order, ascending.
*/
nsresult Recalculate(nsXFormsMDGSet *aChangedNodes);
nsresult Recalculate(nsCOMArray<nsIDOMNode> *aChangedNodes);
/**
* Rebuilds the MDG.

View File

@ -573,12 +573,9 @@ nsXFormsModelElement::Revalidate()
control->GetBoundNode(getter_AddRefs(boundNode));
// Get dependencies
nsCOMPtr<nsIArray> deps;
control->GetDependencies(getter_AddRefs(deps));
PRUint32 depCount = 0;
if (deps) {
deps->GetLength(&depCount);
}
nsCOMArray<nsIDOMNode> *deps = nsnull;
control->GetDependencies(&deps);
PRUint32 depCount = deps ? deps->Count() : 0;
#ifdef DEBUG_MODEL
nsCOMPtr<nsIDOMElement> controlElement;
@ -604,7 +601,7 @@ nsXFormsModelElement::Revalidate()
PRBool refresh = PR_FALSE;
for (PRInt32 j = 0; j < mChangedNodes.Count(); ++j) {
curChanged = mChangedNodes.GetNode(j);
curChanged = mChangedNodes[j];
if (curChanged == boundNode) {
refresh = PR_TRUE;
@ -617,7 +614,7 @@ nsXFormsModelElement::Revalidate()
}
while (depPos < depCount && (void*) curChanged > (void*) curDep) {
curDep = do_QueryElementAt(deps, depPos);
curDep = deps->ObjectAt(depPos);
++depPos;
}
@ -1196,7 +1193,7 @@ nsXFormsModelElement::ProcessBind(nsIXFormsXPathEvaluator *aEvaluator,
// Iterate over resultset
nsXFormsMDGSet set;
nsCOMArray<nsIDOMNode> deps;
nsCOMPtr<nsIDOMNode> node;
PRUint32 snapItem;
for (snapItem = 0; snapItem < snapLen; ++snapItem) {
@ -1265,14 +1262,14 @@ nsXFormsModelElement::ProcessBind(nsIXFormsXPathEvaluator *aEvaluator,
// Get node dependencies
nsAutoPtr<nsXFormsXPathNode> xNode(parser.Parse(propStrings[j]));
set.Clear();
rv = analyzer.Analyze(node, xNode, expr, &propStrings[j], &set);
deps.Clear();
rv = analyzer.Analyze(node, xNode, expr, &propStrings[j], &deps);
NS_ENSURE_SUCCESS(rv, rv);
// Insert into MDG
rv = mMDG.AddMIP((ModelItemPropName) j,
expr,
&set,
&deps,
parser.UsesDynamicFunc(),
node,
snapItem + 1,

View File

@ -149,7 +149,7 @@ private:
* List of changed nodes, ie. nodes that have not been informed about
* changes yet
*/
nsXFormsMDGSet mChangedNodes;
nsCOMArray<nsIDOMNode> mChangedNodes;
/* Controls that need to a Refresh() */
nsVoidArray mControlsNeedingRefresh;

View File

@ -66,7 +66,6 @@
#include "nsXFormsXPathAnalyzer.h"
#include "nsXFormsXPathParser.h"
#include "nsXFormsXPathNode.h"
#include "nsXFormsMDGSet.h"
#include "nsIDOMXPathExpression.h"
#include "nsArray.h"
@ -338,13 +337,13 @@ nsXFormsUtils::GetModel(nsIDOMElement *aElement,
}
/* static */ already_AddRefed<nsIDOMXPathResult>
nsXFormsUtils::EvaluateXPath(const nsAString &aExpression,
nsIDOMNode *aContextNode,
nsIDOMNode *aResolverNode,
PRUint16 aResultType,
PRInt32 aContextPosition,
PRInt32 aContextSize,
nsXFormsMDGSet *aSet)
nsXFormsUtils::EvaluateXPath(const nsAString &aExpression,
nsIDOMNode *aContextNode,
nsIDOMNode *aResolverNode,
PRUint16 aResultType,
PRInt32 aContextPosition,
PRInt32 aContextSize,
nsCOMArray<nsIDOMNode> *aSet)
{
nsCOMPtr<nsIDOMDocument> doc;
aContextNode->GetOwnerDocument(getter_AddRefs(doc));
@ -435,7 +434,7 @@ nsXFormsUtils::EvaluateNodeBinding(nsIDOMElement *aElement,
PRUint16 aResultType,
nsIModelElementPrivate **aModel,
nsIDOMXPathResult **aResult,
nsIMutableArray *aDeps)
nsCOMArray<nsIDOMNode> *aDeps)
{
if (!aElement || !aModel || !aResult) {
return NS_OK;
@ -507,20 +506,13 @@ nsXFormsUtils::EvaluateNodeBinding(nsIDOMElement *aElement,
}
// Evaluate |expr|
nsXFormsMDGSet set;
nsCOMPtr<nsIDOMXPathResult> res = EvaluateXPath(expr,
contextNode,
aElement,
aResultType,
contextSize,
contextPosition,
aDeps ? &set : nsnull);
if (res && aDeps) {
for (PRInt32 i = 0; i < set.Count(); ++i) {
aDeps->AppendElement(set.GetNode(i), PR_FALSE);
}
}
aDeps);
res.swap(*aResult); // exchanges ref
@ -961,6 +953,30 @@ nsXFormsUtils::FocusControl(nsIDOMElement *aElement)
return ret;
}
int
sortFunc(nsIDOMNode *aNode1, nsIDOMNode *aNode2, void *aArg)
{
return (void*) aNode1 < (void*) aNode2;
}
/* static */ void
nsXFormsUtils::MakeUniqueAndSort(nsCOMArray<nsIDOMNode> *aArray)
{
if (!aArray)
return;
aArray->Sort(sortFunc, nsnull);
PRInt32 pos = 0;
while (pos + 1 < aArray->Count()) {
if (aArray->ObjectAt(pos) == aArray->ObjectAt(pos + 1)) {
aArray->RemoveObjectAt(pos + 1);
} else {
++pos;
}
}
}
/* static */ nsresult
nsXFormsUtils::GetInstanceNodeForData(nsIDOMNode *aInstanceDataNode,
nsIModelElementPrivate *aModel,

View File

@ -42,15 +42,15 @@
#include "prtypes.h"
#include "nsCOMPtr.h"
#include "nsCOMArray.h"
#include "nsIDOMNode.h"
#include "nsIDOMXPathResult.h"
#include "nsIModelElementPrivate.h"
class nsIDOMNode;
class nsIDOMElement;
class nsIXFormsModelElement;
class nsIURI;
class nsString;
class nsXFormsMDGSet;
class nsIMutableArray;
#define NS_NAMESPACE_XFORMS "http://www.w3.org/2002/xforms"
@ -209,7 +209,7 @@ public:
PRUint16 aResultType,
nsIModelElementPrivate **aModel,
nsIDOMXPathResult **aResult,
nsIMutableArray *aDeps = nsnull);
nsCOMArray<nsIDOMNode> *aDeps = nsnull);
/**
* Given a bind element |aBindElement|, find the model and the context node
@ -228,13 +228,13 @@ public:
* namespace resolver, and result type.
*/
static NS_HIDDEN_(already_AddRefed<nsIDOMXPathResult>)
EvaluateXPath(const nsAString &aExpression,
nsIDOMNode *aContextNode,
nsIDOMNode *aResolverNode,
PRUint16 aResultType,
PRInt32 aContextPosition = 1,
PRInt32 aContextSize = 1,
nsXFormsMDGSet *aSet = nsnull);
EvaluateXPath(const nsAString &aExpression,
nsIDOMNode *aContextNode,
nsIDOMNode *aResolverNode,
PRUint16 aResultType,
PRInt32 aContextPosition = 1,
PRInt32 aContextSize = 1,
nsCOMArray<nsIDOMNode> *aSet = nsnull);
/**
* Given a node in the instance data, get its string value according
@ -330,6 +330,11 @@ public:
*/
static NS_HIDDEN_(PRBool) FocusControl(nsIDOMElement *aElement);
/**
* Sorts the array and removes duplicate entries
*/
static NS_HIDDEN_(void) MakeUniqueAndSort(nsCOMArray<nsIDOMNode> *aArray);
/**
* Returns the <xf:instance> for a given instance data node.
*/

View File

@ -39,6 +39,7 @@
#include "nsXFormsXPathAnalyzer.h"
#include "nsIDOMXPathResult.h"
#include "nsXFormsUtils.h"
#ifdef DEBUG
//#define DEBUG_XF_ANALYZER
@ -65,7 +66,7 @@ nsXFormsXPathAnalyzer::Analyze(nsIDOMNode *aContextNode,
const nsXFormsXPathNode *aNode,
nsIDOMXPathExpression *aExpression,
const nsAString *aExprString,
nsXFormsMDGSet *aSet)
nsCOMArray<nsIDOMNode> *aSet)
{
NS_ENSURE_ARG(aContextNode);
NS_ENSURE_ARG(aNode);
@ -90,7 +91,7 @@ nsXFormsXPathAnalyzer::Analyze(nsIDOMNode *aContextNode,
NS_ENSURE_SUCCESS(rv, rv);
// Besides making the set a set, it also makes it sorted.
aSet->MakeUnique();
nsXFormsUtils::MakeUniqueAndSort(aSet);
return NS_OK;
}
@ -196,7 +197,7 @@ nsXFormsXPathAnalyzer::AnalyzeRecursively(nsIDOMNode *aContextNode,
#ifdef DEBUG_XF_ANALYZER
printf("collecting '%s'\n", NS_ConvertUCS2toUTF8(xp).get());
#endif
mCurSet->AddNode(node);
mCurSet->AppendObject(node);
}
rv = AnalyzeRecursively(node, aNode->mChild, aIndent + 1);
}

View File

@ -39,11 +39,11 @@
#include "nscore.h"
#include "nsCOMPtr.h"
#include "nsCOMArray.h"
#include "nsXFormsXPathNode.h"
#include "nsIDOMXPathExpression.h"
#include "nsIXFormsXPathEvaluator.h"
#include "nsIDOMXPathNSResolver.h"
#include "nsXFormsMDGSet.h"
#include "nsIDOMNode.h"
#include "nsCOMPtr.h"
#include "nsString.h"
@ -60,7 +60,7 @@ private:
nsCOMPtr<nsIXFormsXPathEvaluator> mEvaluator;
nsCOMPtr<nsIDOMNode> mResolver;
nsXFormsMDGSet *mCurSet;
nsCOMArray<nsIDOMNode> *mCurSet;
nsCOMPtr<nsIDOMXPathExpression> mCurExpression;
const nsAString *mCurExprString;
@ -77,5 +77,5 @@ public:
const nsXFormsXPathNode *aNode,
nsIDOMXPathExpression *aExpression,
const nsAString *aExprString,
nsXFormsMDGSet *aSet);
nsCOMArray<nsIDOMNode> *aSet);
};