content/xul/templates/src/nsContentSupportMap.h

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:827599cddf95
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 nsContentSupportMap_h__
7 #define nsContentSupportMap_h__
8
9 #include "pldhash.h"
10 #include "nsTemplateMatch.h"
11
12 /**
13 * The nsContentSupportMap maintains a mapping from a "resource element"
14 * in the content tree to the nsTemplateMatch that was used to instantiate it. This
15 * is necessary to allow the XUL content to be built lazily. Specifically,
16 * when building "resumes" on a partially-built content element, the builder
17 * will walk upwards in the content tree to find the first element with an
18 * 'id' attribute. This element is assumed to be the "resource element",
19 * and allows the content builder to access the nsTemplateMatch (variable assignments
20 * and rule information).
21 */
22 class nsContentSupportMap {
23 public:
24 nsContentSupportMap() { Init(); }
25 ~nsContentSupportMap() { Finish(); }
26
27 nsresult Put(nsIContent* aElement, nsTemplateMatch* aMatch) {
28 if (!mMap.ops)
29 return NS_ERROR_NOT_INITIALIZED;
30
31 PLDHashEntryHdr* hdr = PL_DHashTableOperate(&mMap, aElement, PL_DHASH_ADD);
32 if (!hdr)
33 return NS_ERROR_OUT_OF_MEMORY;
34
35 Entry* entry = reinterpret_cast<Entry*>(hdr);
36 NS_ASSERTION(entry->mMatch == nullptr, "over-writing entry");
37 entry->mContent = aElement;
38 entry->mMatch = aMatch;
39 return NS_OK; }
40
41 bool Get(nsIContent* aElement, nsTemplateMatch** aMatch) {
42 if (!mMap.ops)
43 return false;
44
45 PLDHashEntryHdr* hdr = PL_DHashTableOperate(&mMap, aElement, PL_DHASH_LOOKUP);
46 if (PL_DHASH_ENTRY_IS_FREE(hdr))
47 return false;
48
49 Entry* entry = reinterpret_cast<Entry*>(hdr);
50 *aMatch = entry->mMatch;
51 return true; }
52
53 nsresult Remove(nsIContent* aElement);
54
55 void Clear() { Finish(); Init(); }
56
57 protected:
58 PLDHashTable mMap;
59
60 void Init();
61 void Finish();
62
63 struct Entry {
64 PLDHashEntryHdr mHdr;
65 nsIContent* mContent;
66 nsTemplateMatch* mMatch;
67 };
68 };
69
70 #endif

mercurial