nsTemplateMap.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsTemplateMap_h__
  6. #define nsTemplateMap_h__
  7. #include "PLDHashTable.h"
  8. #include "nsXULElement.h"
  9. using mozilla::fallible;
  10. class nsTemplateMap {
  11. protected:
  12. struct Entry : public PLDHashEntryHdr {
  13. nsIContent* mContent;
  14. nsIContent* mTemplate;
  15. };
  16. PLDHashTable mTable;
  17. public:
  18. nsTemplateMap() : mTable(PLDHashTable::StubOps(), sizeof(Entry)) { }
  19. ~nsTemplateMap() { }
  20. void
  21. Put(nsIContent* aContent, nsIContent* aTemplate) {
  22. NS_ASSERTION(!mTable.Search(aContent), "aContent already in map");
  23. auto entry = static_cast<Entry*>(mTable.Add(aContent, fallible));
  24. if (entry) {
  25. entry->mContent = aContent;
  26. entry->mTemplate = aTemplate;
  27. }
  28. }
  29. void
  30. Remove(nsIContent* aContent) {
  31. mTable.Remove(aContent);
  32. for (nsIContent* child = aContent->GetFirstChild();
  33. child;
  34. child = child->GetNextSibling()) {
  35. Remove(child);
  36. }
  37. }
  38. void
  39. GetTemplateFor(nsIContent* aContent, nsIContent** aResult) {
  40. auto entry = static_cast<Entry*>(mTable.Search(aContent));
  41. if (entry)
  42. NS_IF_ADDREF(*aResult = entry->mTemplate);
  43. else
  44. *aResult = nullptr;
  45. }
  46. void
  47. Clear() { mTable.Clear(); }
  48. };
  49. #endif // nsTemplateMap_h__