1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/xul/templates/src/nsContentSupportMap.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,70 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsContentSupportMap_h__ 1.10 +#define nsContentSupportMap_h__ 1.11 + 1.12 +#include "pldhash.h" 1.13 +#include "nsTemplateMatch.h" 1.14 + 1.15 +/** 1.16 + * The nsContentSupportMap maintains a mapping from a "resource element" 1.17 + * in the content tree to the nsTemplateMatch that was used to instantiate it. This 1.18 + * is necessary to allow the XUL content to be built lazily. Specifically, 1.19 + * when building "resumes" on a partially-built content element, the builder 1.20 + * will walk upwards in the content tree to find the first element with an 1.21 + * 'id' attribute. This element is assumed to be the "resource element", 1.22 + * and allows the content builder to access the nsTemplateMatch (variable assignments 1.23 + * and rule information). 1.24 + */ 1.25 +class nsContentSupportMap { 1.26 +public: 1.27 + nsContentSupportMap() { Init(); } 1.28 + ~nsContentSupportMap() { Finish(); } 1.29 + 1.30 + nsresult Put(nsIContent* aElement, nsTemplateMatch* aMatch) { 1.31 + if (!mMap.ops) 1.32 + return NS_ERROR_NOT_INITIALIZED; 1.33 + 1.34 + PLDHashEntryHdr* hdr = PL_DHashTableOperate(&mMap, aElement, PL_DHASH_ADD); 1.35 + if (!hdr) 1.36 + return NS_ERROR_OUT_OF_MEMORY; 1.37 + 1.38 + Entry* entry = reinterpret_cast<Entry*>(hdr); 1.39 + NS_ASSERTION(entry->mMatch == nullptr, "over-writing entry"); 1.40 + entry->mContent = aElement; 1.41 + entry->mMatch = aMatch; 1.42 + return NS_OK; } 1.43 + 1.44 + bool Get(nsIContent* aElement, nsTemplateMatch** aMatch) { 1.45 + if (!mMap.ops) 1.46 + return false; 1.47 + 1.48 + PLDHashEntryHdr* hdr = PL_DHashTableOperate(&mMap, aElement, PL_DHASH_LOOKUP); 1.49 + if (PL_DHASH_ENTRY_IS_FREE(hdr)) 1.50 + return false; 1.51 + 1.52 + Entry* entry = reinterpret_cast<Entry*>(hdr); 1.53 + *aMatch = entry->mMatch; 1.54 + return true; } 1.55 + 1.56 + nsresult Remove(nsIContent* aElement); 1.57 + 1.58 + void Clear() { Finish(); Init(); } 1.59 + 1.60 +protected: 1.61 + PLDHashTable mMap; 1.62 + 1.63 + void Init(); 1.64 + void Finish(); 1.65 + 1.66 + struct Entry { 1.67 + PLDHashEntryHdr mHdr; 1.68 + nsIContent* mContent; 1.69 + nsTemplateMatch* mMatch; 1.70 + }; 1.71 +}; 1.72 + 1.73 +#endif