content/xul/templates/src/nsTemplateMap.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/xul/templates/src/nsTemplateMap.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,75 @@
     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 nsTemplateMap_h__
    1.10 +#define nsTemplateMap_h__
    1.11 +
    1.12 +#include "pldhash.h"
    1.13 +#include "nsXULElement.h"
    1.14 +
    1.15 +class nsTemplateMap {
    1.16 +protected:
    1.17 +    struct Entry {
    1.18 +        PLDHashEntryHdr mHdr;
    1.19 +        nsIContent*     mContent;
    1.20 +        nsIContent*     mTemplate;
    1.21 +    };
    1.22 +
    1.23 +    PLDHashTable mTable;
    1.24 +
    1.25 +    void
    1.26 +    Init() { PL_DHashTableInit(&mTable, PL_DHashGetStubOps(), nullptr, sizeof(Entry), PL_DHASH_MIN_SIZE); }
    1.27 +
    1.28 +    void
    1.29 +    Finish() { PL_DHashTableFinish(&mTable); }
    1.30 +
    1.31 +public:
    1.32 +    nsTemplateMap() { Init(); }
    1.33 +
    1.34 +    ~nsTemplateMap() { Finish(); }
    1.35 +
    1.36 +    void
    1.37 +    Put(nsIContent* aContent, nsIContent* aTemplate) {
    1.38 +        NS_ASSERTION(PL_DHASH_ENTRY_IS_FREE(PL_DHashTableOperate(&mTable, aContent, PL_DHASH_LOOKUP)),
    1.39 +                     "aContent already in map");
    1.40 +
    1.41 +        Entry* entry =
    1.42 +            reinterpret_cast<Entry*>(PL_DHashTableOperate(&mTable, aContent, PL_DHASH_ADD));
    1.43 +
    1.44 +        if (entry) {
    1.45 +            entry->mContent = aContent;
    1.46 +            entry->mTemplate = aTemplate;
    1.47 +        }
    1.48 +    }
    1.49 +
    1.50 +    void
    1.51 +    Remove(nsIContent* aContent) {
    1.52 +        PL_DHashTableOperate(&mTable, aContent, PL_DHASH_REMOVE);
    1.53 +
    1.54 +        for (nsIContent* child = aContent->GetFirstChild();
    1.55 +             child;
    1.56 +             child = child->GetNextSibling()) {
    1.57 +            Remove(child);
    1.58 +        }
    1.59 +    }
    1.60 +
    1.61 +
    1.62 +    void
    1.63 +    GetTemplateFor(nsIContent* aContent, nsIContent** aResult) {
    1.64 +        Entry* entry =
    1.65 +            reinterpret_cast<Entry*>(PL_DHashTableOperate(&mTable, aContent, PL_DHASH_LOOKUP));
    1.66 +
    1.67 +        if (PL_DHASH_ENTRY_IS_BUSY(&entry->mHdr))
    1.68 +            NS_IF_ADDREF(*aResult = entry->mTemplate);
    1.69 +        else
    1.70 +            *aResult = nullptr;
    1.71 +    }
    1.72 +
    1.73 +    void
    1.74 +    Clear() { Finish(); Init(); }
    1.75 +};
    1.76 +
    1.77 +#endif // nsTemplateMap_h__
    1.78 +

mercurial