michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsTemplateMap_h__ michael@0: #define nsTemplateMap_h__ michael@0: michael@0: #include "pldhash.h" michael@0: #include "nsXULElement.h" michael@0: michael@0: class nsTemplateMap { michael@0: protected: michael@0: struct Entry { michael@0: PLDHashEntryHdr mHdr; michael@0: nsIContent* mContent; michael@0: nsIContent* mTemplate; michael@0: }; michael@0: michael@0: PLDHashTable mTable; michael@0: michael@0: void michael@0: Init() { PL_DHashTableInit(&mTable, PL_DHashGetStubOps(), nullptr, sizeof(Entry), PL_DHASH_MIN_SIZE); } michael@0: michael@0: void michael@0: Finish() { PL_DHashTableFinish(&mTable); } michael@0: michael@0: public: michael@0: nsTemplateMap() { Init(); } michael@0: michael@0: ~nsTemplateMap() { Finish(); } michael@0: michael@0: void michael@0: Put(nsIContent* aContent, nsIContent* aTemplate) { michael@0: NS_ASSERTION(PL_DHASH_ENTRY_IS_FREE(PL_DHashTableOperate(&mTable, aContent, PL_DHASH_LOOKUP)), michael@0: "aContent already in map"); michael@0: michael@0: Entry* entry = michael@0: reinterpret_cast(PL_DHashTableOperate(&mTable, aContent, PL_DHASH_ADD)); michael@0: michael@0: if (entry) { michael@0: entry->mContent = aContent; michael@0: entry->mTemplate = aTemplate; michael@0: } michael@0: } michael@0: michael@0: void michael@0: Remove(nsIContent* aContent) { michael@0: PL_DHashTableOperate(&mTable, aContent, PL_DHASH_REMOVE); michael@0: michael@0: for (nsIContent* child = aContent->GetFirstChild(); michael@0: child; michael@0: child = child->GetNextSibling()) { michael@0: Remove(child); michael@0: } michael@0: } michael@0: michael@0: michael@0: void michael@0: GetTemplateFor(nsIContent* aContent, nsIContent** aResult) { michael@0: Entry* entry = michael@0: reinterpret_cast(PL_DHashTableOperate(&mTable, aContent, PL_DHASH_LOOKUP)); michael@0: michael@0: if (PL_DHASH_ENTRY_IS_BUSY(&entry->mHdr)) michael@0: NS_IF_ADDREF(*aResult = entry->mTemplate); michael@0: else michael@0: *aResult = nullptr; michael@0: } michael@0: michael@0: void michael@0: Clear() { Finish(); Init(); } michael@0: }; michael@0: michael@0: #endif // nsTemplateMap_h__ michael@0: