|
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 |
|
6 #ifndef nsTemplateMap_h__ |
|
7 #define nsTemplateMap_h__ |
|
8 |
|
9 #include "pldhash.h" |
|
10 #include "nsXULElement.h" |
|
11 |
|
12 class nsTemplateMap { |
|
13 protected: |
|
14 struct Entry { |
|
15 PLDHashEntryHdr mHdr; |
|
16 nsIContent* mContent; |
|
17 nsIContent* mTemplate; |
|
18 }; |
|
19 |
|
20 PLDHashTable mTable; |
|
21 |
|
22 void |
|
23 Init() { PL_DHashTableInit(&mTable, PL_DHashGetStubOps(), nullptr, sizeof(Entry), PL_DHASH_MIN_SIZE); } |
|
24 |
|
25 void |
|
26 Finish() { PL_DHashTableFinish(&mTable); } |
|
27 |
|
28 public: |
|
29 nsTemplateMap() { Init(); } |
|
30 |
|
31 ~nsTemplateMap() { Finish(); } |
|
32 |
|
33 void |
|
34 Put(nsIContent* aContent, nsIContent* aTemplate) { |
|
35 NS_ASSERTION(PL_DHASH_ENTRY_IS_FREE(PL_DHashTableOperate(&mTable, aContent, PL_DHASH_LOOKUP)), |
|
36 "aContent already in map"); |
|
37 |
|
38 Entry* entry = |
|
39 reinterpret_cast<Entry*>(PL_DHashTableOperate(&mTable, aContent, PL_DHASH_ADD)); |
|
40 |
|
41 if (entry) { |
|
42 entry->mContent = aContent; |
|
43 entry->mTemplate = aTemplate; |
|
44 } |
|
45 } |
|
46 |
|
47 void |
|
48 Remove(nsIContent* aContent) { |
|
49 PL_DHashTableOperate(&mTable, aContent, PL_DHASH_REMOVE); |
|
50 |
|
51 for (nsIContent* child = aContent->GetFirstChild(); |
|
52 child; |
|
53 child = child->GetNextSibling()) { |
|
54 Remove(child); |
|
55 } |
|
56 } |
|
57 |
|
58 |
|
59 void |
|
60 GetTemplateFor(nsIContent* aContent, nsIContent** aResult) { |
|
61 Entry* entry = |
|
62 reinterpret_cast<Entry*>(PL_DHashTableOperate(&mTable, aContent, PL_DHASH_LOOKUP)); |
|
63 |
|
64 if (PL_DHASH_ENTRY_IS_BUSY(&entry->mHdr)) |
|
65 NS_IF_ADDREF(*aResult = entry->mTemplate); |
|
66 else |
|
67 *aResult = nullptr; |
|
68 } |
|
69 |
|
70 void |
|
71 Clear() { Finish(); Init(); } |
|
72 }; |
|
73 |
|
74 #endif // nsTemplateMap_h__ |
|
75 |