Land RDF_19990426_BRANCH, which extracted some static C++ routines for manipulating RDF containers into bona fide interfaces. Also, fix a memory leak in the nsElementMap: be sure to hold a reference to the resource that is the key.

This commit is contained in:
waterson%netscape.com 1999-04-27 05:53:53 +00:00
Родитель 472f414c2b
Коммит 3f3dced3eb
2 изменённых файлов: 330 добавлений и 262 удалений

Просмотреть файл

@ -139,6 +139,7 @@ static NS_DEFINE_CID(kPresShellCID, NS_PRESSHELL_CID);
static NS_DEFINE_CID(kRDFCompositeDataSourceCID, NS_RDFCOMPOSITEDATASOURCE_CID);
static NS_DEFINE_CID(kRDFInMemoryDataSourceCID, NS_RDFINMEMORYDATASOURCE_CID);
static NS_DEFINE_CID(kLocalStoreCID, NS_LOCALSTORE_CID);
static NS_DEFINE_CID(kRDFContainerUtilsCID, NS_RDFCONTAINERUTILS_CID);
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
static NS_DEFINE_CID(kRDFXULBuilderCID, NS_RDFXULBUILDER_CID);
@ -188,23 +189,25 @@ private:
}
static PRIntn
ReleaseContentList(PLHashEntry* he, PRIntn index, void* closure)
{
ContentListItem* head =
(ContentListItem*) he->value;
while (head) {
ContentListItem* doomed = head;
head = head->mNext;
delete doomed;
}
return HT_ENUMERATE_NEXT;
}
ReleaseContentList(PLHashEntry* he, PRIntn index, void* closure);
public:
nsElementMap(void)
{
nsElementMap(void);
virtual ~nsElementMap();
nsresult
Add(nsIRDFResource* aResource, nsIContent* aContent);
nsresult
Remove(nsIRDFResource* aResource, nsIContent* aContent);
nsresult
Find(nsIRDFResource* aResource, nsISupportsArray* aResults);
};
nsElementMap::nsElementMap()
{
// Create a table for mapping RDF resources to elements in the
// content tree.
static PRInt32 kInitialResourceTableSize = 1023;
@ -216,19 +219,41 @@ public:
nsnull)) == nsnull) {
NS_ERROR("could not create hash table for resources");
}
}
}
virtual ~nsElementMap()
{
nsElementMap::~nsElementMap()
{
if (mResources) {
PL_HashTableEnumerateEntries(mResources, ReleaseContentList, nsnull);
PL_HashTableDestroy(mResources);
}
}
PRIntn
nsElementMap::ReleaseContentList(PLHashEntry* he, PRIntn index, void* closure)
{
nsIRDFResource* resource =
NS_REINTERPRET_CAST(nsIRDFResource*, NS_CONST_CAST(void*, he->key));
NS_RELEASE(resource);
ContentListItem* head =
(ContentListItem*) he->value;
while (head) {
ContentListItem* doomed = head;
head = head->mNext;
delete doomed;
}
nsresult
Add(nsIRDFResource* aResource, nsIContent* aContent)
{
return HT_ENUMERATE_NEXT;
}
nsresult
nsElementMap::Add(nsIRDFResource* aResource, nsIContent* aContent)
{
NS_PRECONDITION(mResources != nsnull, "not initialized");
if (! mResources)
return NS_ERROR_NOT_INITIALIZED;
@ -242,6 +267,7 @@ public:
return NS_ERROR_OUT_OF_MEMORY;
PL_HashTableAdd(mResources, aResource, head);
NS_ADDREF(aResource);
}
else {
while (1) {
@ -261,11 +287,12 @@ public:
}
return NS_OK;
}
}
nsresult
Remove(nsIRDFResource* aResource, nsIContent* aContent)
{
nsresult
nsElementMap::Remove(nsIRDFResource* aResource, nsIContent* aContent)
{
NS_PRECONDITION(mResources != nsnull, "not initialized");
if (! mResources)
return NS_ERROR_NOT_INITIALIZED;
@ -280,7 +307,9 @@ public:
PL_HashTableAdd(mResources, aResource, newHead);
}
else {
// It was the last reference in the table
PL_HashTableRemove(mResources, aResource);
NS_RELEASE(aResource);
}
delete head;
return NS_OK;
@ -305,11 +334,13 @@ public:
// waterson@netscape.com.
NS_ERROR("attempt to remove an element that was never added");
return NS_ERROR_ILLEGAL_VALUE;
}
}
nsresult
Find(nsIRDFResource* aResource, nsISupportsArray* aResults)
{
nsresult
nsElementMap::Find(nsIRDFResource* aResource, nsISupportsArray* aResults)
{
NS_PRECONDITION(mResources != nsnull, "not initialized");
if (! mResources)
return NS_ERROR_NOT_INITIALIZED;
@ -323,9 +354,7 @@ public:
head = head->mNext;
}
return NS_OK;
}
};
}
////////////////////////////////////////////////////////////////////////
@ -2434,9 +2463,14 @@ XULDocumentImpl::CreateElement(const nsString& aTagName, nsIDOMElement** aReturn
NS_ASSERTION(rv == NS_OK, "unable to mark as XUL element");
if (NS_FAILED(rv)) return rv;
rv = rdf_MakeSeq(mDocumentDataSource, resource);
{
NS_WITH_SERVICE(nsIRDFContainerUtils, rdfc, kRDFContainerUtilsCID, &rv);
if (NS_FAILED(rv)) return rv;
rv = rdfc->MakeSeq(mDocumentDataSource, resource, nsnull);
NS_ASSERTION(rv == NS_OK, "unable to mark as XUL element");
if (NS_FAILED(rv)) return rv;
}
// `this' will be its document
rv = result->SetDocument(this, PR_FALSE);

Просмотреть файл

@ -139,6 +139,7 @@ static NS_DEFINE_CID(kPresShellCID, NS_PRESSHELL_CID);
static NS_DEFINE_CID(kRDFCompositeDataSourceCID, NS_RDFCOMPOSITEDATASOURCE_CID);
static NS_DEFINE_CID(kRDFInMemoryDataSourceCID, NS_RDFINMEMORYDATASOURCE_CID);
static NS_DEFINE_CID(kLocalStoreCID, NS_LOCALSTORE_CID);
static NS_DEFINE_CID(kRDFContainerUtilsCID, NS_RDFCONTAINERUTILS_CID);
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
static NS_DEFINE_CID(kRDFXULBuilderCID, NS_RDFXULBUILDER_CID);
@ -188,23 +189,25 @@ private:
}
static PRIntn
ReleaseContentList(PLHashEntry* he, PRIntn index, void* closure)
{
ContentListItem* head =
(ContentListItem*) he->value;
while (head) {
ContentListItem* doomed = head;
head = head->mNext;
delete doomed;
}
return HT_ENUMERATE_NEXT;
}
ReleaseContentList(PLHashEntry* he, PRIntn index, void* closure);
public:
nsElementMap(void)
{
nsElementMap(void);
virtual ~nsElementMap();
nsresult
Add(nsIRDFResource* aResource, nsIContent* aContent);
nsresult
Remove(nsIRDFResource* aResource, nsIContent* aContent);
nsresult
Find(nsIRDFResource* aResource, nsISupportsArray* aResults);
};
nsElementMap::nsElementMap()
{
// Create a table for mapping RDF resources to elements in the
// content tree.
static PRInt32 kInitialResourceTableSize = 1023;
@ -216,19 +219,41 @@ public:
nsnull)) == nsnull) {
NS_ERROR("could not create hash table for resources");
}
}
}
virtual ~nsElementMap()
{
nsElementMap::~nsElementMap()
{
if (mResources) {
PL_HashTableEnumerateEntries(mResources, ReleaseContentList, nsnull);
PL_HashTableDestroy(mResources);
}
}
PRIntn
nsElementMap::ReleaseContentList(PLHashEntry* he, PRIntn index, void* closure)
{
nsIRDFResource* resource =
NS_REINTERPRET_CAST(nsIRDFResource*, NS_CONST_CAST(void*, he->key));
NS_RELEASE(resource);
ContentListItem* head =
(ContentListItem*) he->value;
while (head) {
ContentListItem* doomed = head;
head = head->mNext;
delete doomed;
}
nsresult
Add(nsIRDFResource* aResource, nsIContent* aContent)
{
return HT_ENUMERATE_NEXT;
}
nsresult
nsElementMap::Add(nsIRDFResource* aResource, nsIContent* aContent)
{
NS_PRECONDITION(mResources != nsnull, "not initialized");
if (! mResources)
return NS_ERROR_NOT_INITIALIZED;
@ -242,6 +267,7 @@ public:
return NS_ERROR_OUT_OF_MEMORY;
PL_HashTableAdd(mResources, aResource, head);
NS_ADDREF(aResource);
}
else {
while (1) {
@ -261,11 +287,12 @@ public:
}
return NS_OK;
}
}
nsresult
Remove(nsIRDFResource* aResource, nsIContent* aContent)
{
nsresult
nsElementMap::Remove(nsIRDFResource* aResource, nsIContent* aContent)
{
NS_PRECONDITION(mResources != nsnull, "not initialized");
if (! mResources)
return NS_ERROR_NOT_INITIALIZED;
@ -280,7 +307,9 @@ public:
PL_HashTableAdd(mResources, aResource, newHead);
}
else {
// It was the last reference in the table
PL_HashTableRemove(mResources, aResource);
NS_RELEASE(aResource);
}
delete head;
return NS_OK;
@ -305,11 +334,13 @@ public:
// waterson@netscape.com.
NS_ERROR("attempt to remove an element that was never added");
return NS_ERROR_ILLEGAL_VALUE;
}
}
nsresult
Find(nsIRDFResource* aResource, nsISupportsArray* aResults)
{
nsresult
nsElementMap::Find(nsIRDFResource* aResource, nsISupportsArray* aResults)
{
NS_PRECONDITION(mResources != nsnull, "not initialized");
if (! mResources)
return NS_ERROR_NOT_INITIALIZED;
@ -323,9 +354,7 @@ public:
head = head->mNext;
}
return NS_OK;
}
};
}
////////////////////////////////////////////////////////////////////////
@ -2434,9 +2463,14 @@ XULDocumentImpl::CreateElement(const nsString& aTagName, nsIDOMElement** aReturn
NS_ASSERTION(rv == NS_OK, "unable to mark as XUL element");
if (NS_FAILED(rv)) return rv;
rv = rdf_MakeSeq(mDocumentDataSource, resource);
{
NS_WITH_SERVICE(nsIRDFContainerUtils, rdfc, kRDFContainerUtilsCID, &rv);
if (NS_FAILED(rv)) return rv;
rv = rdfc->MakeSeq(mDocumentDataSource, resource, nsnull);
NS_ASSERTION(rv == NS_OK, "unable to mark as XUL element");
if (NS_FAILED(rv)) return rv;
}
// `this' will be its document
rv = result->SetDocument(this, PR_FALSE);