dom/xslt/base/txExpandedNameMap.h
author Ryan VanderMeulen <ryanvm@gmail.com>
Thu, 09 Oct 2014 13:39:14 -0400
changeset 232804 cff6335a600972067f0fe41293b52003b0d1fa24
parent 232799 012853bd80b779c08a7bd89424eec47b50c2b0e1
child 233067 f4fa7b1b3f3fc71096c6e5c262e90f83651b9790
permissions -rw-r--r--
Backed out changeset 012853bd80b7 (bug 844457) for likely being the real cause of the mochitest-dt failures. CLOSED TREE

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef TRANSFRMX_EXPANDEDNAMEMAP_H
#define TRANSFRMX_EXPANDEDNAMEMAP_H

#include "nsError.h"
#include "txXMLUtils.h"
#include "nsTArray.h"

class txExpandedNameMap_base {
protected:
    /**
     * Adds an item, if an item with this key already exists an error is
     * returned
     * @param  aKey   key for item to add
     * @param  aValue value of item to add
     * @return errorcode
     */
    nsresult addItem(const txExpandedName& aKey, void* aValue);

    /**
     * Sets an item, if an item with this key already exists it is overwritten
     * with the new value
     * @param  aKey   key for item to set
     * @param  aValue value of item to set
     * @return errorcode
     */
    nsresult setItem(const txExpandedName& aKey, void* aValue,
                     void** aOldValue);

    /**
     * Gets an item
     * @param  aKey  key for item to get
     * @return item with specified key, or null if no such item exists
     */
    void* getItem(const txExpandedName& aKey) const;

    /**
     * Removes an item, deleting it if the map owns the values
     * @param  aKey  key for item to remove
     * @return item with specified key, or null if it has been deleted
     *         or no such item exists
     */
    void* removeItem(const txExpandedName& aKey);

    /**
     * Clears the items
     */
    void clearItems()
    {
        mItems.Clear();
    }

    class iterator_base {
    public:
        explicit iterator_base(txExpandedNameMap_base& aMap)
            : mMap(aMap),
              mCurrentPos(uint32_t(-1))
        {
        }

        bool next()
        {
            return ++mCurrentPos < mMap.mItems.Length();
        }

        const txExpandedName key()
        {
            NS_ASSERTION(mCurrentPos < mMap.mItems.Length(),
                         "invalid position in txExpandedNameMap::iterator");
            return txExpandedName(mMap.mItems[mCurrentPos].mNamespaceID,
                                  mMap.mItems[mCurrentPos].mLocalName);
        }

    protected:
        void* itemValue()
        {
            NS_ASSERTION(mCurrentPos < mMap.mItems.Length(),
                         "invalid position in txExpandedNameMap::iterator");
            return mMap.mItems[mCurrentPos].mValue;
        }

    private:
        txExpandedNameMap_base& mMap;
        uint32_t mCurrentPos;
    };
    
    friend class iterator_base;

    friend class txMapItemComparator;
    struct MapItem {
        int32_t mNamespaceID;
        nsCOMPtr<nsIAtom> mLocalName;
        void* mValue;
    };
    
    nsTArray<MapItem> mItems;
};

template<class E>
class txExpandedNameMap : public txExpandedNameMap_base
{
public:
    nsresult add(const txExpandedName& aKey, E* aValue)
    {
        return addItem(aKey, (void*)aValue);
    }

    nsresult set(const txExpandedName& aKey, E* aValue)
    {
        void* oldValue;
        return setItem(aKey, (void*)aValue, &oldValue);
    }

    E* get(const txExpandedName& aKey) const
    {
        return (E*)getItem(aKey);
    }

    E* remove(const txExpandedName& aKey)
    {
        return (E*)removeItem(aKey);
    }

    void clear()
    {
        clearItems();
    }

    class iterator : public iterator_base
    {
    public:
        explicit iterator(txExpandedNameMap& aMap)
            : iterator_base(aMap)
        {
        }

        E* value()
        {
            return (E*)itemValue();
        }
    };
};

template<class E>
class txOwningExpandedNameMap : public txExpandedNameMap_base
{
public:
    ~txOwningExpandedNameMap()
    {
        clear();
    }

    nsresult add(const txExpandedName& aKey, E* aValue)
    {
        return addItem(aKey, (void*)aValue);
    }

    nsresult set(const txExpandedName& aKey, E* aValue)
    {
        nsAutoPtr<E> oldValue;
        return setItem(aKey, (void*)aValue, getter_Transfers(oldValue));
    }

    E* get(const txExpandedName& aKey) const
    {
        return (E*)getItem(aKey);
    }

    void remove(const txExpandedName& aKey)
    {
        delete (E*)removeItem(aKey);
    }

    void clear()
    {
        uint32_t i, len = mItems.Length();
        for (i = 0; i < len; ++i) {
            delete (E*)mItems[i].mValue;
        }
        clearItems();
    }

    class iterator : public iterator_base
    {
    public:
        explicit iterator(txOwningExpandedNameMap& aMap)
            : iterator_base(aMap)
        {
        }

        E* value()
        {
            return (E*)itemValue();
        }
    };
};

#endif //TRANSFRMX_EXPANDEDNAMEMAP_H